假设我得到一个Cursor
by db.query()
。
然后我修改一行中的一个字段db.update()
,并且该行包含Cursor
在之前的查询中。此游标中的数据是否会更改?
像这样:
Cursor cur = db.query("[some parameter can get row 0]");
cur.moveToFirst;
int a = cur.getInt(0);
db.update("[some parameter can modify row 0 col 0]");
int b = cur.getInt(0);
(a==b) 是真还是假?
====== 2012.10.1 编辑 =========
我不是专业程序员,最近忙得没时间写代码。
如果我让你不开心,我很抱歉。
终于可以放一周假了,哈哈。
我做了一些这样的编码:
SQLiteOpenHelper h = null;
SQLiteDatabase db = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h=new SQLiteOpenHelper(this, "test.db", null,1){
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL("CREATE TABLE t1 (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT )");
arg0.execSQL("INSERT INTO t1 (name) VALUES ('name_wrong')");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}};
db=h.getWritableDatabase();
Cursor cur = db.query("t1", null, null, null, null, null, null);
cur.moveToFirst();
String s1=cur.getString(1);
db.execSQL("UPDATE t1 SET name = 'name_right'");
String s2=cur.getString(1);
Log.d("","s1 = "+s1);
Log.d("","s2 = "+s2);
Log.d("","s1 == s2 is "+s1.equals(s2));
Log.d("","requery...");
cur = db.query("t1", null, null, null, null, null, null);
cur.moveToFirst();
s2=cur.getString(1);
Log.d("","s1 = "+s1);
Log.d("","s2 = "+s2);
Log.d("","s1 == s2 is "+s1.equals(s2));
}
logcat 说:
10-01 03:11:28.356: D/(1514): s1 = name_wrong
10-01 03:11:28.356: D/(1514): s2 = name_wrong
10-01 03:11:28.356: D/(1514): s1 == s2 is true
10-01 03:11:28.356: D/(1514): requery...
10-01 03:11:28.356: D/(1514): s1 = name_wrong
10-01 03:11:28.356: D/(1514): s2 = name_right
10-01 03:11:28.356: D/(1514): s1 == s2 is false
所以我想我得到了答案谢谢大家。