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

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

问题描述

当我打开我的RSS应用程序,我想检查一个文章是否存在我的数据库。如果它存在,我想删除它(或忽略它),如果不是写入db。



我一直在尝试这样做:

  = ourDatabase.rawQuery(select from
+ DBHelper.DATABASE_TABLE +其中+ DBHelper.TITLE +=
+ title +'+AND+ DBHelper.DATE + ='+ date +',
new String [] {});
if(cursor.moveToFirst()){
boolean exists =(cursor.getCount()> 0);
cursor.close();
return exists;
} else {
return false; }

}

但我得到这个错误:

  06-06 16:01:11.341:E / AndroidRuntime(13867):Caused by:android.database.sqlite.SQLiteException:nearόσο :语法错误(代码1):,同时编译:从all_t中选择1其中title ='ΟΦΟΒΟΣκαιοΤΡΟΜΟΣτωνδιαδηλωτών! ΑυτέςοιγυναίκεςείναιπιοσκληρέςαΠ'όσοδείχνουν!'AND date ='Wed,05 Jun 2013 18:12:15'


<这是一个典型的问题,不使用 selectionArgs ,这正是为什么应该使用它们:你用简单的引号引用你的字符串,但是你的字符串包含单引号,所以SQLite在实际结束之前检测字符串的结尾,并尝试将其余部分解析为SQLite关键字。



正确的解决方案是通过使用那些 selectionArgs 离开转义到SQLite,而不是试图逃避你的自我。在你的情况下,这将是:

  Cursor cursor = ourDatabase.rawQuery(select 1 from
+ DBHelper .DATABASE_TABLE +where+ DBHelper.TITLE +=?
+AND+ DBHelper.DATE +=?,
new String [] {title,date});

加上,它更干净,因为你可以从查询中创建一个常量而不是每次构建。


When i open my RSS app, i would like to check if an article exists in my database.If it exists,i would like to delete it (or ignore it),if not to write it in the db.

I have been trying to do it something like this:

            Cursor cursor = ourDatabase.rawQuery("select 1 from "
                    + DBHelper.DATABASE_TABLE + " where " + DBHelper.TITLE + "='"
                    + title + "'" + " AND " + DBHelper.DATE + "='" + date + "'",
                    new String[] {});
            if (cursor.moveToFirst()) {
                boolean exists = (cursor.getCount() > 0);
                cursor.close();
                return exists;
            } else {
    return false;       }

        }

but i get this error:

06-06 16:01:11.341: E/AndroidRuntime(13867): Caused by: android.database.sqlite.SQLiteException: near "όσο": syntax error (code 1): , while compiling: select 1 from all_t where title='Ο ΦΟΒΟΣ και ο ΤΡΟΜΟΣ των διαδηλωτών!!! Αυτές οι γυναίκες είναι πιο σκληρές απ' όσο δείχνουν!' AND date='Wed, 05 Jun 2013 18:12:15'

解决方案

This is a typical issue with not using the selectionArgs, and it is exactly why one should use them: You are quoting your string with simple quotes, but your string contains single quote, so SQLite detect the end of the string before it actually ends, and try to parse the rest as SQLite keyword.

The proper solution is to leave the escaping to SQLite, by using those selectionArgs rather that trying to do the escaping your self. in you case, that would be :

Cursor cursor = ourDatabase.rawQuery("select 1 from "
            + DBHelper.DATABASE_TABLE + " where " + DBHelper.TITLE + "=?"
            + " AND " + DBHelper.DATE + "=?",
            new String[] {title, date});

plus, it's cleaner because you can make a constant out of your query rather than constructing it every time.

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

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