mardi 28 juillet 2015

Android SQlite get all from database

I have one spinner populated with items from SQLite database. I made this as follows:

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() is a method declared in one class (baseDatosHistorico) that extends from SQLiteOpenHelper and its function is select the name of the servers in the column "n_servidor" saved on SQLite database. This works fine.

I have a problem when I select an ITEM from the spinner to show data from database in AlertDialog according with ITEM selected (WHERE clause). I have the following function:

@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 says:

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 exists in database. ¿Any idea why I'm getting this error?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire