Twing90
Twing90

Reputation: 417

How to fix "Unable to start activity" error with Android and SQLite

I have this errors:

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)

This is the code.
I want to open the database and then check if it's empty or not.
If the database is empty, I want to populate the first 8 rows.

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();

and this are the method to open writable the database:

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

I don't know why doesn't it work.
I'm a beginner in Android programming.

Upvotes: 0

Views: 363

Answers (2)

Quintin B
Quintin B

Reputation: 5881

Try the following:

"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);"

Basically, you shouldn't have spaces in column names, and you should use TEXT for String column types and INTEGER or INT for int column types. [https://www.sqlite.org/datatype3.html]

You can use the following website to validate your SQL statements: http://sqlfiddle.com/

I agree with @FrankNStein that you should avoid spaces in column names, but you also had SQLite syntax errors.

Upvotes: 2

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Your problem is... spaces in column names.
You can have spaces in your field names (if you REALLY want them), but only if you escape them.
i.e.:

[tempo della presa precedente] not null

Or simply get rid of spaces and use (for instance) underscores, instead:
i.e.:

tempo_della_presa_precedente not null

Let me suggest you that spaces aren't a great idea for column names.

Upvotes: 2

Related Questions