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);
    }
}
2
  • The simple solution is to use prepopulated database,HOW? check this stackoverflow.com/questions/9109438/… Commented Jun 16, 2012 at 15:19
  • no... i could add records from the main activity, but couldnt do the same from "SQLiteOpenHelper" class Commented Jun 16, 2012 at 17:25

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

1 Comment

no... i could add records from the main activity, but couldnt do the same from "SQLiteOpenHelper" class :-( even after correcting the errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.