更新 SQLite - “WHERE”附近:语法错误(代码 1 SQLITE_ERROR):
Posted
技术标签:
【中文标题】更新 SQLite - “WHERE”附近:语法错误(代码 1 SQLITE_ERROR):【英文标题】:Updating SQLite - near "WHERE": syntax error (code 1 SQLITE_ERROR): 【发布时间】:2021-06-09 14:16:33 【问题描述】:我最近在我的 SQLite 数据库中添加了一个列,因为添加此列是我拥有的一个按钮,该按钮将数据库列之一的值设置为“1”或“0”,现在当该按钮开始使我的应用程序崩溃时尝试使用updateData(),
,所以我认为这就是我的问题所在,在updateData()
的代码或语法中
错误:
android.database.sqlite.SQLiteException:靠近“WHERE”:语法错误 (代码 1 SQLITE_ERROR):,编译时:UPDATE my_manager SET location_name = 'Blarney Stone' , location_county = '软木',location_description = 'jj',location_route = '1',location_position = '2',location_longg = 'null',location_lat = 'null',location_url = 'JJ',location_url2 = 'jj',location_url3 = 'jj', WHERE location_id = '1' 在 android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机 方法)
代码:
void updateData(String id, String row_id, String name, String county, String description, String
in_route, String position, String lat, String longg, String url, String url2, String url3)
System.out.println(TABLE_NAME+"; "+row_id +"; "+ name +"; "+ county+"; "+ description+"; " + lat
+"; "+ longg+"; "+ url +"; "+ url2 +"; "+ url3 +"; ");
SQLiteDatabase db = this.getReadableDatabase();
db.execSQL("UPDATE " + "my_manager" + " SET location_name = "+"'"+ name + "' " + ", " +
"location_county = " + "'"+ county + "'"+ "," +
"location_description = " + "'"+ description + "'" + "," +
"location_route = " + "'"+ in_route + "'" + "," +
"location_position = " + "'"+ position + "'" + "," +
"location_longg = " + "'"+ longg + "'" + "," +
"location_lat = " + "'"+ lat + "'" + "," +
"location_url = " + "'"+ url + "'" + "," +
"location_url2 = " + "'"+ url2 + "'" + "," +
"location_url3 = " + "'"+ url3 + "'" + "," + " WHERE location_id = "+"'"+ row_id+"'");
【问题讨论】:
去掉 WHERE 前的,
。
正如@forpas 所说,应该是:"location_url3 = " + "'"+ url3 + "' WHERE
不要使用字符串粘贴;使用参数化语句。
删除“,”,干杯,男士们
【参考方案1】:
对不起,我的朋友不要认为这是一件坏事,但如果可能的话,你如何避免不必要的“+”。
void updateData("UPDATE my_manager SET location_name = '"+ name + "', " +
"location_county = '"+ county + "', " +
"location_description = '"+ description + "'," +
"location_route = '"+ in_route + "', " +
"location_position = '" + position + "', " +
"location_longg = '" + longg + "'," +
"location_lat = '" + lat + "'," +
"location_url = '" + url + "', " +
"location_url2 = '"+ url2 + "', " +
"location_url3 = '"+ url3 + "' WHERE location_id = '"+ row_id+"'");
【讨论】:
以上是关于更新 SQLite - “WHERE”附近:语法错误(代码 1 SQLITE_ERROR):的主要内容,如果未能解决你的问题,请参考以下文章
org.sqlite.SQLiteException:[SQLITE_ERROR] SQL 错误或缺少数据库(“as”附近:语法错误)
android.database.sqlite.SQLiteException:在“Foreign”附近:语法错误(代码 1):
“COLUMN_NAME”附近:SQLite 中的语法错误(代码 1)