我正在嘗試更新一個值并從 sqlite 資料庫中獲取它:
private static final String TABLE_INTERVALLE_DE_MESURE = "intervalle_de_mesure";
private static final String COLONNE_ID = "_id";
private static final String COLONNE_VALEUR = "valeur";
private static final String CREATE_INTERVALLE_DE_MESURE =
"create table " TABLE_INTERVALLE_DE_MESURE " ("
COLONNE_ID " integer primary key autoincrement, "
COLONNE_VALEUR " integer default 0);";
public MaBD_IntervalleDeMesure (Context context) {
super(context, dbName, null, dbVersion) ;
}
public void updateIntervalle(int intervalle) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLONNE_VALEUR, intervalle);
db.update(TABLE_INTERVALLE_DE_MESURE, values, COLONNE_ID " = ?", new String[] {Long.toString(1)});
db.close();
}
public int getIntervalle() {
int res = 0;
SQLiteDatabase db = this.getReadableDatabase();
String maRequeteIntervalle = "SELECT " COLONNE_VALEUR " FROM " TABLE_INTERVALLE_DE_MESURE ";";
Cursor cursor = db.rawQuery(maRequeteIntervalle, null);
if (cursor.getCount() != 0) {
cursor.moveToFirst();
res = cursor.getInt(0);
}
cursor.close();
db.close();
return res;
}
}
updateIntervalle 函式中 db.update 的回傳回傳一個非 -1 的數字。
但是,cursor.getCount() 回傳 0。為什么?
uj5u.com熱心網友回復:
updateIntervalle 函式中 db.update 的回傳回傳一個非 -1 的數字。
代碼似乎沒有任何問題。但是, 更新便捷方法永遠不會回傳 -1。它回傳已更新的行數,如果沒有,則回傳 0。
但是,cursor.getCount() 回傳 0。為什么?
在一個猜測,因為在表中沒有資料。
- 也許您假設 update方法將添加一行。它不會,它只會更新現有的行。如果是這樣,那么您將需要插入一行。
示范
使用您的代碼的以下改編: -
class MaBD_IntervalleDeMesure extends SQLiteOpenHelper {
public static final String TABLE_INTERVALLE_DE_MESURE = "intervalle_de_mesure";
public static final String COLONNE_ID = "_id";
public static final String COLONNE_VALEUR = "valeur";
private static final String DBNAME = "mydb";
private static final int DBVERSION = 1;
private static final String CREATE_INTERVALLE_DE_MESURE =
"create table " TABLE_INTERVALLE_DE_MESURE " ("
COLONNE_ID " integer primary key autoincrement, "
COLONNE_VALEUR " integer default 0);";
public MaBD_IntervalleDeMesure (Context context) {
super(context, DBNAME, null, DBVERSION) ;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_INTERVALLE_DE_MESURE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public void updateIntervalle(int intervalle) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLONNE_VALEUR, intervalle);
int rv = db.update(TABLE_INTERVALLE_DE_MESURE, values, COLONNE_ID " = ?", new String[] {Long.toString(1)});
Log.d("DBINFO","Result of updateIntervalle is " rv " (0 = nothing update > 0 = number of rows updated");
//db.close(); closing database is inefficient
}
public int getIntervalle() {
int res = 0;
SQLiteDatabase db = this.getReadableDatabase();
String maRequeteIntervalle = "SELECT " COLONNE_VALEUR " FROM " TABLE_INTERVALLE_DE_MESURE ";";
Cursor cursor = db.rawQuery(maRequeteIntervalle, null);
if (cursor.getCount() != 0) {
cursor.moveToFirst();
res = cursor.getInt(0);
}
cursor.close();
//db.close(); again closing is inefficient
return res;
}
}
連同一個活動MainActivity為:-
public class MainActivity extends AppCompatActivity {
MaBD_IntervalleDeMesure dbhelper;
SQLiteDatabase db;
public static final String TAG = "DBINFO";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TEST 1 - Get from empty table
dbhelper = new MaBD_IntervalleDeMesure(this);
Log.d(TAG,"Test 1 - empty database results in " dbhelper.getIntervalle());
// TEST 2 - Update noithing as empty table
Log.d(TAG,"Test 2 - update of empty database (check log for result logged by update)");
dbhelper.updateIntervalle(1);
// TEST 3 - Insert new row (id will very likely be 1)
ContentValues cv = new ContentValues();
cv.put(MaBD_IntervalleDeMesure.COLONNE_VALEUR,999);
db = dbhelper.getWritableDatabase();
db.insert(MaBD_IntervalleDeMesure.TABLE_INTERVALLE_DE_MESURE,null,cv);
Log.d(TAG,"Test 3 - update of empty database (check log for result logged by update)");
dbhelper.updateIntervalle(888);
Log.d(TAG,"Test 4 - database with a single row results in " dbhelper.getIntervalle());
}
}
然后日志包括:-
2022-01-06 08:12:03.314 D/DBINFO: Test 1 - empty database results in 0
2022-01-06 08:12:03.314 D/DBINFO: Test 2 - update of empty database (check log for result logged by update)
2022-01-06 08:12:03.315 D/DBINFO: Result of updateIntervalle is 0 (0 = nothing update > 0 = number of rows updated
2022-01-06 08:12:03.315 D/DBINFO: Test 3 - update of empty database (check log for result logged by update)
2022-01-06 08:12:03.316 D/DBINFO: Result of updateIntervalle is 1 (0 = nothing update > 0 = number of rows updated
2022-01-06 08:12:03.317 D/DBINFO: Test 4 - database with a single row results in 888
所以:-
測驗 1運行您的getIntervalle方法,該方法回傳 0,因為此階段表中沒有資料。
測驗 2報告該updateIntervalle方法不執行任何操作,因為表中沒有要更新的行。
TEST 3報告,因為現在有一個合適的行(誰的id是1),該 1 行已更新。
TEST 4報告已提取值 888(從 999 更新)。
此外,如果使用App 檢查,則可以查看資料庫,它顯示:-

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/405699.html
標籤:
