gpt4 book ai didi

android - E/SQLiteLog : (1) near "All": syntax error

转载 作者:行者123 更新时间:2023-11-29 16:33:01 27 4
gpt4 key购买 nike

我是应用程序开发的新手,我正在尝试构建一个搜索名为“All”的数据库表的应用程序,但不断收到此错误:

E/SQLiteLog: (1) near "All": syntax error

Error Code : 1 (SQLITE_ERROR) Caused By : SQL(query) error or missing database. (near "All": syntax error (code 1): , while compiling: SELECT Phrase FROM All)

我认为这一定是我的数据库设置有问题,但找不到故障。

这是我的代码:

public class Database extends SQLiteAssetHelper {
private static final String DB_NAME="DoYouGetMeDoc.db";
private static final int DB_VER=1;
public Database(Context context) {
super(context, DB_NAME, null, DB_VER);
}

public List<Items> getall(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqliteSelect={"Phrase","Description","Example", "Chapter"};
String tableName="All";
qb.setTables(tableName);
Cursor cursor = qb.query(db,sqliteSelect,null,null,null,null,null);
List<Items> results = new ArrayList<>();
if (cursor.moveToFirst()){
do {
Items items = new Items();
items.setPhrase(cursor.getString(cursor.getColumnIndex("Phrase")));
items.setDescription(cursor.getString(cursor.getColumnIndex("Description")));
items.setExample(cursor.getString(cursor.getColumnIndex("Example")));
items.setChapter(cursor.getString(cursor.getColumnIndex("Chapter")));
results.add(items);
}while (cursor.moveToNext());
}
return results;
}

public List<String> getPhrases(){
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqliteSelect={"Phrase"};
String tableName="All";
qb.setTables(tableName);
Cursor cursor = qb.query(db,sqliteSelect,null,null,null,null,null);
List<String> results = new ArrayList<>();
if (cursor.moveToFirst()){
do {
results.add(cursor.getString(cursor.getColumnIndex("Phrase")));
}while (cursor.moveToNext());
}
return results;

}

public List<Items> getPhrasesbyPhrase(String phrase)
{
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqliteSelect={"Phrase","Description","Example","Chapter"};
String tableName="All";
qb.setTables(tableName);
Cursor cursor = qb.query(db,sqliteSelect,"Phrase like ?",new String[]{"%"+phrase+"%"},null,null,null);
List<Items> results = new ArrayList<>();
if (cursor.moveToFirst()){
do {
Items items = new Items();
items.setPhrase(cursor.getString(cursor.getColumnIndex("Phrase")));
items.setDescription(cursor.getString(cursor.getColumnIndex("Description")));
items.setExample(cursor.getString(cursor.getColumnIndex("Example")));
items.setChapter(cursor.getString(cursor.getColumnIndex("Chapter")));
results.add(items);
}while (cursor.moveToNext());
}
return results;
} }

简而言之,我的应用程序应该允许用户使用我在 MainActivity.xml 中设置的搜索栏来搜索数据库,但每次我尝试打开该应用程序时,它都会崩溃。

我完全被难住了,非常感谢您提供一些建议。

更新:我将表名从“All”更改为“FullBook”,现在出现此错误:

E/SQLiteLog: (1) no such table: FullBook

最佳答案

ALL 是 SQLite 和所有(我相信)rdms 的关键字(保留字)
并且您不能将它用作表的名称。
这里可以看到SQLite所有的保留字:
https://www.sqlite.org/lang_keywords.html
如果你坚持使用它,可以将它括在方括号中,如[All]

关于android - E/SQLiteLog : (1) near "All": syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53824680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com