E / SQLiteLog:(1)在“所有”附近:语法错误

时间:2018-12-18 00:04:21

标签: android sql sqlite syntax-error

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

  

E / SQLiteLog:(1)在“所有”附近:语法错误

     

错误代码:1(SQLITE_ERROR)       引起原因:SQL(query)错误或数据库丢失。         (在“全部”附近:语法错误(代码1):,而在编译时:选择全部的短语)

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

这是我的代码:

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中设置的搜索栏搜索数据库,但是每次尝试打开该应用程序时,它总是崩溃。

我很沮丧,非常感谢您提供一些建议。

更新: 我也将表名也从“全部”更改为“全书”,现在出现此错误:

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

1 个答案:

答案 0 :(得分:2)

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