11

I need to add initial values to SQLite database, once application is started first time. How should I do it?

Nikhil
  • 16,194
  • 20
  • 64
  • 81
LA_
  • 19,823
  • 58
  • 172
  • 308

6 Answers6

15

In DBAdapter.java class file you can do that.


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_1);
        db.execSQL(CREATE_TABLE_2);
        db.execSQL(CREATE_TABLE_3);
        db.execSQL("insert into " + DATABASE_TABLE3 + "(" + KEY_ROWID + ","
                + KEY_DEF + ") values(1,'')");
    }

Like this type.

Nikhil
  • 16,194
  • 20
  • 64
  • 81
  • Is it work only for the first run of the application or every time is running? – Soheil Setayeshi Jul 01 '13 at 07:47
  • @Setayeshi, from developer.android.com "Called when the database is created for the first time. This is where the creation of tables and the initial population of the tables should happen." – bora.oren Jul 01 '15 at 14:43
2

One time or first-load requirements like this are common.

1) Set a flag variable named FIRSTLOAD and set it to True. Be sure to save this variable to isolated storage in the device to read back each time the application is started. 2) Create a method which checks the value of FIRSTLOAD and only executes if FIRSTLOAD is true. Place your code which 'add[s] initial values to SQLite database' here. Then set the FIRSTLOAD variable to false. This way it will only execute the code once!

Boolean FIRSTLOAD= new Boolean("true");

void SetInitialValues()
{
   if(!FIRSTLOAD)
      return;

   // insert your code to run only when application is started first time here


   FIRSTLOAD = False;
}
dev4life
  • 1,150
  • 10
  • 13
2

You can create your own SQLite database, put in assets folder and access it as shown in the answer of this link: adding your own SQLite database to an android application

Community
  • 1
  • 1
Jaydeep Khamar
  • 5,975
  • 3
  • 32
  • 30
2

You should use SQLiteOpenHelper for this

    private static class OpenHelper extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "rss.sqlite";
    private static final int DATABASE_VERSION = 1;

    OpenHelper(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE RSS (TITLE TEXT, LINK TEXT, DESCR TEXT, PUBDATE DATE, GUID TEXT, READ TEXT, TYPE TEXT)");
        db.execSQL("CREATE TABLE PAGE (LINK TEXT, CONTENT TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

And use it in code

    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
sidslog
  • 654
  • 1
  • 6
  • 15
1

In case you are using org.droidparts library OR-Mapper for accessing your database you can do it like this.

In your implementation of

public class CategoryEntityMgr extends EntityManager<Category> {
    public CategoryEntityMgr(Context ctx) {
        super(Category.class, ctx);
    }

    // NEW --> this one must be used in onCreateTables or you will receive a database locked exception
    private CategoryEntityMgr(Context ctx, SQLiteDatabase db) {
        super(Category.class, ctx, db);
    }
}

In your implementation of AbstractDbOpenHelper:

protected void onCreateTables(SQLiteDatabase db) {
    // create your tables as usual
    createTables(db, Booking.class, Category.class);

    // Use a new instance of your entity manager
    //    but use the 2nd constructor to provide the db connection
    CategoryEntityMgr mgr = new CategoryEntityMgr(ctx, db);

    // add the new table row / entity you an to have
    Category c = new Category();
    c.name = "the value for this column";
    mgr.createOrUpdate(c);
}
  • It would be better if this were a generic answer, not tied to a specific library implementation. – Levon May 22 '17 at 20:15
0

You should check the number of objects inside your database each time you open the database for the first time in your app. Don't forget that user can free all data.

    //Open DataBase
    MyDb.Open();

    //Get All List
    MyCursor = MyDb.getAllNames(); //You should have a function to get all the table data

    //First Time we open Database, add default Values
    if ( MyCursor.getCount() < 1 )
    {
        AddDefaultValuesToDataBase();
    }

From Here you can continue to get the values again.

Hope it helps, BR, Adrian.

azelez
  • 2,501
  • 2
  • 27
  • 26