本文介绍了Android的表创建失败(近"自动增量":语法错误)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
公共静态最后弦乐MYDATABASE_NAME =MY_DATABASE;
公共静态最后弦乐MYDATABASE_TABLE =MY_TABLE;
公共静态最后弦乐MYDATABASE_TABLE2 =MY_TABLE2;
公共静态最终诠释MYDATABASE_VERSION = 1;
公共静态最后弦乐KEY_ID =_id;
公共静态最后弦乐KEY_ID2 =_id2;
公共静态最后弦乐KEY_CONTENT1 =内容1;
公共静态最后弦乐KEY_CONTENT2 =内容2;
公共静态最后弦乐KEY_CONTENT3 =Content3;
//创建表MY_DATABASE(ID整数主键,内容文字NOT NULL);
私有静态最后弦乐SCRIPT_CREATE_DATABASE =创建表+ MYDATABASE_TABLE +(
+ KEY_ID +整数主键自动增量,
+ KEY_CONTENT1 +文字NOT NULL);;
私有静态最后弦乐SCRIPT_CREATE_DATABASE2 =创建表+ MYDATABASE_TABLE2 +(
+ KEY_ID2 +整数自动递增,
+ KEY_CONTENT2 +文字不为空,
+ KEY_CONTENT3 +文字不为空,
+外键(+ KEY_ID2 +)参考+ MYDATABASE_TABLE +(+ KEY_ID +));;
我无法找出什么提供了以下错误,请帮我谢谢你。
解决方案
在短:在SQLite的声明为INTEGER PRIMARY KEY将自动增量列
没有自动增量
的关键字,这就是为什么你得到一个错误。
您可以了解更多关于 SQLite的常见问题解答。
编辑:只是写整数主键
这就够了。 的SQLite
将自动增加你的 IDS
。
EDIT2:您 onUpgrade()
方法应该是这样的:
公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
如果(动态网页> oldVersion){
Log.w(MyAppTag+ oldVersion +到从版本更新数据库
+动态网页+.Existing数据都将丢失。);
db.execSQL(DROP TABLE IF EXISTS+ MY_TABLE);
db.execSQL(DROP TABLE IF EXISTS+ MY_TABLE2);
的onCreate(DB);
}
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+"));";
I can not find out what gives the following error, please help me out thank you.
解决方案
In short: In SQLite a column declared INTEGER PRIMARY KEY will autoincrement.
There is no autoincrement
keyword in SQLite, that is why you are getting an error.
You can find out more on SQLite FAQ.
EDIT: just writing integer primary key
it is enough. SQLite
will automatically increment your ids
.
EDIT2: Your onUpgrade()
method should look like this :
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的表创建失败(近"自动增量":语法错误)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!