我在將資料日期插入資料庫時??遇到問題。我嘗試使 ID 自動遞增,并且日期使用的是日期選擇器。是其他方式可以將日期存盤到資料庫中還是其他人在我的代碼中錯了?
下面是 dbhelper.java
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "Budget.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase DB) {
DB.execSQL("create Table Budget(id INTEGER primary key autoincrement not null, date TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase DB, int i, int i1) {
DB.execSQL("drop Table if exists Budget");
}
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd ", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
public Boolean insertbudget(String id, String date)
{
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("id", id);
contentValues.put("date", date);
long result=DB.insert("Budget",null, contentValues);
if(result==-1){
return false;
}else{
return true;
}
}
public Cursor getdata() {
SQLiteDatabase DB = this.getWritableDatabase();
Cursor cursor = DB.rawQuery("Select * from Budget", null);
return cursor;
}
下面是我宣告日期和資料庫是主要的代碼
btnAdd = findViewById(R.id.btnAdd);
id = findViewById(R.id.id);
DB = new DBHelper(this);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String idTXT = id.getText().toString();
String dateTXT = mDisplayDate.getText().toString();
Boolean checkAdddata = DB.insertbudget(idTXT, dateTXT);
if(checkAdddata==true)
Toast.makeText(Add.this, "New Entry Inserted", Toast.LENGTH_SHORT);
else
Toast.makeText(Add.this, "New Entry Not Inserted", Toast.LENGTH_SHORT);
}
});
//date
mDisplayDate = (TextView) findViewById(R.id.date);
mDisplayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
Add.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month 1;
Log.d(TAG, "onDateSet: dd/mm/yyyy:" day "/" month "/" year);
String date = day "/" month "/" year;
mDisplayDate.setText(date);
}
};
這是顯示 錯誤的錯誤
uj5u.com熱心網友回復:
資料型別不匹配是非常具體的,您正在嘗試將不是整數值的值插入 rowid 列或 rowid 列的別名中
INTEGER PRIMARY KEY(有或沒有 AUTOINCREMENT)產生 rowid 列的別名。
根據public Boolean insertbudget(String id, String date)then 您正在傳遞一個非整數字串。
您可以通過使用public Boolean insertbudget(long id, String date)然后調整代碼以適應(即將字串轉換為長)來消除這種情況的發生。然后您將得到的決議錯誤應該指向真正的原因。
但是,最簡單的解決方案是洗掉contentValues.put("id", id);SQLite 將分配適當值的行。然后您可以調整方法的簽名,因為不需要傳遞 id。所以你可能希望使用:-
public Boolean insertbudget(String date)
{
SQLiteDatabase DB = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("date", date);
return result=(DB.insert("Budget",null, contentValues) > 0);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/347980.html
標籤:爪哇 安卓 android-sqlite
