android - 在 sqlite 中使用 autoincrement 关键字时出错

标签 android sqlite

您好,我正在开发 android 应用程序,现在我必须使用 sqlite 数据库在手机中存储一些数据。现在,当我创建没有 autoincrement 关键字的数据库时。但是当我使用这个关键字错误时,它说

"java.lang.RuntimeException: Unable to start activity ComponentInfo{abc.tt/abc.tt.Home}: android.database.sqlite.SQLiteException: near "autoincrement": syntax error: create table calSimpleNote (_id integer PRIMERY KEY autoincrement , Cal_Id TEXT , User_ID INTEGER , Cal_Date TEXT , Note TEXT,Involvers TEXT, DeleteReason TEXT ,Create_Date TEXT ,Changed_date TEXT,EmailInvolvers TEXT,RemindSameDay INTEGER, RemindAllDay INTEGER,RemindBeforeDays INTEGER );" .

Here is my code to create database. 创建数据库时出错。如果有人有任何想法,请帮助我。谢谢。

我使用以下链接作为引用。 This

我正在使用这段代码来插入,但我没有递增。

public long insertCalSimpleNote(String noteId,int userId,String cal_date,String note,String Involved,String DeleteReason,String Create_Date, String Changed_date,String emailInvolvers,int remindSameday,int remindAllday,int remindBeforeday){

    ContentValues initialValues = new ContentValues();

    initialValues.put("_Id", 0);
    initialValues.put("Cal_Id", noteId);
    initialValues.put("User_ID", userId);
    initialValues.put("Cal_Date", cal_date);
    initialValues.put("Note", note);
    initialValues.put("Involvers", Involved);
    initialValues.put("DeleteReason", DeleteReason);
    initialValues.put("Create_Date", Create_Date);
    initialValues.put("Changed_date", Changed_date);
    initialValues.put("EmailInvolvers", emailInvolvers);
    initialValues.put("RemindSameDay", remindSameday);
    initialValues.put("RemindAllDay", remindAllday);
    initialValues.put("RemindBeforeDays", remindBeforeday);

    return db.insert("calSimpleNote", null, initialValues);
}

It set 0 at every row.

//Create Table
private static final String CREATE_SIMPLENOTE = "create table calSimpleNote (_id integer PRIMERY KEY, Cal_Id TEXT , User_ID INTEGER , Cal_Date TEXT , Note TEXT,Involvers TEXT, "+
                                                "DeleteReason TEXT ,Create_Date TEXT ,Changed_date TEXT,EmailInvolvers TEXT, "+ 
                                                "RemindSameDay INTEGER, RemindAllDay INTEGER,RemindBeforeDays INTEGER );"; 
@Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(CREATE_SIMPLENOTE);
    }

最佳答案

声明为 INTEGER PRIMARY KEY 的列将默认自动递增。

我从here偷的:

Short answer: A column declared INTEGER PRIMARY KEY will autoincrement.

Here is the long answer: If you declare a column of a table to be INTEGER PRIMARY KEY, then whenever you insert a NULL into that column of the table, the NULL is automatically converted into an integer which is one greater than the largest value of that column over all other rows in the table, or 1 if the table is empty. (If the largest possible integer key, 9223372036854775807, then an unused key value is chosen at random.) For example, suppose you have a table like this:

CREATE TABLE t1(
  a INTEGER PRIMARY KEY,
  b INTEGER
);

With this table, the statement

INSERT INTO t1 VALUES(NULL,123);

is logically equivalent to saying:

INSERT INTO t1 VALUES((SELECT max(a) FROM t1)+1,123);

关于android - 在 sqlite 中使用 autoincrement 关键字时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8698694/

相关文章:

安卓 OpenGL : draw to half-sized screen resolution

java - 当我使用sqlite运行android程序时如何防止重复行

android - 追加问号到url改造android

android - 创建一个包含固定数量项目的 SQLITE 表

android - 如何在辅助功能模式下将焦点设置在 Thumb for Range Seek Bar?

java - Android Studio : clang error: unknown argument: '-mandroid'

android - 由于每次启动时创建数据库而导致应用程序崩溃

c++ - 如何在 SQLite3 中查找 stmt 到行号?

python - 可移植/独立 Web 应用程序的潜在数据库系统?

java - Android SQL之类