我正在構建android移動應用程序,並使用SQLite作爲其數據庫。我無法在表格上插入記錄,並且它返回Toast消息,它是「插入失敗」意味着保存數據失敗。使用SQLite在表上插入數據時出錯使用SQLite
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MobileBuddy.db";
public static final String TABLE_NAME = "bets_table";
public static final String COL_1 = "COMBINATION";
public static final String COL_2 = "DRAW_DATE";
public static final String COL_3 = "TRANS_DATE";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + COL_1 + " TEXT, " + COL_2 + " DATETIME, " + COL_3 + " DATETIME)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertBetData(String combination, String drawDate, String transDate) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, combination);
contentValues.put(COL_2, drawDate);
contentValues.put(COL_3, transDate);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == 1) return true; else return false;
}
}
我用下面的代碼中的onClick事件,這應該觸發數據的插入在桌子上。
calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
boolean isSave = lottoDb.insertBetData(validBet, sdf.format(calendar.getTime()), sdf.format(calendar.getTime()));
if (isSave == true)
Toast.makeText(PlaySixNumbers.this, "Inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(PlaySixNumbers.this, "Insert failed", Toast.LENGTH_SHORT).show();
我的代碼有什麼問題?希望你能理解我的詢問..
附上你的logcat輸出 – surya
嘗試把.. sdf.format(calendar.getTime())放到一個String中......並將這個String傳遞給你的insertMethod –