android.database.sqlite.SQLiteException:靠近“ORDER":语法错误(代码 1):, [英] android.database.sqlite.SQLiteException: near "ORDER": syntax error (code 1): ,

查看:38
本文介绍了android.database.sqlite.SQLiteException:靠近“ORDER":语法错误(代码 1):,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到这个错误.我确定这是一个简单的语法错误.有人看到吗?我已经调试了大约 30 分钟,但似乎找不到它.

I keep getting this error. I'm sure it's a simple syntax error. Does anyone see it? I been debugging for ~30min and can't seem to find it.

查询

DELETE FROM SuccessfulCalls ORDER BY id DESC LIMIT 2

要插入数据库的实际代码.

Actual code to insert to db.

SQLiteHelper sqLiteHelper     = ApplicationClass.getSqLiteHelper();
SQLiteDatabase db             = sqLiteHelper.getWritableDatabase();
String query = "DELETE FROM " + sqLiteHelper.SUCCESSFULCALLSTABLE + " ORDER BY " + sqLiteHelper.SUCCESSFULID + " DESC LIMIT " + Constants.NUMBEROFCALLSTODELETE ;
db.rawQuery(query, null);
db.close();

更新我试过这个查询,但由于某种原因它没有从我的数据库中删除行.

UPDATE I have tried this query, but for some reason it does not delete the rows from my database.

DELETE FROM SuccessfulCalls WHERE id IN ( SELECT id FROM SuccessfulCalls ORDER BY id DESC LIMIT 5 );

但我知道查询正在执行,因为当我尝试时.它删除了我的整个表.从成功调用中删除

But I know the query is getting executed because when I try. It deletes my whole table. DELETE FROM SuccessfulCalls

更新我的主键设置错误.对于 sql lite 自动增加主键,他们需要 INTEGER PRIMARY KEY 而不是 int PRIMARY KEY

UPDATE I was setting up my primary key wrong. For sql lite to autoincrement one's primary key they need INTEGER PRIMARY KEY not int PRIMARY KEY

推荐答案

ORDER BYLIMIT 在语法上不允许用于 DELETE 查询除非 sqlite 是用 SQLITE_ENABLE_UPDATE_DELETE_LIMIT 构建的,这在 Android 上并非如此.

ORDER BY and LIMIT are not syntactically allowed with a DELETE query unless sqlite is built with SQLITE_ENABLE_UPDATE_DELETE_LIMIT which is not the case on Android.

如果要删除最高的两个 id 行,请使用

If you want to delete the two highest id rows, use

DELETE FROM SuccessfullCalls WHERE id IN (SELECT id FROM SuccessfulCalls ORDER BY id DESC LIMIT 2);

对此类查询使用 execSQL() 而不是 rawQuery() 来实际运行 SQL.

Use execSQL() for such queries and not rawQuery() to get the SQL actually run.

这篇关于android.database.sqlite.SQLiteException:靠近“ORDER":语法错误(代码 1):,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆