0

I am coding a simple DB application, but when I load the application it says: "The app has stopped unexpectedly"

Here is my code for the Database:

public class EventsData extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "events.db";
    private static final int DATABASE_VERSION = 1;

    public EventsData(Context ctx) {
        super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                 + TIME + " INTEGER, " + TITLE + " TEXT NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

my constants:

import android.provider.BaseColumns;;

public interface Constants extends BaseColumns {
    public static final String TABLE_NAME = "events";
    //columns in the events db
    public static final String TIME = "time";
    public static final String TITLE = "title"; 
}

The error is:

04-14 17:08:12.486: E/Database(277): Failure 1 (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY) on 0x217058 when preparing 'CREATE TABLE events(_idINTEGER PRIMARY KEY AUTOINCREMENT, timeINTEGER, title TEXT NOT NULL);'.

Why is there an error?

maximus
  • 11,264
  • 30
  • 93
  • 124
  • my error is: 04-14 17:08:12.486: E/Database(277): Failure 1 (AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY) on 0x217058 when preparing 'CREATE TABLE events(_idINTEGER PRIMARY KEY AUTOINCREMENT, timeINTEGER, title TEXT NOT NULL);'. – maximus Apr 14 '12 at 17:24
  • Where do you define your fields TABLE_NAME, _ID, .... Could you provide the error in your logcat? – Simone Casagranda Apr 14 '12 at 17:27
  • @user1248720 Check the error you've pasted is exactly the same to the one you're getting – Mosty Mostacho Apr 14 '12 at 17:38

9 Answers9

0

You don't have put blank space between your fields and types. _idINTEGER PRIMARY KEY.... must have a blank between _id and INTEGER.

Simone Casagranda
  • 1,217
  • 17
  • 26
0

You should check for spaces between the field names and their types. The error says:

[...] events(_idINTEGER PRIMARY KEY AUTOINCREMENT, timeINTEGER, title TEXT NOT NULL)

However, your code looks fine. Are you sure that is the actual code?

Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
0

I think the issue is related to SQLLITE synatx.Please check this link.

Android table creation Failure (near "autoincrement": syntax error)?.

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66
0

You eill need to add spaces in your sqlite code. If u see the error CREATE TABLE events(_idINTEGER PRIMARY KEY AUTOINCREMENT, timeINTEGER, title TEXT NOT NULL);'

_idINTEGER should be separate. Add the extra spaces

Akhil
  • 13,888
  • 7
  • 35
  • 39
0

Try using this ...

String sql =   db.execSQL("create table " + TABLE_NAME + "( " + BaseColumns._ID + " integer primary key autoincrement, "
             + TIME + " integer, " + TITLE + " text not null);");

db.execSQL(sql);
Nishant Rajput
  • 2,053
  • 16
  • 28
0

Just rename your _ID to _id it will surely work. Rest of your code is looking fine. one more thing you are declaring TIME as String and setting its Type to Integer, kindly watch this too.

dIvYaNsH sInGh
  • 1,943
  • 3
  • 21
  • 40
0

use firefox browser to check your queries. Firefox has a component (or pluggin) to create/edit tables, rows ....

Alex Muni
  • 473
  • 5
  • 16
0

According to the SQlite website (http://sqlite.org/faq.html#q1) you don't have to use autoincrement to add it to the column. I don't remember the code I used to create a database and if i wasn't using a tablet right now I would have checked it.

se_bastiaan
  • 1,716
  • 14
  • 19
0

dude change INTEGER PRIMARY KEY AUTOINCREMENT to INTEGER PRIMARY KEY thats all it will work fine and does also autoincrement

swaru
  • 1