yue1liang1chuan 2013-06-21 08:42 采纳率: 0%
浏览 943

androdi SQLite的SQL语法错误

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语法编译错误。

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-01 12:13
    关注

    这个方法看起来是用来在一个数据库中查询某个国家的城市的名称的。


    错误的原因可能是 SQL 语法的一些问题。这里有一些可能的问题:

    • new String{"1"} 应改为 new String[]{"1"}。
    • LEFT JOIN 应改为 INNER JOIN。
    • WHERE 后应该指定国家代码的列名,而不是 _ID。

    修改后的代码可能是这样的:

    Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM " + COUNTRY_TABLE +
     " INNER JOIN " + CITY_TABLE + " ON " + COUNTRY_TABLE + "." + _ID + " = " +
     CITY_TABLE + "." +  _ID2 + " WHERE " + COUNTRY_TABLE + "." + COUNTRY_CODE + " = ?",
     new String[]{String.valueOf(countryCode)});
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试