我有一个微调器填充了SQLite数据库中的项目。我这样做了:
private void loadSpinnerData() {
baseDatosHistorico db = new baseDatosHistorico(this, "alertasdb.db", null, 1);
List<String> labels = db.getNomServidor();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,labels);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
getNomServidor()
是在一个类(baseDatosHistorico)中声明的方法,它从 SQLiteOpenHelper 扩展,其功能是选择保存在SQLite数据库中的“n_servidor”列中的服务器名称。这很好。
当我从微调器中选择一个ITEM来根据选择的ITEM(WHERE子句)在AlertDialog中显示来自数据库的数据时,我遇到了问题。我有以下功能:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String label = parent.getItemAtPosition(position).toString();
baseDatosHistorico bbdd = new baseDatosHistorico(this, "alertasdb.db", null, 1);
SQLiteDatabase bd = bbdd.getReadableDatabase();
Cursor cursor = bd.rawQuery("SELECT date, description FROM alerts WHERE n_servidor = "+ label + "", null);
if (cursor.moveToFirst()){
AlertDialog.Builder dialog = new AlertDialog.Builder(BbddActivity.this);
dialog.setIcon(android.R.drawable.ic_dialog_info);
dialog.setTitle(label);
dialog.setMessage(cursor.getString(0) + " " + cursor.getString(1));
dialog.setCancelable(false);
dialog.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id){
dialog.cancel();
}
});
dialog.show();
}
}
LogCat说:
07-28 17:20:02.056: E/AndroidRuntime(563): FATAL EXCEPTION: main
07-28 17:20:02.056: E/AndroidRuntime(563): android.database.sqlite.SQLiteException: no such column: hostname1: , while compiling: SELECT fecha, descripcion FROM alertas_email WHERE n_servidor = hostname1
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)
07-28 17:20:02.056: E/AndroidRuntime(563): at activities.monitorapp.BbddActivity.onItemSelected(BbddActivity.java:57)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.widget.AdapterView.fireOnSelected(AdapterView.java:871)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.widget.AdapterView.access$200(AdapterView.java:42)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:837)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.os.Handler.handleCallback(Handler.java:587)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.os.Looper.loop(Looper.java:123)
07-28 17:20:02.056: E/AndroidRuntime(563): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-28 17:20:02.056: E/AndroidRuntime(563): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 17:20:02.056: E/AndroidRuntime(563): at java.lang.reflect.Method.invoke(Method.java:507)
07-28 17:20:02.056: E/AndroidRuntime(563): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-28 17:20:02.056: E/AndroidRuntime(563): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-28 17:20:02.056: E/AndroidRuntime(563): at dalvik.system.NativeStart.main(Native Method)
hostname1存在于数据库中。 ¿我知道为什么会出现这个错误?
提前致谢。
答案 0 :(得分:0)
在n_servidor =“+ label +”“之后=有一个空格,并且在执行该行时包含在查询中,因此”hostname1“不存在但存在”hostname1“。对于字符串,您必须添加这些单qoutes
所以你的查询应该像
Cursor cursor = bd.rawQuery("SELECT date, description FROM alerts WHERE n_servidor='"+ label + "'", null);
尝试以上查询希望有所帮助