Android教程之SQlit数据库操作 (2)
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
1.首先得新建数据库的代理类,通过这个类得到具体数据库的代理类。
mOpenHelper = new DatabaseHelper(this);
这个DatabaseHelper是继承SQLiteOpenHelper
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
2.由于操作的是SQLiteDatabase数据,故得到一个SQLiteDatabase类,这个类里边的方法可以对数据库进行具体的操作。
建立数据库:
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "create table Student(" + "stud_no text not null, "
+ "stud_name text );";
try {
db.execSQL(sql);
setTitle("create table ok!");
} catch (SQLException e) {
Log.e("ERROR", e.toString());
setTitle("create table Error!");
}
drop数据库:
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "drop table Student";
try {
db.execSQL(sql);
setTitle("drop table ok!");
} catch (SQLException e) {
Log.e("ERROR", e.toString());
setTitle("drop table Error!");
}
插入语句:
采用sql语句进行插入:
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql_1 = "insert into Student (stud_no, stud_name) values('S108', 'Lily Chen');";
String sql_2 = "insert into Student (stud_no, stud_name) values('S201', 'Tom Kao');";
String sql_3 = "insert into Student (stud_no, stud_name) values('S333', 'Peter Rabbit');";
try {
db.execSQL(sql_1);
db.execSQL(sql_2);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>