这是我的DataBase.java。但是,当在MainActivity中查询时,LogCat说"没有这样的列:Event_id"
static final String tableName="EventList";
static final String colID="Event_id";
static final String colTitle="Title";
static final String colDetails="Details";
static final String colYear="YY";
static final String colMonth="MM";
static final String colDay="DD";
static final String colHour="HH";
static final String colMinute="TT";
public DateBase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createDB= "create table "+tableName+"("+colID+"integer primary key , "+colTitle+" text not null, "+colDetails+" text, "+colYear+" int, "+colMonth+" int, "+colDay+" int, "+colHour+" int, "+colMinute+" int)";
db.execSQL(createDB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableSQL = "DROP TABLE IF EXISTS " + tableName + " ";
db.execSQL(dropTableSQL);
onCreate(db);
}
public void execSQL(String sql, Object[] args) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL(sql, args);
}
public Cursor query(String sql, String[] args) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(sql, args);
return cursor;
}
}
LogCat报告:
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gabelyu.myapplication/com.example.gabelyu.myapplication.MainActivity}: android.database.sqlite.SQLiteException: no such column: Event_id (code 1): , while compiling: SELECT Event_id as _id, Title, Details, YY, MM, DD, HH, TT FROM EventList
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:154)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: no such column: Event_id (code 1): , while compiling: SELECT Event_id as _id, Title, Details, YY, MM, DD, HH, TT FROM EventList
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1090)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:663)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1420)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1267)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1138)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1306)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.example.gabelyu.myapplication.MainActivity.onCreate(MainActivity.java:40)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5255)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.qihoo360.mobilesafe.loader.c.callActivityOnCreate(SourceFile:98)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.access$700(ActivityThread.java:154)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5306)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-12 13:25:22.895 6410-6410/com.example.gabelyu.myapplication E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
是否意味着无法选择主键?或Android只是监督列?感谢。
答案 0 :(得分:2)
你忘了在某些地方放置正确的空格,这就是它创建错误表格的原因。这是正确的字符串代码:
String createDB = "create table "+tableName+" ("+colID+" integer primary key , "+colTitle+" text not null, "+colDetails+" text, "+colYear+" int, "+colMonth+" int, "+colDay+" int, "+colHour+" int, "+colMinute+" int)";
编辑:我格式化了你的sql查询字符串,让我知道它是否有效。您还可以编辑帖子并包含用于从数据库中获取项目的sql命令吗?
String createDB = "CREATE TABLE " + tableName + "(" + colID + " INTEGER PRIMARY KEY," + colTitle + " TEXT not null," + colDetails + " TEXT," + colYear + " INTEGER," + colMonth + " INTEGER," + colDay + " INTEGER," + colHour + " INTEGER," + colMinute + " INTEGER)";
答案 1 :(得分:0)
您发布的链接是相关的。列名和列类型之间没有空格
您的代码部分:
tableName+"("+colID+"integer
正确的代码:
tableName+"("+colID+" integer