1、继承SqliteOpenHelper类
public class MyOpenHelper extends SQLiteOpenHelper {
public static final String DB_NAME="student.db";
public MyOpenHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(_id integer primary key autoincrement,name char(10),money integer(10))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
2、获取数据库实例
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db=oh.getWritableDatabase();
3、数据库增删查改操作
(1)插入
public void insertDatebase(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db=oh.getWritableDatabase();
db.execSQL("insert into person(name,money) values('ppa',7000);");
db.close();
}
API插入
ContentValues cv=new ContentValues();
cv.put("name", "ppa");
cv.put("money", 16000);
long id = db.insert("person", null, cv);
(2)更新
public void updateDatabase(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db = oh.getWritableDatabase();
db.execSQL("update person set money=? where name=?",new Object[]{13000,"ppa"});
}
API更新
ContentValues cv=new ContentValues();
cv.put("money", 16005);
int num = db.update("person", cv, "name=?", new String[]{"ppa"});
(3)删除
public void deleteDatabase(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db=oh.getWritableDatabase();
db.execSQL("delete from person where name ='李四'");
db.close();
}
API删除
int num = db.delete("person", "name=?", new String[]{"李四"});
(4)查询
public void select(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db = oh.getWritableDatabase();
// Cursor cursor = db.rawQuery("select name,money from person", null);
Cursor cursor = db.rawQuery("select * from person where name=?", new String[]{"潘平安"} );
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String money = cursor.getString(2);
System.out.println(name+";"+money);
}
}
API查询
Cursor cursor = db.query("person", new String[]{"name","money"}, "name=?", new String[]{"ppa"}, null, null, null);
1、继承SqliteOpenHelper类
public class MyOpenHelper extends SQLiteOpenHelper {
public static final String DB_NAME="student.db";
public MyOpenHelper(Context context) {
super(context, DB_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table person(_id integer primary key autoincrement,name char(10),money integer(10))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2、获取数据库实例
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db=oh.getWritableDatabase();
1
2
3、数据库增删查改操作
(1)插入
public void insertDatebase(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db=oh.getWritableDatabase();
db.execSQL("insert into person(name,money) values('ppa',7000);");
db.close();
}
1
2
3
4
5
6
API插入
ContentValues cv=new ContentValues();
cv.put("name", "ppa");
cv.put("money", 16000);
long id = db.insert("person", null, cv);
1
2
3
4
5
(2)更新
public void updateDatabase(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db = oh.getWritableDatabase();
db.execSQL("update person set money=? where name=?",new Object[]{13000,"ppa"});
}
1
2
3
4
5
API更新
ContentValues cv=new ContentValues();
cv.put("money", 16005);
int num = db.update("person", cv, "name=?", new String[]{"ppa"});
1
2
3
4
(3)删除
public void deleteDatabase(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db=oh.getWritableDatabase();
db.execSQL("delete from person where name ='李四'");
db.close();
}
1
2
3
4
5
6
API删除
int num = db.delete("person", "name=?", new String[]{"李四"});
1
2
(4)查询
public void select(){
MyOpenHelper oh=new MyOpenHelper(getContext());
SQLiteDatabase db = oh.getWritableDatabase();
// Cursor cursor = db.rawQuery("select name,money from person", null);
Cursor cursor = db.rawQuery("select * from person where name=?", new String[]{"潘平安"} );
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String money = cursor.getString(2);
System.out.println(name+";"+money);
}
}
1
2
3
4
5
6
7
8
9
10
11
API查询
Cursor cursor = db.query("person", new String[]{"name","money"}, "name=?", new String[]{"ppa"}, null, null, null);
1
2