this.deleteDatabase("Db");
SQLiteDatabase db = this.openOrCreateDatabase("Db", MODE_PRIVATE, null);
db.execSQL("PRAGMA foreign_keys = ON;");
db.execSQL("CREATE TABLE class (id INTEGER PRIMARY KEY, name VARCHAR, data TEXT)");
db.execSQL("INSERT INTO class (id, name, data) VALUES (1, '初一', '班主任:王老师')");
db.execSQL("INSERT INTO class (id, name) VALUES (2, '初二')");
db.execSQL("INSERT INTO class (id, name) VALUES (3, '初三')");
db.execSQL("CREATE TABLE student (id INTEGER PRIMARY KEY, name VARCHAR, data TEXT, class_id INTEGER, FOREIGN KEY (class_id) REFERENCES class(id))");
db.execSQL("INSERT INTO student (id, name, class_id) VALUES (1, '张明一', 3)");
db.execSQL("INSERT INTO student (id, name) VALUES (2, '张明二')");
db.execSQL("INSERT INTO student (id, name) VALUES (3, '张明三')");
db.close();
db = this.openOrCreateDatabase("Db", MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("SELECT * FROM student WHERE id=?", new String[]{"2"});
if(cursor.getCount()!=0){
cursor.moveToFirst();
int i = cursor.getInt(0);
String s = cursor.getString(1);
String s2 = cursor.getString(2);
int i2 = cursor.getInt(3);
Log.d("Tag", s + ":" + i + ":" + s2);
}
SQLiteDatabase db = this.openOrCreateDatabase("Db", MODE_PRIVATE, null);
db.execSQL("PRAGMA foreign_keys = ON;");
db.execSQL("CREATE TABLE class (id INTEGER PRIMARY KEY, name VARCHAR, data TEXT)");
db.execSQL("INSERT INTO class (id, name, data) VALUES (1, '初一', '班主任:王老师')");
db.execSQL("INSERT INTO class (id, name) VALUES (2, '初二')");
db.execSQL("INSERT INTO class (id, name) VALUES (3, '初三')");
db.execSQL("CREATE TABLE student (id INTEGER PRIMARY KEY, name VARCHAR, data TEXT, class_id INTEGER, FOREIGN KEY (class_id) REFERENCES class(id))");
db.execSQL("INSERT INTO student (id, name, class_id) VALUES (1, '张明一', 3)");
db.execSQL("INSERT INTO student (id, name) VALUES (2, '张明二')");
db.execSQL("INSERT INTO student (id, name) VALUES (3, '张明三')");
db.close();
db = this.openOrCreateDatabase("Db", MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("SELECT * FROM student WHERE id=?", new String[]{"2"});
if(cursor.getCount()!=0){
cursor.moveToFirst();
int i = cursor.getInt(0);
String s = cursor.getString(1);
String s2 = cursor.getString(2);
int i2 = cursor.getInt(3);
Log.d("Tag", s + ":" + i + ":" + s2);
}
SQLite数据库操作示例

本文通过一个具体的示例展示了如何使用Android平台上的SQLite数据库进行基本的操作,包括创建表、插入数据、查询数据等过程。
470

被折叠的 条评论
为什么被折叠?



