android游标对于select查询不返回任何行

sshcrbum  于 2021-07-09  发布在  Java
关注(0)|答案(5)|浏览(317)

我正在尝试从我自己的数据库中获取一行,该数据库位于assets文件夹中。通过使用sqlite查询浏览器和调试器的select语句检查表中是否有行,我在数据库中得到了一行。不管怎样,从代码中调用语句只会得到一个空游标。我还添加了方法
cursor.movetofirst()
避免打电话
mydatabase.close();
下面是我尝试在textview上附加单个条目的方法:

private void getAdresses(Location location) { // TODO
    double lat = location.getLatitude();
    double lng = location.getLongitude();
    Geocoder geocoder = new Geocoder(this, Locale.GERMAN);

    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        if (addresses.size() != 0) {
            Address address = addresses.get(0);

            String addressString = address.getAddressLine(0).toString();
            System.out.println("(geocoder) " + addressString);
            textView.append(addressString + "\n");

            Cursor cursor = mDbHelper.getAllEntrys("tblBar", "Adresse",
                    addressString);

            if (cursor.moveToFirst()) {

                String cursorContent = cursor.getString(cursor
                        .getColumnIndexOrThrow("Name"));
                textView.append(cursorContent + "\n");

            } else {
                System.out.println("No bar at current location");
            }
        } else {
            System.out.println("(geocoder) no address!");
        }
    } catch (IOException e) {
        System.out.println(e.toString() + "(geocoder)");
    }
}

此外,这是我调用数据库实际查询的方法:

public Cursor getAllEntrys(String table, String column, String search) {
    try {
        String sSelect = "SELECT * FROM " + table + " WHERE " + column
                + " = '" + search + "'";
        Cursor cursor = mDb.rawQuery(sSelect, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    } catch (SQLException mSQLException) {
        System.out.println("getData >>" + mSQLException.toString());
        throw mSQLException;
    }
}

希望有人能帮我。非常感谢!

hyrbngr7

hyrbngr71#

好的,伙计们,首先我要感谢你们所有人的快速React。坚持下去!虽然答案很明显,但我自己想出来了。教程中的问题来自:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ 也就是说,它没有考虑在第一次运行程序之后手动向数据库添加值(在sqlite数据库浏览器中更新)的情况。我就是这么做的。。。因此,您必须编写自己的方法来检查自上次运行以来是否有任何更改。希望任何人都能需要这个:)

j2cgzkjk

j2cgzkjk2#

//这对我有用

final Cursor c = sampleDB.rawQuery("SELECT FirstName, LastName,RedId FROM " +
                SAMPLE_TABLE_NAME +
                " where RedId =  '"+a+"'", null);
        int count = c.getCount();

        if(count == 0){

            Toast.makeText(RetrieveData.this, "Not Found", Toast.LENGTH_SHORT).show();
        }
a2mppw5e

a2mppw5e3#

尝试从数据库中获取单行。
在数据库类中声明此方法。

public Cursor selectSingleRecord(String tableName, String[] tableColumns,
        String whereClause, String whereArgs[], String groupBy,
        String having, String orderBy) {
    return myDataBase.query(tableName, tableColumns, whereClause, whereArgs,
            groupBy, having, orderBy);  

}

并使用此方法获取光标。
如果我的表结构在下面。
_身份证|姓名|地址
我将用下面的方式写

String tableColumns[] = {"_id"}; // Here mansion the column which you want to retrieve I think in your case "addressString".
    String whereClause = {"Name"}; // Here use where clause

    Cursor cursor = dbHelper.selectSingleRecord("Your_Table_Name", tableColumns, whereClase +"=?" , new String[] {String.valueOf("NameOfPerson")}, null, null, null);

     // NameOfperson is the name which I am going to enter through EditText.
      if (cursor.moveToFirst())
        {            
              String getId = cursor.getString[0];  // Here you can get the id of respective person (i.e. the name which I have entered )                          
         }
        cursor.close();  // Finally close the curser

希望这对你有帮助。

neskvpey

neskvpey4#

将您的选择改为:有时在sql中,使用like'something'比使用=something更好。
string sselect=“select*from”+表+“其中”+列+“类似于”'+搜索+“'”;

vtwuwzda

vtwuwzda5#

不知道这是不是问题,但是。

String sSelect = "SELECT * FROM " + table + " WHERE " + column
                + " = '" + search + "'";

当搜索变量为null时,查询将转换为:
从tblbar中选择*其中adresse='null'
请尝试使用此选项:

search = (search == null) ? " IS NULL " : " = '" + search +" '" ;

String sSelect = "SELECT * FROM " + table + " WHERE " + column + search;

相关问题