android怎么查询sqlite中的数据
2个回答
展开全部
这个很简单的:
//打开或创建test.db数据库
SQLiteDatabasedb = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
//创建person表
db.execSQL("DROPTABLE IF EXISTS person");
db.execSQL("CREATE TABLE person (_idINTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
//插入数据
Personperson = new Person();
person.name= "john";
person.age = 30;
db.execSQL("INSERT INTO person VALUES(NULL, ?, ?)",new Object[]{person.name, person.age});
//读取数据
Cursor c = db.rawQuery("SELECT* FROM person WHERE age >= ?", new String[]{"33"});
while (c.moveToNext()) {
int _id = c.getInt(c.getColumnIndex("_id"));
String name = c.getString(c.getColumnIndex("name"));
int age = c.getInt(c.getColumnIndex("age"));
Log.i("db", "_id=>" + _id + ", name=>" + name + ", age=>" + age);
}
c.close();
//关闭当前数据库
db.close();
//打开或创建test.db数据库
SQLiteDatabasedb = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
//创建person表
db.execSQL("DROPTABLE IF EXISTS person");
db.execSQL("CREATE TABLE person (_idINTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
//插入数据
Personperson = new Person();
person.name= "john";
person.age = 30;
db.execSQL("INSERT INTO person VALUES(NULL, ?, ?)",new Object[]{person.name, person.age});
//读取数据
Cursor c = db.rawQuery("SELECT* FROM person WHERE age >= ?", new String[]{"33"});
while (c.moveToNext()) {
int _id = c.getInt(c.getColumnIndex("_id"));
String name = c.getString(c.getColumnIndex("name"));
int age = c.getInt(c.getColumnIndex("age"));
Log.i("db", "_id=>" + _id + ", name=>" + name + ", age=>" + age);
}
c.close();
//关闭当前数据库
db.close();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |