Android 小项目之SQLite 使用法门
2010-06-13 15:25:00 来源:WEB开发网public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql="Create table "+TABLE_NAME+"("+FIELD_ID+" integer primary key autoincrement,"
+FIELD_TITLE+" text );";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
public Cursor select()
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, " _id desc");
return cursor;
}
public long insert(String Title)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(FIELD_TITLE, Title);
long row=db.insert(TABLE_NAME, null, cv);
return row;
}
public void delete(int id)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=FIELD_ID+"=?";
String[] whereValue={Integer.toString(id)};
db.delete(TABLE_NAME, where, whereValue);
}
public void update(int id,String Title)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=FIELD_ID+"=?";
String[] whereValue={Integer.toString(id)};
ContentValues cv=new ContentValues();
cv.put(FIELD_TITLE, Title);
db.update(TABLE_NAME, cv, where, whereValue);
}
}
创建和打开数据库
上篇通过构造函数来创建数据库,看一下构造函数的方法
android.database.sqlite.SQLiteOpenHelper.SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version)
更多精彩
赞助商链接