为什么rawQuery()返回零行?

时间:2017-02-06 04:47:35

标签: android sql android-cursor sqliteopenhelper

我的数据库有两个表.District和Place.I我正在Place表上运行查询。

private void test(){
    Cursor cur = new DatabaseQuery().getPlaceDetailsForPhonebook(DatabaseHelper.getInstance(this).getMyWritableDatabase(),"hospital","District");
    Log.e("myapp","row = "+cur.getCount());
}

这是getPlaceDetailsForPhonebook方法

public Cursor getPlaceDetailsForPhonebook(SQLiteDatabase db,String type,String DIS_NAME){
    String sql;
    if (DIS_NAME.equals("District")) {
        sql = "SELECT name,phonenumber FROM Place WHERE type = '" + type + "'";
    }
    else {
        sql = "SELECT name,phonenumber FROM Place WHERE districtname = '"+DIS_NAME+"' AND type = '"+type+"'";
    }
    Log.e("myapp",sql);
    Cursor cr = null;
    try {
        cr = db.rawQuery(sql, null);
        Log.e("myapp","inside method row = "+cr.getCount());
    }
    catch(Exception e){
        Log.e("myapp",e.getLocalizedMessage());
    }
    return cr;

}

我的DatabaseHelper

public class DatabaseHelper extends SQLiteOpenHelper {

private static SQLiteDatabase myWritableDb;
private static DatabaseHelper sInstance;

private DatabaseHelper(Context context){
    super(context,"district.db",null,1);
}

public static synchronized DatabaseHelper getInstance(Context context) {

    if (sInstance == null) {
        sInstance = new DatabaseHelper(context.getApplicationContext());
    }
    return sInstance;
}

public SQLiteDatabase getMyWritableDatabase() {
    if ((myWritableDb == null) || (!myWritableDb.isOpen())) {
        myWritableDb = this.getWritableDatabase();
    }

    return myWritableDb;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}}

在区域表上运行的所有查询都返回正确的结果。我正在使用SqliteBrower,并且我在Place表上执行了相同的查询(test()方法内的查询),它给了我正确的结果。所以数据库没有任何问题。 我的logcat

02-06 10:09:43.393 11938-11938/com.example.helpfinder E/myapp: SELECT name,phonenumber FROM Place WHERE type = 'hospital'

02-06 10:09:43.393 11938-11938 / com.example.helpfinder E / myapp:inside method row = 0

02-06 10:09:43.393 11938-11938 / com.example.helpfinder E / myapp:row = 0

请帮帮我。谢谢你提前:)

1 个答案:

答案 0 :(得分:0)

傻傻的我。我只需要从我的设备中清除以前的数据库,问题就会立即解决。我必须看看是否有办法以编程方式删除它。