import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SQLHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "todo_db";//数据库名
private final static int DATABASE_VERSION = 1;//数据库版本号
private final static String TABLE_NAME = "todo_table";//数据库表名
public final static String FIELD_id = "_id";//
public final static String FIELD_TEXT = "todo_text";//字段
public SQLHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String sql = "CREATE TABLE " + TABLE_NAME + " (" + FIELD_id
+ " INTEGER primary key autoincrement, " + " " + FIELD_TEXT
+ " 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, null);
return cursor;
}
public long insert(String text) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
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 text) {
SQLiteDatabase db = this.getWritableDatabase();
String where = FIELD_id + " = ?";
String[] whereValue = { Integer.toString(id) };
ContentValues cv = new ContentValues();
cv.put(FIELD_TEXT, text);
db.update(TABLE_NAME, cv, where, whereValue);
}
}
2014-10-31Android学习------SQLite数据库操作(四)-----数据库模块(二)增删改查
最新推荐文章于 2024-09-04 19:45:09 发布