gpt4 book ai didi

android - 使用 rawquery 对单词进行排序

转载 作者:行者123 更新时间:2023-11-30 00:08:26 27 4
gpt4 key购买 nike

我希望能够使用原始查询按升序对单词进行排序(因为我将使用游标)。但是,当我尝试按字母顺序对单词进行排序时,我的应用程序崩溃了,出现了这个错误:

android.database.sqlite.SQLiteException: near "SELECTWORDFROM": syntax error (code 1): , while compiling: SELECTWORDFROM WORDSORDER BYWORD

错误发生的代码如下。

WordsDataSource.java

public Cursor sortWords (){
String[] wordname = new String[]{ WordsHelper.COLUMN_WORD };
Cursor cursor = mDatabase.rawQuery
("SELECT"+ TextUtils.join(", ", wordname)+"FROM " +
WordsHelper.TABLE_WORDS +
"ORDER BY"+ TextUtils.join(", ", wordname),null);
//Cursor cursor= mDatabase.query(WordsHelper.TABLE_WORDS,wordname , null, null, null, null, WordsHelper.COLUMN_WORD+" ASC");

return cursor;
}

WordsHelper.java(我的实际数据库)

package mapp.com.sg.pocketdictionary.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class WordsHelper extends SQLiteOpenHelper {
public static final String TABLE_WORDS ="WORDS";
public static final String COLUMN_ID = "_ID";
public static final String COLUMN_WORD = "WORD";
public static final String COLUMN_MEANING = "MEANING";
public static final String COLUMN_TYPE = "TYPE";
public static final String COLUMN_FAVOURITE = "FAVOURITE";

private static final String DB_NAME = "words.db";
private static final int DB_VER = 1;
private static final String DB_CREATE = "CREATE TABLE "+TABLE_WORDS+
" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT" +
", " + COLUMN_WORD + " TEXT, " +
COLUMN_MEANING + " TEXT,"+
COLUMN_TYPE + " TEXT,"+ COLUMN_FAVOURITE +" INTEGER)";


public WordsHelper(Context context){
super(context, DB_NAME, null, DB_VER);

}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(DB_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
}

我哪里出错了?

最佳答案

您正在使用 Arrays.ToString() 方法,这是错误的原因。尝试使用 TextUtils.join(", ", wordname)

将数组转换为字符串

Arrays.toString() 返回“debug-style” 字符串,即“[WORD]”。而不是“[WORD]”,你需要 WORD

更新

查看当前错误

android.database.sqlite.SQLiteException: near "SELECTWORDFROM": syntax error (code 1): , while compiling: SELECTWORDFROM WORDSORDER BYWORD

您需要在 SELECT 和 ORDER 之后添加空格

尝试以下操作

mDatabase.rawQuery
("SELECT "+ TextUtils.join(", ", wordname)+" FROM " +
WordsHelper.TABLE_WORDS +
" ORDER BY "+ TextUtils.join(", ", wordname),null);

关于android - 使用 rawquery 对单词进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48577119/

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