android SQLite数据库查询
现在我有一个数据库(下图),现在要根据条码字段(barcode字段)判断这个条码是对应的哪一个物品(B7,B4代表物品),用代码怎么实现?...
现在我有一个数据库(下图),现在要根据条码字段(barcode字段)判断这个条码是对应的哪一个物品(B7,B4代表物品),用代码怎么实现?
展开
1个回答
展开全部
这个很简单的:
//打开或创建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();
追问
我是找某一行的数据,是不是把 Cursor c = db.rawQuery("SELECT* FROM person WHERE age >= ?", new String[]{"33"});
这里的33换成我想查的字段,然后string product = c.getString(c.getColumnIndex("product")); 是这样吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询