我有問題在SQLite數據庫中插入數據。當我點擊按鈕保存,我沒有看到在SQLite編輯器中的數據.. 這個代碼有什麼問題?SQLite編輯器中沒有數據
1)AddMedicine.java
private void AddData() {
btn3.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap queryValues = new HashMap();
queryValues.put("medicine", medicine.getText().toString());
queryValues.put("quantity", quantity.getText().toString());
queryValues.put("date", date.getText().toString());
queryValues.put("time", time.getText().toString());
boolean isInserted = myDb.insertData(queryValues);
if (isInserted = true) {
Toast.makeText(AddMedicine.this, "Data inserted", Toast.LENGTH_LONG).show();
Intent i = new Intent(AddMedicine.this, MainActivity.class);
startActivity(i);
// this.callHomeActivity(view);
} else
Toast.makeText(AddMedicine.this, "Data not inserted", Toast.LENGTH_LONG).show();
}
}
);
}
2)DatabaseHelper.java
public boolean insertData(String Name, String quantity, String Date, String Time) {
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, Name);
contentValues.put(COL_3, quantity);
contentValues.put(COL_4, Date);
contentValues.put(COL_5, Time);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1) {
return false;
} else {
return true;
}
}
好吧,以'if(isInserted = true)'開始並沒有做你想做的事。它應該是'if(isInserted == true)'或者更簡單的'if(isInserted)'。 (同樣,你的返回語句後面可能只是'返回結果!= -1;',現在沒有錯,但比它需要的時間長。)通過這個改變,你得到了什麼祝酒? –
我也強烈建議您避免使用原始類型,並遵循Java命名約定,並避免在所有內容中使用字符串 - 而且您向我們顯示的insertData方法不是您要調用的方法。 –
如果(isInserted = true),我剛剛看到了這個代碼..謝謝你的提醒。我應該使用db.query不是嗎?不是db.rawQuery對不對?你是什麼意思insertData方法是不是你打來的? – BlurCode