gpt4 book ai didi

android - 如何在 Android Sqlite 中进行查询以拉丁顺序返回

转载 作者:太空狗 更新时间:2023-10-29 15:05:18 25 4
gpt4 key购买 nike

我在我的 android 应用程序中有如下查询

SELECT N.NOME AS NUTRIENTE FROM NUTRIENTES WHERE CODNUTRIENTE IN (" + nutrientes + ") ORDER BY  N.NOME COLLATE NOCASE

返回结果

Cálcio
ED Suinos
Fibra Bruta
Fósforo
Gordura
Lisina
Metionina
Proteina
Sódio´
Ácido
Triptofano

但它应该以正确的顺序返回

Ácido
Cálcio
ED Suinos
Fibra Bruta
Fósforo
Gordura
Lisina
Metionina
Proteina
Sódio
Triptofano

任何人都可以帮助我或提供指导或阅读文档,我在 sqlite 官方网站上搜索了答案,但没有找到任何东西。我将数据库创建为

公共(public)类 MySQLiteHelper 扩展 SQLiteOpenHelper {

  private static Context context;


public static final String TABLE_NUTRIENTES = "NUTRIENTES";

public static final String CREATE_TABLE_NUTRIENTES = "CREATE TABLE NUTRIENTES ( "
+ " CODNUTRIENTE INTEGER primary key autoincrement, "
+ " NOME VARCHAR(25) , "
+ " CODUNIDADE INTEGER, "
+ " COMENTARIO VARCHAR(1000) , "
+ " MULTIPLICADOR REAL, "
+ " VISIVEL INTEGER, "
+ " TRAVADO INTEGER, "
+ " FOREIGN KEY (CODUNIDADE) REFERENCES UNIDADES (CODUNIDADE) ON UPDATE CASCADE "
+ " ); ";

private static String DATABASE_NAME = ((Config)Config.getInstance(context)).getValue("BancoDeDados") + ".db";
private static final int DATABASE_VERSION = 1;


public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}

public MySQLiteHelper(Context context, String nomeBanco) {
super(context, nomeBanco + ".db", null, DATABASE_VERSION);
this.context=context;
}

@Override
public void onCreate(SQLiteDatabase database) {
if (!database.isReadOnly()) {
database.execSQL("PRAGMA foreign_keys=ON;");
}
criaTabelas(database);


preencheNutrientes(database);


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");


db.execSQL("DROP TABLE IF EXISTS " + TABLE_NUTRIENTES);


onCreate(db);
}

public void criaTabelas(SQLiteDatabase database) {

database.execSQL(CREATE_TABLE_NUTRIENTES);

}


public void preencheNutrientes(SQLiteDatabase database) {
List<String[]> valueslist = Util.readCsv(context,"nutrientes.csv");

for(String[] dados: valueslist){
ContentValues insertValues = new ContentValues();
insertValues.put("CODNUTRIENTE",dados[0]);
insertValues.put("NOME",dados[1]);
insertValues.put("CODUNIDADE",dados[3]);
insertValues.put("COMENTARIO",dados[2]);
insertValues.put("MULTIPLICADOR",dados[4]);
insertValues.put("VISIVEL",dados[5]);
insertValues.put("TRAVADO",dados[6]);
database.insert("NUTRIENTES", null, insertValues);
}
}


@SuppressLint("Override")
public static String getDatabaseName() {
return DATABASE_NAME;
}
@Override
public void onOpen(SQLiteDatabase db) {
if (!db.isReadOnly()) {
db.execSQL("PRAGMA foreign_keys=ON;");
}
super.onOpen(db);
}

public static void changeDatabase(String nomeBD) {
DATABASE_NAME = nomeBD + ".db";
}

最佳答案

我正要给你一个答案,但我发现我遇到了同样的问题。不管怎样,我刚刚找到了解决方案 :)。

您只需要在表的定义中添加COLLATE LOCALIZED。对 NOME 使用 VARCHAR(25) COLLATE LOCALIZEDTEXT COLLATE LOCALIZED

请记住,您需要重新创建数据库以应用此更改或在 onUpgrade 函数中编写相应的代码。

通过这样做,您将不需要在 SELECT 语句中使用 COLLATE

关于android - 如何在 Android Sqlite 中进行查询以拉丁顺序返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22492348/

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