android的sqlite怎么查询数据
2016-07-28 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
这个很简单的:
//打开或创建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();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询