Somk
Somk

Reputation: 12047

SQLITE Query forces application close

I have checked everything and the creating of the database is fine its when I try to add data to it that it forces the app to close. I cant see why.

    db.execSQL("CREATE TABLE IF NOT EXISTS " +
            SAMPLE_TABLE_NAME2    
            + " (GenID INT, "
            + " Nxtq INT);");

    db.execSQL("INSERT INTO " +
            SAMPLE_TABLE_NAME2 +
            " Values (1,1);");

The error I get output is

Failure 1 (table general has 4 columns but 2 values were supplied) when prepearing 'INSERT INTO general values (1,1)

There is some stuff before these two query's here it is in its entirety.

    db =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

    db.execSQL("CREATE TABLE IF NOT EXISTS " +
            SAMPLE_TABLE_NAME +
            " (ID INT(3), Questions VARCHAR, Answer1 VARCHAR," +
            " Answer2 VARCHAR, Answer3 VARCHAR," +
            " Answer4 VARCHAR, CorrectAnswer INT(1), Reason VARCHAR);");

    db.execSQL("CREATE TABLE IF NOT EXISTS " +
            SAMPLE_TABLE_NAME2    
            + " (GenID INT, "
            + " Nxtq INT);");

    db.execSQL("INSERT INTO " +
            SAMPLE_TABLE_NAME2 +
            " Values (1,1);");

Upvotes: 1

Views: 476

Answers (5)

Seph
Seph

Reputation: 8693

try changing the name of "general" to something else like "general_new" and see if the script works, as mentioned, general the table might already exist (even if you don't think you made it yourself).

Upvotes: 2

dbm
dbm

Reputation: 10485

What's the respective values of SAMPLE_TABLE_NAME and SAMPLE_TABLE_NAME2? Is it possible that they are equal like so:

private static final String SAMPLE_TABLE_NAME = "general";
private static final String SAMPLE_TABLE_NAME2 = "general";

Upvotes: 0

martin clayton
martin clayton

Reputation: 78115

The table in question - named general - already exists before the above code is executed. This prior-existing table has four columns, hence the INSERT INTO with two columns fails. The CREATE TABLE does nothing because IF NOT EXISTS is false.

You mention that the database is created correctly - does your application start with an empty database, or might it re-use an existing one? Alternatively, perhaps there is some code that is called earlier that creates a table named general with a 4-column scheme. This clashes with the above.

Upvotes: 0

orionk
orionk

Reputation: 1

@Max for the insert try using db.insert(........);

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006704

Use adb logcat, DDMS, or the DDMS perspective in Eclipse to examine LogCat and look at the stack trace associated with your exception. Do that before posting here, and include the relevant bits of LogCat output if you do not understand them.

In this case, I suspect that you will find that INT(3) is not a valid SQLite datatype, but that is just a guess.

Upvotes: 0

Related Questions