1

I want to insert records into my sqlite database only when the database is created (i.e. only when my app installs for the first time on a device). I googled for help and some people suggested to insert the data in the onCreate() method of the SQLiteOpenHelper, but records couldn't be inserted using that method.

This is my code

public class EventsData extends SQLiteOpenHelper {

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

    // Table name
    public static final String TABLE = "events";

    // Columns
    public static final String NAME = "first";
    public static final String TITLE = "second";

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

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE "+TABLE+"(" + _ID + "INTEGER PRMARY KEY AUTOINCREMENT, " + NAME +"TEXT, " + TITLE + "TEXT NOT NULL);");
        db.execSQL("INSERT INTO " + TABLE +" VALUES (1,'david','packard')");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                    db.execSQL("DROP TABLE IF EXISTS " + TABLE);
                    onCreate(db);
    }
}
Bono
  • 4,757
  • 6
  • 48
  • 77
rampireram
  • 481
  • 2
  • 7
  • 16
  • The simple solution is to use prepopulated database,HOW? check this http://stackoverflow.com/questions/9109438/using-already-created-database-with-android/9109728#9109728 – Yaqub Ahmad Jun 16 '12 at 15:19
  • no... i could add records from the main activity, but couldnt do the same from "SQLiteOpenHelper" class – rampireram Jun 16 '12 at 17:25

3 Answers3

1

use sqlite database browser to make datbase it will help a lot try this link Inserting data to record in sqlite

Community
  • 1
  • 1
user2326414
  • 27
  • 1
  • 6
0

Looks in you CREATE TABLE query spaces are missing between texts....

NAME +"TEXT, " + TITLE + "TEXT NOT NULL);"

Just print the queries in SOP and try to execute them in you SQLITe tools on desktop.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • no... i could add records from the main activity, but couldnt do the same from "SQLiteOpenHelper" class :-( even after correcting the errors – rampireram Jun 16 '12 at 17:26
0

It will work fine when you get the CREATE statement right. It's missing spaces and correct spellings. _ID + "INTEGER... must be _ID + " INTEGERandPRMARYmust bePRIMARY`. There may be other mistakes. It's a good idea to have a debugging option in your program to emit all SQL you're executing to the log.

Gene
  • 46,253
  • 4
  • 58
  • 96