Android Question SQL error

Vicente Barba

Member
Licensed User
Hi all !

With the project attached, I am trying to create a database and insert several records. Then read those records again and I get null error.
The log output is:

DB Files: 1
Rows: 0


So the database file exists and after the insert there are no records but table exists.

Full log output:

--------- beginning of main
Copying updated assets files (3)
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
DB Files: 1
Rows: 0
Error occurred on line: 40 (Main)
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.test.gastos.main._activity_create(main.java:422)
[...]
 

Attachments

  • SQLTest.zip
    41 KB · Views: 145

DonManfred

Expert
Licensed User
Longtime User
Replace the sub InicializarBD in your app with this one

B4X:
Sub InicializarBD
    ' TABLA CONFIGURACION
    sql.ExecNonQuery("CREATE TABLE configuracion (id INTEGER PRIMARY KEY AUTOINCREMENT, variable TEXT, valor TEXT)")
    sql.ExecNonQuery2("INSERT INTO configuracion (variable, valor) VALUES(?,?);", Array As Object("version_bd", 1))
    sql.ExecNonQuery2("INSERT INTO configuracion (variable, valor) VALUES(?,?);", Array As Object("version_app", 2))
    sql.ExecNonQuery2("INSERT INTO configuracion (variable, valor) VALUES(?,?);", Array As Object("usuario", 0))
    sql.ExecNonQuery2("INSERT INTO configuracion (variable, valor) VALUES(?,?);", Array As Object("contrasena", 0))
    sql.ExecNonQuery2("INSERT INTO configuracion (variable, valor) VALUES(?,?);", Array As Object("precio_km", 0.32))

    Log("DB Files: " & sql.ExecQuerySingleResult("SELECT count(name) FROM sqlite_master WHERE type='table' AND name='configuracion' COLLATE NOCASE"))
    Log("Rows: " & sql.ExecQuerySingleResult("SELECT count(*) FROM configuracion"))
End Sub

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
DB Files: 1
Rows: 5
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
** Activity (main) Resume **

Instead of INSERTING data (ExecNonQuery) you was using

B4X:
sql.ExecQuery("INSERT INTO configuracion (variable, valor) VALUES ('version_bd', '1')")

I also changed it to use parametrized parameters. No need to care about the quotes '
 
Last edited:
Upvote 0
Top