应用程序崩溃打开数据库

时间:2012-12-26 19:55:11

标签: android database sqlite

当我尝试打开我的数据库时,应用程序崩溃了。我发布了下面的代码,但是我在同一个应用程序中以相同的方式实现了另一个数据库,我对此没有任何问题。

public class projectdatabase {

public static final String KEY_ROWID = "_id";
public static final String PROJECT_NAME = "project_name";
public static final String PROJECT_ID = "project_id";
public static final String PROJECT_DIFFICULTY = "project_difficulty";
public static final String PROJECT_STATUS = "project_status";
public static final String PROJECT_START_DATE = "project_start_date";
public static final String PROJECT_FINISH_DATE = "project_finsish_date";

private static final String DATABASE_NAME = "project_db";
private static final String DATABASE_TABLE = "project_details";
private static final int DATABASE_VERSION = 1;

private DbHelper ourHelper;
private final Context projectdbContext;
private SQLiteDatabase projectDatabase;

private static class DbHelper extends SQLiteOpenHelper {
    public DbHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL("CREATE TABLE "+ DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER AUTOINCREMENT," +
                PROJECT_NAME + " TEXT NOT NULL," +
                PROJECT_ID + " INTEGER PRIMARY KEY," +
                PROJECT_START_DATE + " TEXT," +
                PROJECT_FINISH_DATE + " TEXT," +
                PROJECT_DIFFICULTY + " TEXT, " +
                PROJECT_STATUS + " TEXT);"
               ); 

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

        db.execSQL("DROP TABLE IF EXIST " + DATABASE_NAME);
        onCreate(db);

    }
}

public projectdatabase (Context c) {                            
     projectdbContext = c;
}

public projectdatabase open() {                                                 //Open database
    ourHelper = new DbHelper(projectdbContext);
    projectDatabase = ourHelper.getWritableDatabase();
    return this;
}

这是Logcat: (我用粗体制作了那个部分,我认为突出了主要问题)

12-27 01:22:21.893: E/AndroidRuntime(2214): FATAL EXCEPTION: main
12-27 01:22:21.893: E/AndroidRuntime(2214): java.lang.IllegalStateException: Could not execute method of the activity
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.view.View$1.onClick(View.java:3044)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.view.View.performClick(View.java:3511)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.view.View$PerformClick.run(View.java:14105)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.os.Handler.handleCallback(Handler.java:605)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.os.Looper.loop(Looper.java:137)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.app.ActivityThread.main(ActivityThread.java:4424)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at java.lang.reflect.Method.invoke(Method.java:511)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at dalvik.system.NativeStart.main(Native Method)
12-27 01:22:21.893: E/AndroidRuntime(2214): Caused by: java.lang.reflect.InvocationTargetException
12-27 01:22:21.893: E/AndroidRuntime(2214):     at java.lang.reflect.Method.invokeNative(Native Method)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at java.lang.reflect.Method.invoke(Method.java:511)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.view.View$1.onClick(View.java:3039)
12-27 01:22:21.893: E/AndroidRuntime(2214):     ... 11 more
**12-27 01:22:21.893: E/AndroidRuntime(2214): Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error: , while compiling: CREATE TABLE project_details (_id INTEGER AUTOINCREMENT,project_name TEXT NOT NULL,project_id INTEGER PRIMARY KEY,project_start_date TEXT,project_finsish_date TEXT,project_difficulty TEXT, project_status TEXT);**
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at com.kk.project.projectdatabase$DbHelper.onCreate(projectdatabase.java:38)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at com.kk.project.projectdatabase.open(projectdatabase.java:66)
12-27 01:22:21.893: E/AndroidRuntime(2214):     at com.kk.project.Addproject.submit_project(Addproject.java:112)
12-27 01:22:21.893: E/AndroidRuntime(2214):     ... 14 more

1 个答案:

答案 0 :(得分:3)

您不能在不属于AUTOINCREMENT的列上使用PRIMARY KEY

但是,您可以使用嵌套的SELECT语句或创建自己的触发器来执行相同的操作。请阅读:SQLite auto-increment non-primary key field