Error querying sqlite database in android studio.

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

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.

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

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.

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_llistajug);
  5. Llistajugadors();
  6. }
  7. public void Llistajugadors(){
  8. AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracio", null, 1);
  9. SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
  10. if(BaseDeDades!=null){
  11. Cursor c= BaseDeDades.rawQuery("select * from jugadors", null);
  12. int quantitat = c.getCount();
  13. int i=0;
  14. String[] array = new String[quantitat];
  15. if (c.moveToFirst()){
  16. do{
  17. String linia = c.getInt(0) + "-" + c.getString(1);
  18. array[i] = linia;
  19. i++;
  20. }while(c.moveToNext());
  21. }
  22. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array);
  23. final ListView llista = (ListView)findViewById(R.id.llista);
  24. llista.setAdapter(adapter);
  25. llista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  26. @Override
  27. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  28. Intent intent = getIntent();
  29. intent.putExtra("dato2", llista.getItemAtPosition(position).toString());
  30. setResult(RESULT_OK, intent);
  31. finish();
  32. }
  33. });
  34. }
  35. }

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.

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_llistajug);
  5. jugador = getIntent().getStringExtra("name");
  6. exercici = getIntent().getStringExtra("exercise");
  7. nom = jugador.split("-")[1];
  8. codi = Integer.parseInt(jugador.split("-")[0]);
  9. Resultats();
  10. }
  11. public void Resultats() {
  12. AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracio", null, 1);
  13. SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
  14. if (BaseDeDades != null) {
  15. Cursor c2 = BaseDeDades.rawQuery("select * from resultats", null);
  16. int quantitat2 = c2.getCount();
  17. int i2 = 0;
  18. String[] array2 = new String[quantitat2];
  19. if (c2.moveToFirst()) {
  20. do {
  21. String linia2 = c2.getInt(0) + "-" + c2.getString(1);
  22. array2[i2] = linia2;
  23. i2++;
  24. } while (c2.moveToNext());
  25. }
  26. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array2);
  27. final ListView llista2 = (ListView) findViewById(R.id.llista2);
  28. llista2.setAdapter(adapter);
  29. }
  30. }

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?

  1. 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.

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

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.

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_llistajug);
  5. Llistajugadors();
  6. }
  7. public void Llistajugadors(){
  8. AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,&quot;administracio&quot;,null,1);
  9. SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
  10. if(BaseDeDades!=null){
  11. Cursor c= BaseDeDades.rawQuery(&quot;select * from jugadors&quot;,null);
  12. int quantitat = c.getCount();
  13. int i=0;
  14. String[] array = new String[quantitat];
  15. if (c.moveToFirst()){
  16. do{
  17. String linia = c.getInt(0)+&quot;-&quot;+c.getString(1);
  18. array[i] = linia;
  19. i++;
  20. }while(c.moveToNext());
  21. }
  22. ArrayAdapter&lt;String&gt;adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1,array);
  23. final ListView llista = (ListView)findViewById(R.id.llista);
  24. llista.setAdapter(adapter);
  25. llista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  26. @Override
  27. public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  28. Intent intent = getIntent();
  29. intent.putExtra(&quot;dato2&quot;, llista.getItemAtPosition(position).toString());
  30. setResult(RESULT_OK,intent);
  31. finish();
  32. }
  33. });
  34. }
  35. }

}

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

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.activity_llistajug);
  5. jugador = getIntent().getStringExtra(&quot;name&quot;);
  6. exercici = getIntent().getStringExtra(&quot;exercise&quot;);
  7. nom = jugador.split(&quot;-&quot;)[1];
  8. codi = Integer.parseInt(jugador.split(&quot;-&quot;)[0]);
  9. Resultats();
  10. }
  11. public void Resultats() {
  12. AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, &quot;administracio&quot;, null, 1);
  13. SQLiteDatabase BaseDeDades = admin.getWritableDatabase();
  14. if (BaseDeDades != null) {
  15. Cursor c2 = BaseDeDades.rawQuery(&quot;select * from resultats&quot;,null);
  16. int quantitat2 = c2.getCount();
  17. int i2 = 0;
  18. String[] array2 = new String[quantitat2];
  19. if (c2.moveToFirst()) {
  20. do {
  21. String linia2 = c2.getInt(0) + &quot;-&quot; + c2.getString(1);
  22. array2[i2] = linia2;
  23. i2++;
  24. } while (c2.moveToNext());
  25. }
  26. ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, array2);
  27. final ListView llista2 = (ListView) findViewById(R.id.llista2);
  28. llista2.setAdapter(adapter);
  29. }
  30. }

}

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?

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

答案1

得分: 0

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

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

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:

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

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:

确定