2016-06-10 227 views
0

我想從sqlite數據庫中檢索數據,但是當我調用nameData()logcat顯示異常時: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 我不明白爲什麼,有任何線索?從sqlite數據庫檢索數據?

過程:

public class SearchContactByName2 extends AppCompatActivity { 
    String dbString="",dbString2=""; 
     SQLiteDatabase db; 
     protected void onCreate(Bundle savedInstanceState){ 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.searchcontactbynamelayout2_main); 

      TextView textView=(TextView)findViewById(R.id.textViewShowName); 
      TextView textView2=(TextView)findViewById(R.id.textView2ShowNumber); 

      SearchContactByName objOfSearchContactByName=new SearchContactByName(); 

      ContactDatabase onbOfContactDatabase=new ContactDatabase(getBaseContext()); 

      Cursor allcontact2= onbOfContactDatabase.nameData(objOfSearchContactByName.getNameForSearchTypeString); 


      allcontact2.moveToFirst(); 

      do{ 
       dbString+=allcontact2.getString(allcontact2.getColumnIndex("name")); 
       dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone")); 
       dbString+="\n"; 
       dbString2+="\n"; 
       textView.setText(dbString); 
       textView2.setText(dbString2); 
      }while(allcontact2.moveToNext()); 
      Toast.makeText(getBaseContext(), "data", Toast.LENGTH_LONG).show(); 

     } 
    } 

數據庫部分:

public class ContactDatabase extends SQLiteOpenHelper { 
    SQLiteDatabase db; 
    public static final String DATABASE_NAME="totalContact.db"; 
    public static final String TABLE_NAME="mecontact"; 
    public static final String NAME="name"; 
    public static final String PHONE="phone"; 

    public ContactDatabase(Context context) { 
     super(context, DATABASE_NAME, null, 1); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     try { 
      db.execSQL("create table mecontact" + 
        "(id integer primary key autoincrement, name text, phone text)"); 
     }catch(android.database.SQLException e){ 
       System.out.println("table create nhi ho rha"); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS mecontact"); 
     onCreate(db); 
    } 

    public void insertContact(String nam,String mob){ 

     db=this.getWritableDatabase(); 
     ContentValues contentValues=new ContentValues(); 

     contentValues.put(NAME,nam); 
     contentValues.put(PHONE,mob); 

     db.insert(TABLE_NAME, null, contentValues); 
     db.close(); 
    } 

    public Cursor showData(){ 

     db=this.getWritableDatabase(); 
     Cursor res = db.rawQuery("SELECT * FROM mecontact", null); 
     return res; 

    } 

    public Cursor nameData(String dataName){ 

     db=this.getWritableDatabase(); 
     Cursor res = db.rawQuery("SELECT * FROM mecontact WHERE name = '"+dataName+"'", null); 
     return res; 

    } 
} 
+0

先用這個鏈接https://stackoverflow.com/questions/24901617/insert-values-in-to-sqlite-database-java-lang-nullpointerexception/24901693#24901693比較你的代碼,然後你會發現你錯了的地方 – DJhon

+0

一旦看到這個,你會得到一個想法http://androidcoding.in/2016/03/22/android-sqlite-tutorial-on-inserting-deleting-values-into-database/ – Abhishek

回答

0

試試下面的代碼

if(allcontact2.moveToFirst()){ 

     do{ 
      dbString+=allcontact2.getString(allcontact2.getColumnIndex("name")); 
      dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone")); 
      dbString+="\n"; 
      dbString2+="\n"; 
      textView.setText(dbString); 
      textView2.setText(dbString2); 
     }while(allcontact2.moveToNext());} 
     Toast.makeText(getBaseContext(), " no data", Toast.LENGTH_LONG).show(); 

其實你的數據庫沒有數據

+0

爲什麼光標不檢索任何數據 –

+0

是你正在發生.. jst檢查是數據名稱是der還是不 – Sush

0
Cursor allcontact2= onbOfContactDatabase.nameData(objOfSearchContactByName.getNameForSearchTypeString); 

if(allcontact2.size() > 0){ 
while(allcontact2.moveToNext()){ 
    dbString+=allcontact2.getString(allcontact2.getColumnIndex("name")); 
    dbString2+=allcontact2.getString(allcontact2.getColumnIndex("phone")); 
    dbString+="\n"; 
    dbString2+="\n"; 
    textView.setText(dbString); 
    textView2.setText(dbString2); 
    } 
    Toast.makeText(getBaseContext(), "data", Toast.LENGTH_LONG).show(); 
} 
0

隨着由sush變化nameData方法的答案,

public Cursor nameData(String dataName){ 
    db=this.getReadableDatabase(); 
    //String dataname might contain special characters like a quote 
    //retreive the cursor this way 
    Cursor res=db.query("mecontact",new String[]{columnsYouwantToSelect},"name =?",new String[]{dataName},null,null,null); 
    //if you want to select all the columns in the table replace 
    //the second parameter with null 
    return res; 
} 
0

沒有真正回答你的問題,但更多的諮詢如何防止今後麻煩。我建議你改變你的oncreate代碼,並像在其他人那樣聲明你的ID值在頂端。這將確保數據庫被正確創建,並且將來如果發生更改,您可以輕鬆獲取值而不會出現輸入錯誤。這樣的代碼比純查詢更安全。

db.execSQL("CREATE TABLE " 
      + TABLE_NAME 
      + " (" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
      + NAME + " TEXT," 
      + PHONE + " TEXT);"); 
相關問題