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

Code: Copy
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
 
@Erel ... slightly confused . any difference between ResultSet and Cursor in B4A / B4j. ?

@hamedaz .. try

Code: Copy
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
@Erel ... slightly confused . any difference between ResultSet and Cursor in B4A / B4j. ?

@hamedaz .. try

Code: Copy
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
I'm writing code in b4a
You wrote commands output were as follows:
0
 
Upvote 0
are you able to upload you db ?
 
Upvote 0
Your db is good .. try

Code: Copy
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
Your db is good .. try

Code: Copy
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

I get the following output again:
0
 
Upvote 0
I get the following output again: 0

Can you upload project IDE > File > Export as Zip ... or paste a segment of code .
 
Upvote 0
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
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:

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



Untitled.png
 

Attachments

  • s.zip
    s.zip
    646 bytes · Views: 378
Upvote 0
Back
Top