Error querying sqlite database in android studio.

huangapple go评论80阅读模式
英文:

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 and 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);
英文:

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(&quot;create table jugadors(codi int primary key, nom text, cognoms text, data date, club text, categoria text)&quot;);
BaseDeDades.execSQL(&quot;create table resultats(codipuntuacio int primary key, codijugador int,codiexercici text, puntuacio text, temps long, data date)&quot;);
}

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,&quot;administracio&quot;,null,1);
SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
if(BaseDeDades!=null){
Cursor c= BaseDeDades.rawQuery(&quot;select * from jugadors&quot;,null);
int quantitat = c.getCount();
int i=0;
String[] array = new String[quantitat];
if (c.moveToFirst()){
do{
String linia = c.getInt(0)+&quot;-&quot;+c.getString(1);
array[i] = linia;
i++;
}while(c.moveToNext());
}
ArrayAdapter&lt;String&gt;adapter = new ArrayAdapter&lt;String&gt;(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&lt;?&gt; parent, View view, int position, long id) {
Intent intent = getIntent();
intent.putExtra(&quot;dato2&quot;, 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(&quot;name&quot;);
exercici = getIntent().getStringExtra(&quot;exercise&quot;);
nom = jugador.split(&quot;-&quot;)[1];
codi = Integer.parseInt(jugador.split(&quot;-&quot;)[0]);
Resultats();
}
public void Resultats() {
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, &quot;administracio&quot;, null, 1);
SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
if (BaseDeDades != null) {
Cursor c2 = BaseDeDades.rawQuery(&quot;select * from resultats&quot;,null);
int quantitat2 = c2.getCount();
int i2 = 0;
String[] array2 = new String[quantitat2];
if (c2.moveToFirst()) {
do {
String linia2 = c2.getInt(0) + &quot;-&quot; + c2.getString(1);
array2[i2] = linia2;
i2++;
} while (c2.moveToNext());
}
ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(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(&quot;select * from resultats where codijugador = &quot;+codi+&quot; and codiexercici=&quot;+exercici, null);

答案1

得分: 0

字符串必须用单引号括起来,但这不是通过连接参数和单引号来完成的。<br/>
使用 ? 占位符来表示参数,并在 rawQuery() 的第二个参数中传递它们:

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

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

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

huangapple
  • 本文由 发表于 2020年8月3日 04:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63220834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定