Android Question sqlite read data from database

hamedaz

Member
Hi you dear friends
I've written this code. I want to get information from the database based on the following conditions. But I am faced with the following error message.

android.database.sqlite.SQLiteException: near "of": syntax error (code 1): , while compiling: SELECT * FROM idom WHERE name=Horse of a different color

B4X:
b="Horse of a different color"
cur1=sql1.ExecQuery("SELECT * FROM idom WHERE name="&b)
For i=0 To cur1.RowCount -1
lbl.Initialize("")
        'lbl.Gravity=Gravity.CENTER_HORIZONTAL
        lbl.Gravity= Bit.Or(Gravity.CENTER_HORIZONTAL,Gravity.FILL)

        lbl.TextSize=20
        lbl.TextColor=Colors.Black
        lbl.Text=cur1.GetString("text")
next
 

hamedaz

Member
Always prefer parameterized queries:
B4X:
cur1 = sql1.ExecQuery2("SELECT * FROM ido WHERE name = ?" ,Array As String(b))
I've used this code, I do not get an error message. But empty information from the database will show me
 
Upvote 0

hamedaz

Member
The output is as follows:
Horse of a different color
Horse of a different color
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
@Erel ... slightly confused . any difference between ResultSet and Cursor in B4A / B4j. ?

@hamedaz .. try

B4X:
b="Horse of a different color"
    cur1 = SQL1.ExecQuery2("SELECT * FROM idom WHERE name = ?" ,Array As String(b))  '?? table name = idom / ido
   
    Log (cur1.RowCount)   
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i   
        log(cur1.GetString("name"))    '?? there is a Column / field =  "text"  or "name"    
    Next
 
Upvote 0

hamedaz

Member
I'm writing code in b4a
You wrote commands output were as follows:
0
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
are you able to upload you db ?
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Your db is good .. try

B4X:
Dim b as string ="Horse of a different color"
 cur1 = SQL1.ExecQuery2("SELECT * FROM idom WHERE name = ?" ,Array As String(b)) 
 Log (cur1.RowCount)  'Should = 2

For i = 0 To cur1.RowCount - 1
  cur1.Position = i 
  Log(cur1.GetString("text"))
Next
 
Upvote 0

hamedaz

Member

I get the following output again:
0
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
I get the following output again: 0

Can you upload project IDE > File > Export as Zip ... or paste a segment of code .
 
Upvote 0

fixit30

Active Member
Licensed User
Longtime User
Sound's to me like you have an old/empty copy of the database on your device.

Try uninstalling the app and installing a fresh copy and try again.
 
Upvote 0

fixit30

Active Member
Licensed User
Longtime User
Hi Hamedaz.

The problem is with your database file. It contains two carriage returns after the text, and therefore is not an exact match for your search text.

Try the corrected database attached, or change your code to:

B4X:
b = "%Horse of a different color%"
cur1=sql1.ExecQuery2("SELECT * FROM idom WHERE name Like ?", Array As String(b))



 

Attachments

  • s.zip
    646 bytes · Views: 190
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…