插入数据库时​​出现问题

Problems when INSERT into DB

当我尝试将数据提交到数据库时,应用程序崩溃了。

以下是 java 文件中与创建数据并将数据插入数据库有关的部分。

我的Contract.javaclass有以下信息:

public class Contract {
    public static abstract class customReminder{
        public static final String TABLE_NAME = "CUSTOM_REMINDER";
        public static final String ID = "ID";
        public static final String TITLE = "TITLE";
        public static final String DESCRIPTION = "DESCRIPTION";
        public static final String DATE_TIME = "DATE_TIME";

        public static final String[] REMINDER_COLUMNS = {ID, TITLE, DESCRIPTION, DATE_TIME};

    }

我的DBHelper.javaclass:

//SQLite statement for custom reminder table
public static final String CUSTOM_REMINDER_TABLE = "CREATE TABLE " + Contract.customReminder.TABLE_NAME + "("
        + Contract.customReminder.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + Contract.customReminder.TITLE + " TEXT,"
        + Contract.customReminder.DESCRIPTION + " TEXT,"
        + Contract.customReminder.DATE_TIME + " TEXT,"
        + ");";

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CUSTOM_REMINDER_TABLE);
}

我的CustumRemider.javaclass:

public boolean createCustomReminder(String title, String description, String dateTime){
    ContentValues contentValues = new ContentValues();
    contentValues.put(mAllColumns[1], title);
    contentValues.put(mAllColumns[2], description);
    contentValues.put(mAllColumns[3], dateTime);
    long result = mDatabase.insert(Contract.customReminder.TABLE_NAME, null, contentValues);
    return result != -1;
}

我从 Android Device Monitor 下载数据库到我的 PC 上,然后用 SQLiteBrowser 打开它。我注意到创建了 table android_metadata 而不是 CUSTOM_REMINDER.

CREATE TABLE android_metadata (locale TEXT)

我既不知道查询来自何处,也不知道它为什么这样做。

你需要改变 在 create table query

中的最后一个参数后删除逗号 (",")
public static final String CUSTOM_REMINDER_TABLE = "CREATE TABLE " + Contract.customReminder.TABLE_NAME + "("
    + Contract.customReminder.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + Contract.customReminder.TITLE + " TEXT,"
    + Contract.customReminder.DESCRIPTION + " TEXT,"
    + Contract.customReminder.DATE_TIME + " TEXT"
    + ");";

而不是这个

public static final String CUSTOM_REMINDER_TABLE = "CREATE TABLE " + Contract.customReminder.TABLE_NAME + "("
    + Contract.customReminder.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + Contract.customReminder.TITLE + " TEXT,"
    + Contract.customReminder.DESCRIPTION + " TEXT,"
    + Contract.customReminder.DATE_TIME + " TEXT,"
    + ");";