Android基础之sqlite 数据库简单操作
尽管很简单,但是也存下来,以后直接粘过去就能用了。
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "lintest.db";
private static final String TBL_NAME = "lin";
private static final String CREATE_TBL = " create table "
+ " CollTbl(_id integer primary key autoincrement,name text,url text, desc text) ";
private SQLiteDatabase db;
DBHelper(Context c) {
super(c, DB_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
db.execSQL(CREATE_TBL);
}
public void insert(ContentValues values) {
SQLiteDatabase db = getWritableDatabase();
db.insert(TBL_NAME, null, values);
db.close();
}
public Cursor query() {
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
return c;
}
public void del(int id) {
if (db == null)
db = getWritableDatabase();
db.delete(TBL_NAME, "_id=?", new String[] { String.valueOf(id) });
}
public void close() {
if (db != null)
db.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
存入数据库内容
String name = "lin"
- String url = "http://www.cnblogs.com/CaptainLin"
- String desc = "test"
- ContentValues values = new ContentValues();
- values.put("name", name);
- values.put("url", url);
- values.put("desc", desc);
- DBHelper helper = new DBHelper(getApplicationContext());
- helper.insert(values);