Android的 - 崩溃时,我在数据库和选择数据撇号 [英] Android - Crash when i have apostrophe in database and select data
问题描述
我从我SQLite数据库选择数据。我的问题是,当数据库中有撇号(我是约翰 - 例如)。我尽量选择数据,那么我的应用程序崩溃。
I select data from my sqlite database. My problem is that when in database are apostrophes ("I' m John" - for example) and i try to select data, then my application crash.
如果我不拥有数据库撇号,然后万事大吉。
If i don' t have apostrophes in database, then everything is all right.
我的选择数据功能:
String query = "SELECT * FROM " + mainCollumn + " WHERE used=0 " + " AND season <= " + seasons + " ORDER BY RANDOM() LIMIT " + count ;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if(cursor.moveToFirst()){
do {
Question question = new Question(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),
Integer.parseInt(cursor.getString(6)));
questionList.add(question);
}while(cursor.moveToNext());
}
感谢您的帮助。
推荐答案
您可以通过特殊的双引号替换它们处理撇号,但最好的方法是使用<一个href=\"http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#query%28java.lang.String,%20java.lang.String%5B%5D,%20java.lang.String,%20java.lang.String%5B%5D,%20java.lang.String,%20java.lang.String,%20java.lang.String%29\"><$c$c>query()$c$c>与 selectionArgs两个
而不是 rawQuery()
。
You can specially handle apostrophes by replacing them with double apostrophes, but the best way is to use query()
with selectionArgs
instead of rawQuery()
.
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = new String[] {column1, column2}; // the columns you want
Cursor cursor = db.query(mainCollumn, String[] columns, "used=0 and season<=?", new String[] {seasons}, null, null, "RANDOM() LIMIT " + count);
这篇关于Android的 - 崩溃时,我在数据库和选择数据撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!