Android Question How to pass variables to a sql database

cammel8

Member
Licensed User
Longtime User
OK so I am trying to set up a database and this is what i have so far

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim sql1 As SQL

End Sub
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    If FirstTime Then
        sql1.Initialize(File.DirDefaultExternal, DateTime.GetMonth(DateTime.Now) &"."& DateTime.GetDayOfMonth(DateTime.Now) &"."& DateTime.GetYear(DateTime.Now) & " " & DateTime.TIME(DateTime.Now) & ".db", True)

    End If
createtables

End Sub
Sub populatedatabased
    sql1.BeginTransaction
    Try
        sql1.ExecNonQuery("INSERT INTO table1 VALUES ('"+playeronename+"', "+EBmakep1+", "+EBMissp1+", "+EBdefp1+", "+EBLotp1+", "+EBMobP1+", "+EBDeadp1+", "+EBe8P1+", "+EB8obp1+", "+EBbnrp1+", '"+winnerwinner+"', "+gamechosen+")")
        sql1.ExecNonQuery("INSERT INTO table1 VALUES ("+playertwoname+", "+EBmakep2+", "+EBMissp2+", "+EBdefp2+", "+EBLotp2+", "+EBMobP2+", "+EBDeadp2+", "+EBe8P2+", "+EB8obp2+", "+EBbnrp2+", "+winnerwinner+", "+gamechosen+")")
        sql1.ExecNonQuery("INSERT INTO table1 VALUES ('george', "+EBmakep1+", 4, 2, 0, 1, 6, 0, 0, 1, 'george', 8)")
        sql1.ExecNonQuery("INSERT INTO table1 VALUES ('TED', 22, 41, 21, 01, 11, 61, 01, 01, 11, 'george', 8)")
         sql1.TransactionSuccessful

    Catch
        Log(LastException.Message)
    End Try

    sql1.EndTransaction
    End Sub
Sub createtables
    sql1.ExecNonQuery("DROP TABLE IF EXISTS table1")
    sql1.ExecNonQuery("CREATE TABLE table1 (Col1 TEXT, col2 INTEGER, col3 INTEGER, col4 INTEGER, col5 INTEGER, col6 INTEGER, col7 INTEGER, col8 INTEGER, col9 INTEGER, col10 INTEGER, col11 TEXT, col12 INTEGER)")


End Sub

**********************
The four lines are all uncommented but the first two ae my first try and second two are my second try and i commented out the non working ones when trying other things they are just here to show how i was doing it. Also all of the variables are already in globals and working fine. I just didn't see the need to copy that code.
**********************
But when I try to call populatedatabase it skips over everything.
this line works:
sql1.ExecNonQuery("INSERT INTO table1 VALUES ('TED', 22, 41, 21, 01, 11, 61, 01, 01, 11, 'george', 8)")

but this doesnt:
sql1.ExecNonQuery("INSERT INTO table1 VALUES ('george', "+EBmakep1+", 4, 2, 0, 1, 6, 0, 0, 1, 'george', 8)")

So obviously it has something to do with the "+variable+" format

I have tried to find another way to pass it but i havent found a way.

In the tutorial it says use execnonquery2 to put ? in place of values but doesn't have anything on how to do it to write known values

All I'm trying to do is write the values i have stored in the variables to a database. Any help would be appreciated. And thank you in advance.
 
Last edited:

cammel8

Member
Licensed User
Longtime User
Table fields are missing in your code
You have to do that way:


I tried both of these liones in place of what i hd and neither worked

B4X:
  sql1.ExecNonQuery("INSERT INTO table1 (Col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12) VALUES ("+playertwoname+", "+EBmakep2+", "+EBMissp2+", "+EBdefp2+", "+EBLotp2+", "+EBMobP2+", "+EBDeadp2+", "+EBe8P2+", "+EB8obp2+", "+EBbnrp2+", "+winnerwinner+", "+gamechosen+")")
sql1.ExecNonQuery("INSERT INTO table1 (Col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12) VALUES (playertwoname, EBmakep2, EBMissp2, EBdefp2, EBLotp2, EBMobP2, EBDeadp2, EBe8P2, EB8obp2, EBbnrp2, winnerwinner, gamechosen)")


im not having a problem when i use the values themselves. Its when i try to use the variable its stored in that i have the problem.

So in your example ted and 22 are the values but playername and ebmakep1 are the variables it has to get the values from.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
some databases support the lack of fields. it will then insert in the order of the existing fields.

the problem is the concatenating. it's not + but & to merge strings.
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
some databases support the lack of fields. it will then insert in the order of the existing fields.

the problem is the concatenating. it's not + but & to merge strings.
Ok i tried both of these lines of code following what you said and neither work

B4X:
sql1.ExecNonQuery("INSERT INTO table1 VALUES ('"&playeronename&"', "&EBmakep1&", "&EBMissp1&", "&EBdefp1&", "&EBLotp1&", "&EBMobP1&", "&EBDeadp1&", "&EBe8P1&", "&EB8obp1&", "&EBbnrp1&", '"&winnerwinner&"', "&gamechosen&")")
sql1.ExecNonQuery("INSERT INTO table1 (Col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12) VALUES ("&playertwoname&", "&EBmakep2&", "&EBMissp2&", "&EBdefp2&", "&EBLotp2&", "&EBMobP2&", "&EBDeadp2&", "&EBe8P2&", "&EB8obp2&", "&EBbnrp2&", "&winnerwinner&", "&gamechosen&")")
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
try

B4X:
sql1.ExecNonQuery("INSERT INTO table1 (Col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12) VALUES ('"& playertwoname &"', "& EBmakep2 &", "& EBMissp2 &", "& EBdefp2 &", "& EBLotp2 &", "& EBMobP2 &", "& EBDeadp2 &", "& EBe8P2 &", "& EB8obp2 &", "& EBbnrp2 &", '"& winnerwinner &"', "& gamechosen  & ")")
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
B4X:
sql1.ExecNonQuery("CREATE TABLE table1 (Col1 TEXT, col2 INTEGER, col3 INTEGER, col4 INTEGER, col5 INTEGER, col6 INTEGER, col7 INTEGER, col8 INTEGER, col9 INTEGER, col10 INTEGER, col11 TEXT, col12 INTEGER)")


Most of the culumns are INTEGER. You can't insert "& EBmakep2 &" (which is a string). What are those "&" for anyway?
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
Nope same problem, still screws up and doesnt write to database.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If you want to use variables and not worry about quotes getting in the way, it is better to parameterize the query like this:
B4X:
SQL1.ExecNonQuery2("INSERT INTO table1  VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", _
    Array As Object( playertwoname, EBmakep2, EBMissp2, EBdefp2, EBLotp2, EBMobP2, EBDeadp2, EBe8P2, _
    EB8obp2, EBbnrp2, winnerwinner, gamechosen))
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
use it lile this then it's easier to debug

B4X:
dim mysql as string
mysql="INSERT INTO table1 (Col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12) VALUES ('"& playertwoname &"', "& EBmakep2 &", "& EBMissp2 &", "& EBdefp2 &", "& EBLotp2 &", "& EBMobP2 &", "& EBDeadp2 &", "& EBe8P2 &", "& EB8obp2 &", "& EBbnrp2 &", '"& winnerwinner &"', "& gamechosen  & ")"
log(mysql)

and paste the result here
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
INSERT INTO table1 (Col1, col2, col3, col4, col5, col6, col7, col8, col9, col10, col11, col12) VALUES ('"ANDREW" D HANKEE ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 'GEORGE MCCOLLUM II ', 8)
** Activity (main) Pause, UserClosed = true **
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
Nope that didnt work either

at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:267)
at b4a.example.main._populatedatabased(main.java:3757)
at b4a.example.main._winnerp2_click(main.java:5475)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:702)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4807)
at android.view.View$PerformClick.run(View.java:20106)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5567)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
java.lang.Exception: android.database.sqlite.SQLiteException: no such table: table1 (code 1): , while compiling: INSERT INTO table1 VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
are you sure about your table layout & field names?

Edit: ignore this.... I just saw that you created the table by code
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
are you sure about your table layout & field names?
I'm not sure about anything but I followed the tutorial and basically tried copying what I could. But anything referring to the database is copied into the first post. I'll be honest I have no clue what I'm doing but since I haven't been able to get anything else to work I was trying a database. I have been working on this app for about four or five weeks and I'm stuck at this point the last five days have just been figuring out what doesn't work.
 
Upvote 0

cammel8

Member
Licensed User
Longtime User
IT has col2 INTEGER in the create table statement. And ebmakep2 is an integer when I defined it. If i pass a number to it it works it just doesn't work when I use a variable in place of the integer which is what I'm trying to figure out. And the & signs are because someone said i couldn't use the + I had to use the & which also did not work
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
the query works fine on my computer so that good.

it's something else that goes wrong
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…