当我搜索表中不存在的名称时,我的android sqlite数据库应用程序会关闭

时间:2012-12-24 17:39:08

标签: android sqlite 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中的所有名称。我可以在调用上面的函数之前调用它来解决我的问题。但是我想问一下有没有其他(直接的)方法可以做到这一点

4 个答案:

答案 0 :(得分: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();

请注意,Cursor返回的SQLiteDatabase.query保证非空

答案 1 :(得分:0)

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

答案 2 :(得分: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());
}

答案 3 :(得分: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;
}

然后在另一个函数中调用它,如果表“false”中存在name,则返回“true”,否则......

    /**
     * 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;
    }

如果此函数返回“true”,则调用上面给定的函数来获取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获取此联系人,两者都可用于获取所需的联系人(即本例中的“姓名”)