尝试插入TABLE_EVENTS时出错,但插入/查询TABLE_CONTACTS没有问题。这是我的创建字符串:
private static final String DATABASE_CREATE =
"create table "+ TABLE_CONTACTS + "( " + COLUMN_CONTACTS_LOOKUP
+ " text primary key, " + COLUMN_CONTACTS_ADDED
+ " real not null);" +
" create table "+ TABLE_EVENTS +"( "+ COLUMN_ID +" integer primary key autoincrement, "+
COLUMN_EVENTS_TITLE + " text not null, "+COLUMN_EVENTS_START_TIME+" real not null, "+
COLUMN_EVENTS_END_TIME+ " real, "+COLUMN_EVENTS_TYPE+" integer not null, "+COLUMN_EVENTS_START_LONG+" integer, "+
COLUMN_EVENTS_START_LAT+" integer);";
用
调用public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
我的插页是
public long createEvent(String title, long start, long end, int type, int long_, int lat){
ContentValues initialValues = new ContentValues();
initialValues.put(COLUMN_EVENTS_TITLE, title);
initialValues.put(COLUMN_EVENTS_START_TIME, start);
initialValues.put(COLUMN_EVENTS_END_TIME, end);
initialValues.put(COLUMN_EVENTS_TYPE, type);
initialValues.put(COLUMN_EVENTS_START_LAT, lat);
initialValues.put(COLUMN_EVENTS_START_LONG, long_);
return mDb.insert(TABLE_EVENTS, null, initialValues);
}
答案 0 :(得分:2)
这是因为SQLiteDatabase
类只允许CREATE
字符串中的一个DATABASE_CREATE
语句。如果要创建多个表,诀窍是准备多个创建字符串,然后在onCreate
方法中逐个调用它们:
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE_CONTACTS_TABLE);
db.execSQL(DATABASE_CREATE_EVENTS_TABLE);
}
您收到错误是因为您的事件表从未创建过。
答案 1 :(得分:2)
SQLite只允许随时使用一个SQL命令。您需要将create语句拆分为单个字符串并逐个执行。
private static final String CREATE_TABLE_1 =
"create table "+ TABLE_CONTACTS + "( " + COLUMN_CONTACTS_LOOKUP
+ " text primary key, " + COLUMN_CONTACTS_ADDED
+ " real not null);";
private static final String CREATE_TABLE_2 =
"create table "+ TABLE_EVENTS +"( "+ COLUMN_ID +" integer primary key autoincrement, "+
COLUMN_EVENTS_TITLE + " text not null, "+COLUMN_EVENTS_START_TIME+" real not null, "+
COLUMN_EVENTS_END_TIME+ " real, "+COLUMN_EVENTS_TYPE+" integer not null, "+COLUMN_EVENTS_START_LONG+" integer, "+
COLUMN_EVENTS_START_LAT+" integer);";
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_1);
db.execSQL(CREATE_TABLE_2);
}
答案 2 :(得分:1)
我遇到了同样的问题。男孩很头疼。
为每个新表创建一个字符串,并在每个表上调用db.execSQL(query)
!使用该似乎是一些问题;分隔符。