Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Problems with sql query using rawQuery

Posted on November 14, 2022

Solution 1 :

I assume that both unioned queries return the same number of columns, since you don’t get any errors.

You should use proper JOIN syntax with an ON clause and not this archaic syntax with the comma.

Also, the ORDER BY clause when used at the end of UNION, it is applied to the result of the UNION and not to any of the the participating queries.
So you can’t use the aliases of the tables that you have previously set, to the columns after ORDER BY.

Finally, in your query there are 2 ? placeholders, so you must pass as the 2nd argument of rawQuery() an array of 2 strings. I guess it’s the same string, but you must use it twice in the array.

So change to this:

val parametro_lista_seleccionada = arrayOf(lista_seleccionada.toString(), lista_seleccionada.toString())
val cursor_consulta_sql: Cursor = bd_conexion.rawQuery(
    "SELECT * " +
    "FROM contenido_lista AS cl INNER JOIN productos AS p " + 
    "ON cl.id_producto = p.id_producto " + 
    "WHERE cl.id_lista = ? " + 
    "UNION " + 
    "SELECT * " + 
    "FROM contenido_lista AS cl INNER JOIN productos_manuales AS pm " + 
    "ON cl.id_producto = pm.id_producto " + 
    "WHERE cl.id_lista = ? " + 
    "ORDER BY id_seccion" ,
    parametro_lista_seleccionada
)

Just by the name of lista_seleccionada, I will make a guess, maybe it is not a string. Is it a list?
If so then lista_seleccionada.toString() will not return 2 strings, but 1 string which is a comma separated list of the strings.
So change to:

val parametro_lista_seleccionada = arrayOf(lista_seleccionada[0], lista_seleccionada[1])

or if the items of the list are numbers:

val parametro_lista_seleccionada = arrayOf(
    lista_seleccionada[0].toString(), 
    lista_seleccionada[1]
)

Problem :

My problem is that the query that I put below, used in DbBrowser for Sqlite on my PC, returns the data correctly. But once executed in the application it does not show me all the data I am missing what results from the UNION SELECT, it is as if I only solved the first part before UNION

val parametro_lista_seleccionada: Array<String> =
        arrayOf(lista_seleccionada.toString()) //Creamos un array con el id de la lista seleccionada para pasarla como parametro al SELECT
val cursor_consulta_sql: Cursor = bd_conexion.rawQuery("SELECT * FROM contenido_lista AS cl, productos AS p WHERE cl.id_lista = ? AND cl.id_producto = p.id_producto UNION SELECT * FROM contenido_lista AS cl, productos_manuales AS pm WHERE cl.id_lista = ? AND cl.id_producto = pm.id_producto ORDER BY p.id_seccion,pm.id_seccion" ,parametro_lista_seleccionada)

Comments

Comment posted by metacircuit

Thanks, I hadn’t noticed. Now everything works correctly.

READ  [ANSWERED] android - Interpreting data from an ImageReader with format ImageFormat.Private
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress