如何解决"无法开始活动" Android和SQLite出错

时间:2015-08-06 16:38:55

标签: android android-studio

我有这个错误:

Process: com.fusinato.lorenzo.colorglass, PID: 2257
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fusinato.lorenzo.colorglass/com.fusinato.lorenzo.colorglass.Main}: android.database.sqlite.SQLiteException: unrecognized token: "' prec int not null,tempo della presa precedente int not null);" (code 1): , while compiling: create table gran (_id integer primary key autoincrement, principale string,secondario string not null,media precedente int not null,quantita' prec int not null,tempo della presa precedente int not null);
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: android.database.sqlite.SQLiteException: unrecognized token: "' prec int not null,tempo della presa precedente int not null);" (code 1): , while compiling: create table gran (_id integer primary key autoincrement, principale string,secondario string not null,media precedente int not null,quantita' prec int not null,tempo della presa precedente int not null);
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
        at com.fusinato.lorenzo.colorglass.Main$GRANdb$SQLiteHelper.onCreate(Main.java:273)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at com.fusinato.lorenzo.colorglass.Main$GRANdb.openToWrite(Main.java:226)
        at com.fusinato.lorenzo.colorglass.Main.onCreate(Main.java:53)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这是代码 我想打开数据库,然后检查它是否为空。
如果数据库为空,我想填充前8行。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    Granidb.openToWrite();
    System.out.println("queueALL= " + Granidb.queueAll());
    if (Granidb.queueAll().getCount() != 0) {
        System.out.println("queueALL partito if= " + Granidb.queueAll().getCount());
        //creagranigliatori();
    }

    Granidb.close();

这是打开可写数据库的方法:

public GRANdb openToWrite() throws android.database.SQLException {
        sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
        sqLiteDatabase = sqLiteHelper.getWritableDatabase();
        return this;
    }

我不知道为什么它不起作用。
我是Android编程的初学者。

2 个答案:

答案 0 :(得分:2)

您的问题是... spaces列名 您可以在字段名称中添加空格(如果您真的想要它们),但前提是escape它们。 即:

[tempo della presa precedente] not null

或者简单地删除空格并使用(例如)下划线,而不是:
即:

tempo_della_presa_precedente not null

我建议你,列名不是空格。

答案 1 :(得分:2)

尝试以下方法:

"create table gran (" +
    "_id integer primary key autoincrement, " +
    "principale text, " +
    "secondario text not null, " +
    "media_precedente integer not null, " +
    "quantita_prec integer not null, " +
    "tempo_della_presa_precedente integer not null);"

基本上,您不应该在列名中包含空格,并且应该使用TEXT作为String列类型,使用INTEGER或INT作为int列类型。 [https://www.sqlite.org/datatype3.html]

您可以使用以下网站验证您的SQL语句:http://sqlfiddle.com/

我同意@FrankNStein你应该避免列名中的空格,但你也有SQLite语法错误。