3

I want to insert Arraylist into my sqlite without same value in anggota coloumn that have same id as below :

helper=new DBSpaj(getActivity());
    for (int i=0; i<ListKeluargaPP.size();i++)  {
    helper.insertTableMarital(
            a,
            ListKeluargaPP.get(i).getAnggota_pp(),
            ListKeluargaPP.get(i).getNama_Anggota_pp(),
            ListKeluargaPP.get(i).getTtl_Anggota_pp()

);

This is my insert code :

public void insertTableMarital(
        String master_id,
        String anggota,
        String nama,
        String ttl
        ) 
{
String selectQuery = "SELECT master_id,anggota FROM table_marital " +
        "where master_id='"+Menu_SPPAJ.getX()+"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.equals(null)){
    ContentValues cv=new ContentValues();
    cv.put("master_id",  master_id);
    cv.put("anggota", anggota);
    cv.put("nama", nama);
    cv.put("ttl", ttl);
    getWritableDatabase().insert("table_marital",null, cv);
}
    if (cursor.getString(1)!=PemegangPolis.getStrAnggota()){
    ContentValues cv=new ContentValues();
    cv.put("master_id",  master_id);
    cv.put("anggota", anggota);
    cv.put("nama", nama);
    cv.put("ttl", ttl);
    getWritableDatabase().insert("table_marital",null, cv);
    }else {
        System.out.println("udah ada");
    }
        }   

I got errors when I want to save it, and the logcat said as below :

    02-04 10:15:31.274: E/AndroidRuntime(17174): FATAL EXCEPTION: main
02-04 10:15:31.274: E/AndroidRuntime(17174): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at id.co.ajsmsig.espaj.DBSpaj.insertTableMarital(DBSpaj.java:912)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at id.co.ajsmsig.espaj.PemegangPolis.onSave(PemegangPolis.java:1574)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at id.co.ajsmsig.espaj.Menu_SPPAJ$SlideMenuClickListener.onClick(Menu_SPPAJ.java:243)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.view.View.performClick(View.java:4222)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.view.View$PerformClick.run(View.java:17337)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.os.Handler.handleCallback(Handler.java:615)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.os.Looper.loop(Looper.java:137)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at android.app.ActivityThread.main(ActivityThread.java:4895)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at java.lang.reflect.Method.invokeNative(Native Method)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at java.lang.reflect.Method.invoke(Method.java:511)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
02-04 10:15:31.274: E/AndroidRuntime(17174):    at dalvik.system.NativeStart.main(Native Method)

I have no idea where is my fault, I hope someone understand my problem and can help me to solve my problem.

3
  • why would you wish to combine an insert and a select ?? Cant you create a function that checks if the cursor is empty, is yes,then insert else fetch data. Or else, insert the row, close the cursor, and call another function that performs the fetch Commented Feb 4, 2014 at 4:31
  • @Rat-a-tat-a-tatRatatouille because i want to insert without same value in anggota coloumn that have same id Commented Feb 4, 2014 at 6:46
  • in that case, try putting the second if inside an else. Commented Feb 4, 2014 at 7:03

2 Answers 2

2

I have solve my problem, put all cursor.getString(1) in List and then check it one by one with this code :

public void insertTableMarital(
        String master_id,
        String anggota,
        String nama,
        String ttl
        ) 
{
    List<String> test= new ArrayList<String>();
String selectQuery = "SELECT master_id,anggota FROM table_marital " +
        "where master_id='"+Menu_SPPAJ.getX()+"'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if(cursor.getCount()>0){
    cursor.moveToPosition(cursor.getCount() - 1);
    if (cursor.moveToFirst()) {
        do {
            test.add(cursor.getString(1));
        } while (cursor.moveToNext());
    } 
        Log.i("TES LAIN", PemegangPolis.getStrAnggota());
    if (!(test.contains(PemegangPolis.getStrAnggota()))){
    ContentValues cv=new ContentValues();
    cv.put("master_id",  master_id);
    cv.put("anggota", anggota);
    cv.put("nama", nama);
    cv.put("ttl", ttl);
    getWritableDatabase().insert("table_marital",null, cv);
    }
    else {
        System.out.println("udah ada");
    }

        }   
else {
    ContentValues cv=new ContentValues();
    cv.put("master_id",  master_id);
    cv.put("anggota", anggota);
    cv.put("nama", nama);
    cv.put("ttl", ttl);
    getWritableDatabase().insert("table_marital",null, cv);
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

You need to call cursor.moveToFirst() before you are able to use it, at the moment it is pointing to before the first (if any) entry

1 Comment

but the size is 0 so the cursor is null it won't move first (no first to begin with)

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.