我的数据库有两个表.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
请帮帮我。谢谢你提前:)
答案 0 :(得分:0)