@Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table userinfo(uid primary key,phone,email,password )"); db.execSQL("insert into userinfo(uid,password)values(\"admin\",\""+getStringMD5("admin000000")+"\") "); } @Override public void onUpgrade(SQLiteDatabase db, int olds, int news) { if(olds==1&&news==2) { } if(olds==2&&news==3怎么增加查询
时间: 2024-03-14 16:44:13 浏览: 74
在 onUpgrade 方法中增加查询操作的步骤如下:
1. 首先,获取一个可读写的 SQLiteDatabase 对象:
```
SQLiteDatabase db = getWritableDatabase();
```
2. 然后,构造一个查询语句,例如:
```
String query = "SELECT * FROM userinfo WHERE uid='admin'";
```
这个查询语句的意思是从 userinfo 表中查询 uid 为 'admin' 的记录。
3. 接着,执行查询操作,并获取查询结果:
```
Cursor cursor = db.rawQuery(query, null);
```
这个操作会返回一个 Cursor 对象,其中包含了查询结果。
4. 最后,遍历查询结果,获取需要的数据:
```
if (cursor.moveToFirst()) {
String uid = cursor.getString(cursor.getColumnIndex("uid"));
String password = cursor.getString(cursor.getColumnIndex("password"));
Log.i(TAG, "uid: " + uid + ", password: " + password);
}
```
这个操作会遍历查询结果,并输出 uid 和 password 字段的值。
完整的代码如下:
```
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 2 && newVersion == 3) {
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM userinfo WHERE uid='admin'";
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
String uid = cursor.getString(cursor.getColumnIndex("uid"));
String password = cursor.getString(cursor.getColumnIndex("password"));
Log.i(TAG, "uid: " + uid + ", password: " + password);
}
}
}
```
阅读全文