Search code examples
androidsoapinsertandroid-sqlite

Android SQLiteStatement Insert, nothing happen


i have some data from SoapObject, i want insert to sqlite, for better performance, i use the following code :

public void testInsert(String sql, SoapObject rs, int index) {
        try {
            sql = "INSERT INTO NSPMasterHarga (KdBarang, Wilayah, HargaJual1, HargaJual2) VALUES (?, ?, ?, ?)";
            theDatabase = getWritableDatabase();
            theDatabase.beginTransaction();

            String drop = "DROP TABLE IF EXISTS NSPMasterHarga";
            SQLiteStatement stmtDrop = theDatabase.compileStatement(drop);
            stmtDrop.execute();

            String create = "CREATE TABLE NSPMasterHarga (KdBarang TEXT PRIMARY KEY, Wilayah TEXT, HargaJual1 TEXT, HargaJual2 TEXT)";
            SQLiteStatement stmtCreate = theDatabase.compileStatement(create);
            stmtCreate.execute();

            SQLiteStatement stmt = theDatabase.compileStatement(sql);

            int count = rs.getPropertyCount();

            for (int i = 0; i < count; i++) {
                SoapObject row = (SoapObject) rs.getProperty(i);

                for (int j = 1; j <= index; j++) {
                    stmt.bindString(j, row.getProperty(j - 1).toString().replace("anyType{}", ""));
                }

                long entryID = stmt.executeInsert();
                stmt.clearBindings();
            }
        /*for (int i = 0; i < NUMBER_OF_ROWS; i++) {
            //generate some values

            stmt.bindString(1, randomName);
            stmt.bindString(2, randomDescription);
            stmt.bindDouble(3, randomPrice);
            stmt.bindLong(4, randomNumber);

            long entryID = stmt.executeInsert();
            stmt.clearBindings();
        }*/

            theDatabase.setTransactionSuccessful();
            theDatabase.endTransaction();

            theDatabase.close();
        }
        catch (Exception ex)
        {
            String err = ex.getMessage();
        }
    }

When debug, i've got nothing error, but the data not insert to my sqlite. Any idea or clue ?

Thanks


Solution

  • for better performance

    I'm not so sure which part of the code you are referring to. Opening and closing the database after each interaction is terrible for performance. The SQLiteOpenHelper takes care of all this, so you don't need to do anything manually.

    Try the following alternative to insert an entry:

    public boolean addEntry(){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
    
        values.put("column1", "value1");  // make sure the type corresponds to your sql column type
        values.put("column2", "value2");
        values.put("column3", "value3");
        values.put("column4Int", 1);
    
        long newRowId = db.insert(TABLE_NAME, null, values);
        Log.d("DBHelper", "Added row " + newRowId + " to DB.");
    
        return newRowId != -1;  // -1 means it failed
    }