gpt4 book ai didi

Android sqlite如何检查记录是否存在

转载 作者:IT王子 更新时间:2023-10-28 23:51:02 28 4
gpt4 key购买 nike

我想检查一条记录是否存在。

这是我尝试过的:

MainActivity.class

    public void onTextChanged(CharSequence s, int start, int before, int count) {

System.out.println("Ontext changed " + new String(s.toString()));
strDocumentFrom = s.toString();

if(s.toString().isEmpty()){

} else {

try{
strTransactionDate = dbHelper.getTransactionDateByDocumentNumber(strDocumentFrom);
//strTotalAmount = dbHelper.getTotalAmountByDocumentNumber(strDocumentFrom);
//strVan = dbHelper.getVanByDocumentNumber(strDocumentFrom);


//etTransactionDate.setText(strTransactionDate);
//etTotalAmount.setText(strTotalAmount);
//Log.d("Van", "" + strVan);
//etVan.setText(strVan);

} catch (SQLiteException e) {
e.printStackTrace();
Toast.makeText(ReceivingStocksHeader.this,
"Document number does not exist.", Toast.LENGTH_SHORT).show();
}

}

DBHelper.class

            // TODO DISPLAYING RECORDS TO TRANSRCVHEADER
public String getTransactionDateByDocumentNumber(String strDocumentNumber){
String[] columns = new String[]{KEY_TRANSACTIONDATE};

Cursor c = myDataBase.query(TBL_INTRANS,
columns, null,
null, null, null, null, null);

if(c != null){
c.moveToFirst();
String date = c.getString(0);
return date;
} else {
Log.d("Error", "No record exists");
}


return null;
}

但它并没有让它到 catch block 来显示 toast。

我在这里做错了什么?

最佳答案

public static boolean CheckIsDataAlreadyInDBorNot(String TableName,
String dbfield, String fieldValue) {
SQLiteDatabase sqldb = EGLifeStyleApplication.sqLiteDatabase;
String Query = "Select * from " + TableName + " where " + dbfield + " = " + fieldValue;
Cursor cursor = sqldb.rawQuery(Query, null);
if(cursor.getCount() <= 0){
cursor.close();
return false;
}
cursor.close();
return true;
}

我希望这对你有用...如果 db 中已经存在记录,则此函数返回 true。否则返回 false。

关于Android sqlite如何检查记录是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20415309/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com