android.database.sqlite.SQLiteDatatypeMismatchException:数据类型不匹配(代码 20 SQLITE_MISMATCH)
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20 SQLITE_MISMATCH)
我在将数据日期插入数据库时遇到问题。我尝试使 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);
}
};
这是显示的错误
error
数据类型不匹配非常具体您正在尝试将不是整数值的值插入 rowid 列或 rowid 列的别名
INTEGER PRIMARY KEY
(有或没有 AUTOINCREMENT) 导致 rowid 列的别名。
根据 public Boolean insertbudget(String id, String date)
那么你传递的是一个非整数字符串。
您可以通过使用 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);
}
我在将数据日期插入数据库时遇到问题。我尝试使 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);
}
};
这是显示的错误 error
数据类型不匹配非常具体您正在尝试将不是整数值的值插入 rowid 列或 rowid 列的别名
INTEGER PRIMARY KEY
(有或没有 AUTOINCREMENT) 导致 rowid 列的别名。
根据 public Boolean insertbudget(String id, String date)
那么你传递的是一个非整数字符串。
您可以通过使用 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);
}