我正在尝试将一些数据上传到SQLite数据库,但是当我单击“提交”时,我会不断收到“未插入数据”Toast消息。
我不确定为什么我的数据没有被正确传递?
DatabaseHelper.java:
public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME = "streaks.db"; // Name of DB
public static final String TABLE_NAME = "streak_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "STREAKNAME";
public static final String COL_3 = "STREAKCATEGORY";
public static final String COL_4 = "DATESTARTED";
public static final String COL_5 = "DAYSKEPT";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, STREAKNAME TEXT, STREAKCATEGORY TEXT, DATESTARTED TEXT, DAYSKEPT INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String STREAKNAME, String STREAKCATEGORY, String DATESTARTED, int DAYSKEPT){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, STREAKNAME);
contentValues.put(COL_3, STREAKCATEGORY);
contentValues.put(COL_4, DATESTARTED);
contentValues.put(COL_5, DAYSKEPT);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result == -1){
return false;
} else return true;
}
}
MainActivity.java的片段:
// Declaring variables from private variables above
db = new DatabaseHelper(MainActivity.this);
addStreakName = chooseName.getText().toString();
addCategory = prefs.getString("Category", "");
addToday = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
if(prefs.getInt("todayOrChosen", 1) == 1){
addStartedZero = 0;
streakList.add(new Streak(addStreakName, addCategory, addToday, addStartedZero));
boolean isInserted = db.insertData(addStreakName, addCategory, addToday, 0);
if(isInserted){
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}
错误日志:
08-04 17:46:41.409 22835-22835/? E/SQLiteLog: (20) statement aborts at 5: [INSERT INTO streak_table(STREAKNAME,STREAKCATEGORY,DAYSKEPT,DATESTARTED) VALUES (?,?,?,?)] datatype mismatch
08-04 17:46:41.410 22835-22835/? D/AndroidRuntime: Shutting down VM
08-04 17:46:41.413 22835-22835/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: carter.streakly, PID: 22835
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:780)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1471)
at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1367)
at carter.streakly.DatabaseHelper.insertData(DatabaseHelper.java:43)
at carter.streakly.MainActivity$11.onClick(MainActivity.java:301)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:1)
我刚刚运行了你的代码,创建了可以正常使用的表格。
您可能已经编辑了其中一列的类型,但表格尚未更新。尝试删除应用程序的数据或卸载它一次并重新构建它。
答案 1 :(得分:-1)
哦发现错误。在您创建表的execSQL方法中,您忘记添加&#39 ;;' E.G。
db.execSQL("create table " +
TABLE_NAME +
" (ID INTEGER PRIMARY KEY AUTOINCREMENT,
STREAKNAME TEXT,
STREAKCATEGORY TEXT,
DATESTARTED TEXT,
DAYSKEPT INTEGER);"); //right here add the semicolon