Search code examples
javasqliteandroid-sqlite

Error querying sqlite database in android studio


I have a problem in my application, to see if there is someone who can help me.

It turns out that in my application I have made a database with SQLite that has two tables, one for players and one for results.

@Override
public void onCreate(SQLiteDatabase BaseDeDades) {
    BaseDeDades.execSQL("create table jugadors(codi int primary key, nom text, cognoms text, data date, club text, categoria text)");
    BaseDeDades.execSQL("create table resultats(codipuntuacio int primary key, codijugador int,codiexercici text, puntuacio text, temps long, data date)");
}

To consult the first of the tables (players) that shows a list of all the players entered in the database, I did it as follows.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_llistajug);
    Llistajugadors();
}

public void Llistajugadors(){
    AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,"administracio",null,1);
    SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
    if(BaseDeDades!=null){
        Cursor c= BaseDeDades.rawQuery("select * from jugadors",null);
        int quantitat = c.getCount();
        int i=0;
        String[] array = new String[quantitat];
        if (c.moveToFirst()){
            do{

                String linia = c.getInt(0)+"-"+c.getString(1);

                array[i] = linia;
                i++;

            }while(c.moveToNext());
        }
        ArrayAdapter<String>adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,array);
        final ListView llista = (ListView)findViewById(R.id.llista);
        llista.setAdapter(adapter);

        llista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = getIntent();
                intent.putExtra("dato2", llista.getItemAtPosition(position).toString());
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }
}

}

The problem has arisen when trying to consult the data of the other table (results) since I have tried to do it the same way

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_llistajug);

    jugador = getIntent().getStringExtra("name");
    exercici = getIntent().getStringExtra("exercise");

    nom = jugador.split("-")[1];
    codi = Integer.parseInt(jugador.split("-")[0]);

    Resultats();

}

public void Resultats() {
    AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracio", null, 1);
    SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
    if (BaseDeDades != null) {
        Cursor c2 = BaseDeDades.rawQuery("select * from resultats",null);
        int quantitat2 = c2.getCount();
        int i2 = 0;
        String[] array2 = new String[quantitat2];
        if (c2.moveToFirst()) {
            do {

                String linia2 = c2.getInt(0) + "-" + c2.getString(1);

                array2[i2] = linia2;
                i2++;

            } while (c2.moveToNext());
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array2);
        final ListView llista2 = (ListView) findViewById(R.id.llista2);
        llista2.setAdapter(adapter);
    }
}

}

But when executing this activity, in this case the application stops.

Does anyone know why if I have done it the same way? Thank you

This is the error that appears in Logcat when executing the activity: Logcat error

Thanks, the bug was fixed. But now I have another problem with the query. How can I make the query for a string?

codijugador i codi are integers and it works correctly but adding another parameter codiexercici = exerici which are strings gives me an error, are they not done the same way?

Thanks, the bug was fixed. But now I have another problem with the query. How can I make the query for a string?

Thanks, the bug was fixed. But now I have another problem with the query. How can I make the query for a string?

co-player i codi are integers and it works correctly but adding another parameter codiexercici = exerici which are strings gives me an error, are they not done the same way?

            Cursor c = BaseDeDades.rawQuery("select * from resultats where codijugador = "+codi+" and codiexercici="+exercici, null);

Solution

  • String must be enclosed inside single quotes, but this is something that you should not do by concatenating the parameters and the single quotes.
    Use ? placeholders for the parameters and the 2nd argument of rawQuery() to pass them:

    Cursor c = BaseDeDades.rawQuery(
        "select * from resultats where codijugador = ? and codiexercici = ?",
        new String[] {String.valueOf(codi), exercici}
    );