0

I am new to android. I haven't worked with SQLite DBs before.
I think this is a very basic question, but I am unable to find the solution.

The code is here (assume declarations)

public void onCreate(SQLiteDatabase db) {

    String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + FORMULA + "("
            + CAT_CD + " TEXT ," + S_CAT_CD + " TEXT, PRIMARY 
 KEY(CAT_CD,S_CAT_CD))";
    db.execSQL(CREATE_CATEGORIES_TABLE);

    String CREATE_CAT_DESC_TABLE="CREATE TABLE "+ FORMULA_CAT_DESC + "
("+CAT_CD+" TEXT PRIMARY KEY, "+ DESC +" TEXT ) ";
    db.execSQL(CREATE_CAT_DESC_TABLE);

    String CREATE_CURRENCY_TABLE="CREATE TABLE "+ VI_CURRENCY + "("+ 
CURRENCY_CD +" TEXT PRIMARY KEY, "+ CURRENCY_SIGN +" TEXT ) ";
    db.execSQL(CREATE_CURRENCY_TABLE);

   String query=  "INSERT INTO "+ VI_CURRENCY +" ("+CURRENCY_CD +", 
"+CURRENCY_SIGN+ ") VALUES " +
            "('INR', '₹'), " +
            " ('USD','$') " +
            "('JPY','¥') ";
    db.execSQL(query);
 }

The first three commands execute successfully, while by executing the insert command, SQLite throws an exeption.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vinod wakade
  • 139
  • 1
  • 1
  • 8
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Jun 05 '17 at 17:21
  • 1
    You are trying to insert multiple rows by running a single SQL command. – Phantômaxx Jun 05 '17 at 17:26

3 Answers3

1

You could create a sql statement for each record

String query1 =  "INSERT INTO " + VI_CURRENCY + " (" + CURRENCY_CD + ", " + CURRENCY_SIGN + ") VALUES " + "('INR', '₹')" ;

String query2 =  "INSERT INTO " + VI_CURRENCY + " (" + CURRENCY_CD + ", " + CURRENCY_SIGN + ") VALUES " + "('USD','$')" ;

String query3 =  "INSERT INTO " + VI_CURRENCY + " (" + CURRENCY_CD + ", " + CURRENCY_SIGN + ") VALUES " + "('JPY','¥')" ;

db.execSQL(query1);
db.execSQL(query2);
db.execSQL(query3);

Or insert multiple values in one statement, with the correct sintax it would be like this:

String query = "INSERT INTO " + VI_CURRENCY + " (" + CURRENCY_CD + ", " + CURRENCY_SIGN + ") VALUES " +
             "('INR', '₹'), " +
             "('USD','$'), " +
             "('JPY','¥')";
db.execSQL(query);

For more information visit SqlLite

fdelafuente
  • 1,114
  • 1
  • 13
  • 24
1

In Android, writing operation is slow, So in your case, if you have hundreds of data, you can use transactions in SQLite database.

db.beginTransaction();
try {
    for(/*your loop*/) {
        String query =  /*your query*/

        db.execSQL(query);
    }
    db.setTransactionSuccessful();
} finally {
    db.endTransaction();
}

For more detail, Please refer this, official doc.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
0

If you want to insert multiple records, then you should take data in hashmap object in key value pair. Use iterator on the same and insert data in table.

        Map<String,String> myMap = new HashMap<>();
        myMap.put("INR","₹");
        myMap.put("USD","$");
        myMap.put("JPY","¥");


 Iterator it = myMap.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry pair = (Map.Entry)it.next();
      String query1 =  "INSERT INTO " + VI_CURRENCY + " (" + CURRENCY_CD + ", " + CURRENCY_SIGN + ") VALUES " + "("+ pair.getKey()+ "," + pair.getvalue()+)" ;
    db.execSQL(query1);
    it.remove(); // avoids a ConcurrentModificationException
}
Ct9
  • 150
  • 1
  • 11