我想将ArrayList插入到sqlite中,并且在anggota列中具有相同id的情况下没有相同的值,如下所示:
这是我的插入代码:
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()
);
这是我的插入代码:
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");
}
}
当我想要保存时,出现了错误,logcat 的报错如下:
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)
我不知道我的问题出在哪里,希望有人能理解我的问题并帮助我解决它。