大部分代码转自 http://blog.youkuaiyun.com/barryhappy/article/details/7240390
DBUtil类,继承自SQLiteOpenHelper,用以实现各种操作功能:
package barry.android.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBUtil extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "db2004";
private final static int DATABASE_VERSION = 1;
private static final String TABLE_NAME ="students";
private static final String FILED_1 = "name";
private static final String FILED_2 = "password";
public DBUtil(Context context){
super(context, DATABASE_NAME,null,DATABASE_VERSION);
System.out.println("new DBUtil");
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE "+TABLE_NAME+" ( "+FILED_1 +" TEXT, "+ FILED_2 +" TEXT );";
db.execSQL(sql);
System.out.println("oncreate创建表");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("onUpgrade删除表");
this.onCreate(db);
}
/**
* 查询表中所有的数据
* @return
*/
public Cursor select(){
return this.getReadableDatabase()
.query(TABLE_NAME, null, null, null, null, null, null);
}
/**
* 插入一条数据到表中
* @param name 字段一的值
* @param password 字段二的值
*/
public void insert(String name ,String password){
ContentValues cv = new ContentValues();
cv.put(FILED_1, name);
cv.put(FILED_2, password);
this.getWritableDatabase().insert(TABLE_NAME, null, cv);
this.getWritableDatabase().close();//关闭数据库对象
}
/**
* 删除表中的若干条数据
* @param name 一个包含所有要删除数据的"name"字段的数组
*/
public void delete(String[] name){
String where = FILED_1+" = ?";
String[] whereValues = name;
this.getWritableDatabase().delete(TABLE_NAME, where, whereValues);
this.getWritableDatabase().close();
}
/**
* 更新表中的数据(修改字段二"password")
* @param name 要更新的数据"name"字段值
* @param newPassword 新的"password"字段
*/
public void update(String name,String newPassword){
ContentValues cv = new ContentValues();
cv.put(FILED_2, newPassword);
String where =FILED_1+" = ?";
String[] whereValues= {name};
this.getWritableDatabase().update(TABLE_NAME, cv, where, whereValues);
this.getWritableDatabase().close();
}
/**
* 清空表中的数据
*/
public void clean (){
this.getWritableDatabase().execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
System.out.println("clean删除表");
this.onCreate(this.getWritableDatabase());
this.getWritableDatabase().close();
}
}
操作方法
package barry.android.db;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
public class Demo04_helperActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DBUtil dbUtil = new DBUtil(this);
dbUtil.insert("周杰伦", "jaychou");
dbUtil.insert("韩寒", "twocolds");
dbUtil.insert("郭德纲", "yunhejiuxiao");
System.out.println("***********************************全部数据息");
printData(dbUtil);
dbUtil.delete(new String[]{"周杰伦"});
System.out.println("***********************************删除'周杰伦'之后数据");
printData(dbUtil);
dbUtil.update("郭德纲", "longtengsihai");;
System.out.println("***********************************修改‘郭德纲’的密码为'longtengsihai'");
printData(dbUtil);
dbUtil.clean();
}
private void printData(DBUtil dbUtil) {
Cursor cursor = dbUtil.select();
if(cursor.moveToFirst()){
System.out.println("当前表中的数据条数:"+cursor.getCount());
do{
System.out.println(cursor.getString(0)+cursor.getString(1));
}while(cursor.moveToNext());
}
cursor.close();
}
}
一般来说 我都喜欢把数据库弄成单例的
private static DBUtil instance;
private DBUtil (Context context) {
super(context, dbName, null, DATABASE_VERSION);
}
public static DBUtil getInstance(Context context) {
if (instance == null) {
instance = new DBUtil (context);
}
return instance;
}