Android表创建失败(接近“autoincrement”:语法错误)?

10
 public static final String MYDATABASE_NAME = "MY_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_TABLE";
 public static final String MYDATABASE_TABLE2 = "MY_TABLE2";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String KEY_ID2 = "_id2";
 public static final String KEY_CONTENT1 = "Content1";
 public static final String KEY_CONTENT2 = "Content2";
 public static final String KEY_CONTENT3 = "Content3";

 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" 
                                                        +KEY_ID + " integer primary key autoincrement, " 
                                                        + KEY_CONTENT1 + " text not null);";

 private static final String SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE2 + " (" 
                                                        + KEY_ID2 + " integer autoincrement, " 
                                                        + KEY_CONTENT2 + " text not null, " 
                                                        + KEY_CONTENT3 + " text not null, "
                                                        + " FOREIGN KEY ("+KEY_ID2+") REFERENCES "+MYDATABASE_TABLE+" ("+KEY_ID+"));";

我找不出下面的错误原因,请帮忙看看,谢谢。

09-29 13:41:19.760: ERROR/Database(334): Failure 1 (near "autoincrement": syntax error) on 0x218df0 when preparing 'create table MY_TABLE2 (_id2 integer autoincrement, Content2 text not null, Content3 text not null, FOREIGN KEY (_id2) REFERENCES MY_TABLE (_id));'.

09-29 13:41:19.770: DEBUG/AndroidRuntime(334): Shutting down VM

09-29 13:41:19.770: WARN/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

09-29 13:41:19.791: ERROR/AndroidRuntime(334): FATAL EXCEPTION: main

09-29 13:41:19.791: ERROR/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{sep.com/sep.com.SepActivity}: android.database.sqlite.SQLiteException: near "autoincrement": syntax error: create table MY_TABLE2 (_id2 integer autoincrement, Content2 text not null, Content3 text not null, FOREIGN KEY (_id2) REFERENCES MY_TABLE (_id));

5个回答

38
简而言之:在SQLite中,声明为INTEGER PRIMARY KEY的列将自动递增。SQLite中没有autoincrement关键字,这就是您收到错误的原因。

您可以在SQLite FAQ上了解更多信息。

编辑:只需编写integer primary key即可。 SQLite将自动递增您的ids

EDIT2:您的onUpgrade()方法应该像这样:

  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            Log.w("MyAppTag","Updating database from version " + oldVersion + " to "
                    + newVersion + " .Existing data will be lost.");
            db.execSQL("DROP TABLE IF EXISTS " + MY_TABLE);
            db.execSQL("DROP TABLE IF EXISTS " + MY_TABLE2);
            onCreate(db);
        }

谢谢,但是我该如何在代码中添加自增。我对Android编程非常新,请您好心告诉我应该怎么做。 - checkmate
我按照你说的做了,但是出现了以下错误:09-29 14:16:58.580: ERROR/AndroidRuntime(464): java.lang.RuntimeException: Unable to start activity ComponentInfo{sep.com/sep.com.SepActivity}: android.database.sqlite.SQLiteException: no such column: Content2: , while compiling: SELECT _id, Content1, Content2, Content3 FROM MY_TABLE - checkmate
1
我认为你需要升级你的数据库。增加版本并确保你正确地实现了onUpdate()。 - Ovidiu Latcu
再次问候, 好的,如何做到...每当我进行更改时,它都不会得到更新。public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO 自动生成的方法存根 }这仅适用于我的upgrade()函数。 - checkmate

8

This:

+ KEY_ID2 + " integer autoincrement, " 

应该是这样的:

+ KEY_ID2 + " integer primary key autoincrement, " 

如果您遵循CREATE TABLE的语法图,您将看到autoincrement应该仅在primary key之后。

enter image description here enter image description here

如果您想使_id2成为外键,则根本不需要自动增量。


我按照你说的做了,然后出现了以下错误:09-29 14:16:58.580: ERROR/AndroidRuntime(464): java.lang.RuntimeException: Unable to start activity ComponentInfo{sep.com/sep.com.SepActivity}: android.database.sqlite.SQLiteException: no such column: Content2: , while compiling: SELECT _id, Content1, Content2, Content3 FROM MY_TABLE - checkmate
@LoshWick:Content2列在MY_TABLE2而不是MY_TABLE - mu is too short
私有静态最终字符串SCRIPT_CREATE_DATABASE = "create table " + MYDATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " + KEY_CONTENT1 + " text not null);";私有静态最终字符串SCRIPT_CREATE_DATABASE2 = "create table " + MYDATABASE_TABLE2 + " (" + KEY_ID2 + " integer primary key autoincrement, " + KEY_CONTENT2 + " text not null, " + KEY_CONTENT3 + " text not null, " + " FOREIGN KEY ("+KEY_ID2+") REFERENCES "+MYDATABASE_TABLE+" ("+KEY_ID+"));"; - checkmate

4
一条回答说“SQLite中没有autoincrement关键字”,这是不正确的。实际上有一个叫做“autoincrement”的关键字,我在主键中使用过它。
例如:
   static final String CREATE_LOCATION_TABLE="CREATE TABLE Location"+
            "( LocationID INTEGER PRIMARY KEY AUTOINCREMENT," +
            " RestuarantID int not null," +
            " Latitude float not null," +
            " Longitude float not null)";

同时,当您向此表中输入数据时,请在主键位置使用关键字“null”。

例如:

   static final String INSERT_LOCATION= "Insert into Location values" +
            "(null,1002,6.905369,79.851514)";

2

三件事情:

  • 从语句中移除尾随的 ;。
  • 在自增列上添加“主键”约束。
  • 从PK列中删除自增 - 这将自动发生。

0
sqlite> CREATE TABLE app (a INTEGER PRIMARY KEY NOT NULL, B VARCHAR);
sqlite> insert into app (b) values ('');
sqlite> insert into app (b) values ('a');
sqlite> 
sqlite> insert into app (b) values ('b');
sqlite> insert into app (b) values ('c');
sqlite> insert into app (b) values ('d');
sqlite> select * from app;
1|
2|a
3|b
4|c
5|d
sqlite> exit;

注意:在SQLite中没有AUTOINCREMENT关键字。因此,您只需使用INTEGER PRIMARY KEY NOT NULL即可。它将自动插入属性的递增值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接