簡體   English   中英

檢查行是否存在這種實現sqlite db

[英]check if row exist with this kind of implementing sqlite db

如果行存在,如何檢查sqlite數據庫? 我實現sqlite db的風格是這樣的

public class EmployeeDBController extends SQLiteOpenHelper {

    public EmployeeDBController(Context applicationcontext) {
        super(applicationcontext, "registeruser.db", null, 1);

    }

    public  SQLiteDatabase db;
    //Creates Table
    @Override
    public void onCreate(SQLiteDatabase database) {
        String query;
        query = "CREATE TABLE register ( userId INTEGER PRIMARY KEY, Name text, Age text, Gender text," +
                "HomeAddress text, Password text, QRID text, udpateStatus TEXT)";
        database.execSQL(query);
    }
    @Override
    public void onUpgrade(SQLiteDatabase database, int version_old, int current_version) {
        String query;
        query = "DROP TABLE IF EXISTS register";
        database.execSQL(query);
        onCreate(database);
    }
}

例如:我想檢查在sqlite數據庫中是否有“名稱”和“約翰”。

有一個幫助程序函數 ,可以避免被游標弄亂:

public boolean rowExists(String table, String column, String value) {
    long count = DatabaseUtils.queryNumEntries(db,
            table, column+" = ?", new String[]{ value });
    return count > 0;
}

使用這種方法!

public boolean isRowExist(/*YOUR_INPUTS*/){
    try {
        db = mDBHelper.getReadableDatabase();
        String selectQuery =  YOUR_SEARCH_QUERY_STATMENT
         //You should use YOUR_INPUTS to create a selectQuery that can select the row/rows for you

        Cursor c = db.rawQuery(selectQuery, null);
        if(c.getCount()>=1){
            c.close();
            return true;
        }
        c.close();
        return false;
    } catch (Exception e) {
        return false;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM