簡體   English   中英

Android SQLite中LEFT JOIN語句的SQL語法錯誤

[英]error in SQL syntax for LEFT JOIN statement in Android SQLite

  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 stacktrace的原因。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM