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+"));";
Run Code Online (Sandbox Code Playgroud)
我无法找出出现以下错误,请帮帮我谢谢.
09-29 13:41:19.760:ERROR /数据库(334):在准备'create table MY_TABLE2时,在0x218df0上失败1(接近"自动递增":语法错误)(_id2整数自动递增,Content2文本不为null,Content3文本不为null, FOREIGN KEY(_id2)REFERENCES MY_TABLE(_id));'.
09-29 13:41:19.770:DEBUG/AndroidRuntime(334):关闭VM
09-29 13:41:19.770:WARN/dalvikvm(334):threadid = 1:线程退出未捕获异常(组= 0x4001d800)
09-29 13:41:19.791:ERROR/AndroidRuntime(334):致命异常:主要
09-29 13:41:19.791:ERROR/AndroidRuntime(334):java.lang.RuntimeException:无法启动活动ComponentInfo {sep.com/sep.com.SepActivity}:android.database.sqlite.SQLiteException:near"autoincrement ":语法错误:创建表MY_TABLE2(_id2整数自动增量,Content2文本不为空,Content3文本不为空,FOREIGN KEY(_id2)REFERENCES MY_TABLE(_id));
Ovi*_*tcu 37
简而言之:SQLite中In SQLite a column declared INTEGER PRIMARY KEY will autoincrement.没有autoincrement关键字,这就是您收到错误的原因.
您可以在SQLite FAQ上找到更多信息.
编辑:只是写integer primary key它就足够了.SQLite会自动递增你的ids.
编辑2:您的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);
}
Run Code Online (Sandbox Code Playgroud)
这个:
+ KEY_ID2 + " integer autoincrement, "
Run Code Online (Sandbox Code Playgroud)
应该这样:
+ KEY_ID2 + " integer primary key autoincrement, "
Run Code Online (Sandbox Code Playgroud)
如果您按照语法图表进行操作,CREATE TABLE您将会看到autoincrement只有之后才会出现primary key.

如果你想_id2成为一个外键,那么你根本不希望它是自动增量.
| 归档时间: |
|
| 查看次数: |
21998 次 |
| 最近记录: |