大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容介绍了“如何使用ContentValues对数据库进行相关操作”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
成都创新互联公司专注于揭东网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供揭东营销型网站建设,揭东网站制作、揭东网页设计、揭东网站官网定制、微信小程序服务,打造揭东网络公司原创品牌,更为您提供揭东网站排名全网营销落地服务。
在main.xml中:
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal">
创建数据库的建表操作类Mytab.java
package com.li.sqlite;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
public class Mytab {
private static final String TABLENAME = "mytab"; // 表示要操作的数据表名称
private SQLiteDatabase db = null; // 数据库操作
public Mytab(SQLiteDatabase db) {
this.db = db;
}
public void insert(String name,String birthday) { //向表中增加数据
ContentValues cv = new ContentValues();
cv.put("name",name);
cv.put("birthday",birthday);
this.db.insert(TABLENAME, null, cv);
this.db.close() ;
}
public void update(int id, String name, String birthday) { //修改表的数据
ContentValues cv = new ContentValues();
cv.put("name",name);
cv.put("birthday",birthday);
String whereCaluse = "id=?";
String whereArgs[] = new String[]{String.valueOf(id)};
this.db.update(TABLENAME, cv, whereCaluse, whereArgs);
this.db.close() ;
}
public void delete(int id) { //删除表的数据
String whereCaluse = "id=?";
String whereArgs[] = new String[]{String.valueOf(id)};
this.db.delete(TABLENAME, whereCaluse, whereArgs);
this.db.close() ;
}
}
在MyDatabaseHelper.java类中:
package com.li.sqlite;
//数据库的辅助操作类
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASENAME = "liyewen.db" ;
private static final int DATABASERVERSION = 1 ; // 设置数据库的版本
private static final String TABLENAME = "mytab" ;
public MyDatabaseHelper(Context context) { // 用户最关心的也肯定只是Context
super(context, DATABASENAME, null, DATABASERVERSION);
}
@Override
public void onCreate(SQLiteDatabase db) { // 创建数据表
String sql = "CREATE TABLE " + TABLENAME + "("
+ "id INTEGER PRIMARY KEY ," // 在SQLite中设置为Integer、PRIMARY KEY则ID自动增长
+ "name VARCHAR(50) NOT NULL ,"
+ "birthday DATE NOT NULL" + ")";
db.execSQL(sql) ; // 执行SQL
System.out.println("****************** 创建:onCreate()。");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS " + TABLENAME ;
db.execSQL(sql) ;
System.out.println("****************** 更新:onUpgrade()。");
this.onCreate(db) ;
}
}
在MySQLiteDemo.java中:
package com.li.sqlite;
import android.app.Activity;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MySQLiteDemo extends Activity {
private Button inserBut = null;
private Button updateBut = null;
private Button deleteBut = null;
private SQLiteOpenHelper helper = null;
private Mytab mtab = null;
private static int count = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.inserBut = (Button)super.findViewById(R.id.insertBut);
this.updateBut = (Button)super.findViewById(R.id.updateBut);
this.deleteBut = (Button)super.findViewById(R.id.deleteBut);
this.helper = new MyDatabaseHelper(this);
this.inserBut.setOnClickListener(new InertOnClickListenerImpl());
this.updateBut.setOnClickListener(new UpdateOnClickListenerImpl());
this.deleteBut.setOnClickListener(new DeleteOnClickListenerImpl());
}
private class InertOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MySQLiteDemo.this.mtab = new Mytab(
MySQLiteDemo.this.helper.getWritableDatabase());
MySQLiteDemo.this.mtab.insert("liyewen" + count++, "1988-08-16");
}
}
private class UpdateOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MySQLiteDemo.this.mtab = new Mytab(
MySQLiteDemo.this.helper.getWritableDatabase());
MySQLiteDemo.this.mtab.update(72, "update", "1988/8/15");
}
}
private class DeleteOnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
MySQLiteDemo.this.mtab = new Mytab(
MySQLiteDemo.this.helper.getWritableDatabase());
MySQLiteDemo.this.mtab.delete(73);
}
}
}
“如何使用ContentValues对数据库进行相关操作”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!