Android的源码如何检查是否存在记录 [英] Android sqlite how to check if a record exists

查看:138
本文介绍了Android的源码如何检查是否存在记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查记录是否存在。

下面是我已经试过:

MainActivity.class

 公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){

        的System.out.println(Ontext改为+新的String(s.toString()));
        strDocumentFrom = s.toString();

        如果(s.toString()。的isEmpty()){

        } 其他 {

             尝试{
                 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);

             }赶上(SQLiteException E){
                 e.printStackTrace();
                 Toast.makeText(ReceivingStocksHeader.this,
                         证件号码不存在,Toast.LENGTH_SHORT).show();
             }

        }
 

DBHelper.class

  // TODO中显示记录TO TRANSRCVHEADER
        公共字符串getTransactionDateByDocumentNumber(字符串strDocumentNumber){
            的String []列=新的String [] {KEY_TRANSACTIONDATE};

            光标C = myDataBase.query(TBL_INTRANS,
                    列,空,
                    NULL,NULL,NULL,NULL,NULL);

            如果(C!= NULL){
                c.moveToFirst();
                串日期= c.getString(0);
                返回日期;
            } 其他 {
                Log.d(错误,没有记录存在);
            }


            返回null;
        }
 

但它没有得到它的catch块显示敬酒。

我在做什么错在这里?

解决方案

 公共静态布尔CheckIsDataAlreadyInDBorNot(字符串表名,
        字符串dbfield,字符串fieldValue方法){
    SQLiteDatabase SQLDB = EGLifeStyleApplication.sqLiteDatabase;
    查询字符串= +表名+,其中+ dbfield +=+ fieldValue方法选择从*;
    光标光标= sqldb.rawQuery(查询,NULL);
        如果(cursor.getCount()&其中; = 0){
            cursor.close();
            返回false;
        }
    cursor.close();
    返回true;
}
 

我希望这是对你有用... 如果记录已经存在于数据库这个函数返回true。否则返回false。

I would like to check whether a record exists or not.

Here is what I've tried:

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

But it doesn't get it to the catch block to display the toast.

What am I doing wrong in here?

解决方案

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

I hope this is useful to you... This function returns true if record already exists in db. Otherwise returns false.

这篇关于Android的源码如何检查是否存在记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆