1

这是查找表中是否存在某个名称的代码/方法。

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

4 回答 4

3

当你调用它时,如果那里有有效的结果,或者没有找到结果,cursor.moveToFirst()它将返回。如果返回,则调用任何方法都将失败。truefalsecursor.moveToFirst()falsegetXXX()

尝试这样的事情:

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

请注意,Cursor返回的 fromSQLiteDatabase.query保证为非 null

于 2012-12-24T17:43:02.363 回答
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());
}
于 2012-12-24T18:13:43.527 回答
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”中,则返回“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”,则调用上述给定函数以获取联系人。

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中获取这个联系人,两者都可以用来获取需要的联系人(即本例中的“姓名”)

于 2012-12-25T19:03:08.753 回答
0
if (cursor == null)
Toast.makeText(getApplicationContext(), "No Records Exist", Toast.LENGTH_SHORT).show();
于 2012-12-24T17:42:23.483 回答