创建数据步骤
1、创建一个类 继承 SqliteOpenHelper 实现 onCreate onUpgrade
2、创建这个helper类的一个对象,调用getReadableDatabase()方法,会帮我们创建打开一个数据库
DBHelper helper =new DBHelper(mContext);
//调用getReadableDatabase方法,初始化数据库SQLiteDatabase db = helper.getReadableDatabase();
3、增加 删除 修改 查询
DBHelper helper =new DBHelper(mContext);
//调用getReadableDatabase方法,初始化数据库
SQLiteDatabase db = helper.getReadableDatabase();
String name = "张三";
db.execSQL ( "insert into user(name) values(?)",(new Object[]{name}));
db.close();
DBHelper helper =new DBHelper(mContext);
//调用getReadableDatabase方法,初始化数据库
SQLiteDatabase db = helper.getReadableDatabase();
String name = "沈震";
db.execSQL ( "delete from user where id=?",(new Object[]{1}));
db.close();
DBHelper helper =new DBHelper(mContext);
//调用getReadableDatabase方法,初始化数据库
SQLiteDatabase db = helper.getReadableDatabase();
String name = "沈震1";
int id = 1;
db.execSQL ( "update user set name=? where id=?",(new Object[]{name,id}));
db.close();
DBHelper helper =new DBHelper(mContext);
//调用getReadableDatabase方法,初始化数据库
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery ("select * from user",(new String[]{}));
if (cursor!=null&&cursor.getCount ()>0){
while (cursor.moveToNext ()){
String name = cursor.getString (1);
Log.i ( "queryDB", "queryDB: "+name );
}
}
db.close ();