0
  public ArrayList<String> getCitiesFromCountry(int countryCode){

     ArrayList<String> cityNames = new ArrayList<String>(); 

     Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + COUNTRY_TABLE +
     " LEFT JOIN " + CITY_TABLE + " ON " + COUNTRY_TABLE + "." + _ID + " = " +
     CITY_TABLE + "." +  _ID2 + " WHERE " + COUNTRY_TABLE + "." + _ID + " = ?",
     new String{"1"});

     if (cursor != null){

        while(cursor.moveToNext()){
        cityNames.add(cursor.getString(cursor.getColumnIndex(CITY_NAME)));                   
     }
  }
  return cityNames;
  }

数据库常量

  public static final String WORLD_DATABASE = "world_database";
  public static final String COUNTRY_TABLE = "country_table";
  public static final String CITY_TABLE = "city_table";
  public static final int DATABASE_VERSION = 1;
  public static final String _ID = "_id";
  public static final String _ID2 = "_id2";
  public static final String COUNTRY_NAME = "country_name";
  public static final String CITY_NAME = "city_name";

编辑:由于 SQL 语句中的语法编译错误,我无法运行它,所以这就是没有 logcat 堆栈跟踪的原因。

4

1 回答 1

4
Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + COUNTRY_TABLE +
     " LEFT JOIN " + CITY_TABLE + " ON " + COUNTRY_TABLE + "." + _ID + " = " +
     CITY_TABLE + "." +  _ID2 + " WHERE " + COUNTRY_TABLE + "." + _ID + " = ?",
     new String[]{"1"});

rawquery 第二个参数是一个数组

rawQuery(String sql, String[] selectionArgs)
于 2013-06-21T06:06:51.027 回答