2012-12-24 17 views
1

下面是一個代碼/方法找到,如果一些名稱存在於表或不存在的名字..我的Android SQLite數據庫應用靠攏,當我搜索不表中的

Contact getContact(String name) { 
SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, 
      KEY_NAME, KEY_PH_NO }, KEY_NAME + "=?", 
      new String[] { String.valueOf(name) }, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), 
      cursor.getString(1), cursor.getString(2)); 

    db.close(); 
    cursor.close(); 
    // return contact 
    return contact; 
} 

我已經有一個函數來獲取arrayList中的所有名字。我可以在調用上述函數來解決我的問題之前調用它。但我想問的是有沒有其他的(直)的方式來做到這一點

+0

哪裏是logcat的? ..請提供給我們看看發生了什麼。 – kdehairy

回答

0
if (cursor == null) 
Toast.makeText(getApplicationContext(), "No Records Exist", Toast.LENGTH_SHORT).show(); 
3

當你調用cursor.moveToFirst()它將返回true是否有一個有效的結果出現,或者在沒有找到結果的情況下false。如果cursor.moveToFirst()返回false,則調用getXXX()方法中的任何一個都將失敗。

嘗試這樣:

if(cursor.moveToFirst()) 
{ 
    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), 
     cursor.getString(1), cursor.getString(2)); 
} 
cursor.close(); 

注意,CursorSQLiteDatabase.query返回保證是非空

+0

我見過'query'返回空遊標,儘管我無法弄清楚它的確切情況。然而,對於'moveToFirst'來說,+1返回false,這就是答案。 –

0
SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, 
      KEY_NAME, KEY_PH_NO }, KEY_NAME + "=?", 
      new String[] { String.valueOf(name) }, null, null, null, null); 
      if(cursor.moveToFirst()){ 
     do { 
      Double lat = cursor.getDouble(2); 
      Double lon = cursor.getDouble(1); 
      } while (trackCursor.moveToNext()); 
} 
0

這裏是讓所有的聯繫人從表列表中的代碼...

/** 
* Getting all the contacts in the database 
*/ 
public List<Contact> getAllContacts() { 
    List<Contact> contactList = new ArrayList<Contact>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Contact contact = new Contact(); 
      contact.setID(Integer.parseInt(cursor.getString(0))); 
      contact.setName(cursor.getString(1)); 
      contact.setPhoneNumber(cursor.getString(2)); 
      // Adding contact to list 
      contactList.add(contact); 
     } while (cursor.moveToNext()); 
    } 

    cursor.close(); 
    db.close(); 

    // return contact list 
    return contactList; 
} 

則在返回「真」另一個函數調用,如果名字出現在臺「假」,否則......

/** 
    * checks if name already present in the database 
    * @param name 
    * @return 
    */ 
    public boolean checkDbData(String name){ 
     List<Contact> contactList =getAllContacts(); 
     boolean checkName = false ; 

     for(Contact cn: contactList){ 
      String dbName = cn.getName(); 
      if(name.equals(dbName)){ 
       checkName = true ; 
      }    
     } 

     return checkName; 
    } 

如果這個函數返回「真」,那麼上面給出的函數被調用,以獲得contacti.e。

Contact getContact(String name) { 
SQLiteDatabase db = this.getReadableDatabase(); 

Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, 
     KEY_NAME, KEY_PH_NO }, KEY_NAME + "=?", 
     new String[] { String.valueOf(name) }, null, null, null, null); 
if (cursor != null) 
    cursor.moveToFirst(); 

Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), 
     cursor.getString(1), cursor.getString(2)); 

db.close(); 
cursor.close(); 
// return contact 
return contact; 

}

注意:我們還可以從contactList這種接觸,既可以用來獲得(在這種情況下,即「名」)所需的聯繫人

相關問題