0

i am having one table with four columns but i want to add some data in category column when application opens. I tried to insert data directly by using insert query but i found this is not the correct way to put initial data.

chinu
  • 37
  • 6
  • 1
    can you please post what you have tried – pkgrover Jan 30 '17 at 06:54
  • db.execSQL("INSERT INTO Category VALUES(' ','Shopping',' ',')"; db.execSQL("INSERT INTO Category VALUES(' ','Food',' ',')"; db.execSQL("INSERT INTO Category VALUES(' ','Travel',' ',')"; – chinu Jan 30 '17 at 12:49
  • Sorry for late reply..i was stuck somewhere – chinu Jan 30 '17 at 12:50
  • This is not even correct syntex for insertion a record in table,please refer this link and check for exception in insertion https://www.tutorialspoint.com/sqlite/sqlite_insert_query.htm – pkgrover Jan 30 '17 at 12:54

1 Answers1

0

Have you checked out the 2nd answer at:

initialize a sqlite database android

? What Suragch does there is create an array in an xml resource, and then retrieves that array in the onCreate method of SQLiteOpenHelper. Then they insert it into the database using ContentValues.put and database.insert.

Community
  • 1
  • 1
  • it is not working or i am doing wrong .Please check my code – chinu Jan 31 '17 at 11:11
  • public static final String CATEGORY_SQL = "CREATE TABLE "+CATEGORY+" ("+ID+" varchar, "+TITLE+" varchar, "+DESCRIPTION+" varchar, "+TYPE+" varchar)"; – chinu Jan 31 '17 at 11:12
  • ContentValues values = new ContentValues(); Resources res = mContext.getResources(); String[] myArray = res.getStringArray(R.array.my_array); for (String item : myArray) { values.put(CategoryModelDAO.ID, DEFAULT_FIRST_COLUMN); values.put(CategoryModelDAO.TITLE, item); values.put(CategoryModelDAO.DESCRIPTION, DEFAULT_THIRD_COLUMN); values.put(CategoryModelDAO.TYPE, DEFAULT_FOURTH_COLUMN); db.insert(CategoryModelDAO.CATEGORY, null, values); – chinu Jan 31 '17 at 11:15
  • this is array created in resources Clothing Eating Out Entertainment Food General – chinu Jan 31 '17 at 11:23