Android Question SQL error

a_carignan

Member
Licensed User
Longtime User
code here
'Code for save
For a =0 To liste_cartes.Size-1
carte=liste_cartes.Get(a)
liste_arg.Clear
liste_arg.Add(carte.nom)
liste_arg.Add(carte.pos)
liste_arg.Add(renamefile_forwrite(carte.directory))
liste_arg.Add(renamefile_forwrite(carte.jpeg))
liste_arg.Add(renamefile_forwrite(carte.html))
liste_arg.Add(carte.web_code)
liste_arg.Add(writeboolean(carte.reversible))
liste_arg.Add(writeboolean(carte.inverser))
liste_arg.Add(carte.bottom)
liste_arg.Add(carte.box_left)
liste_arg.Add(carte.box_top)
liste_arg.Add(carte.height)
liste_arg.Add(carte.left)
liste_arg.Add(carte.right)
liste_arg.Add(carte.top)
liste_arg.Add(carte.widht)
liste_arg.Add(savebitmaptoblob(carte.image))
sql1.ExecNonQuery2("insert into cartes values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",liste_arg)
Next

'Code for read
cursor1=sql1.ExecQuery("select * from cartes order by pos")
If cursor1.RowCount>0 Then
liste_cartes.Clear
For a =0 To cursor1.RowCount-1
cursor1.Position=a
Dim carte As type_carte
carte.Initialize
carte.nom=cursor1.GetString("name")
carte.pos=cursor1.GetLong("pos")
carte.directory=renamefile_forread(cursor1.GetString("directory"))
carte.jpeg=renamefile_forread(cursor1.GetString("jpeg"))
carte.html=renamefile_forread(cursor1.GetString("html"))
carte.web_code=cursor1.GetString("web_code")
carte.reversible=readboolean(cursor1.GetInt("reversible"))
carte.inverser=readboolean(cursor1.Getint("inverser"))
carte.bottom=cursor1.Getint("bottom")
carte.box_left=cursor1.Getint("box_left")
carte.box_top=cursor1.Getint("box_top")
carte.height=cursor1.Getint("height")
carte.left=cursor1.Getint("left")
carte.right=cursor1.Getint("right")
carte.top=cursor1.Getint("top")
carte.widht=cursor1.Getint("widht")
carte.image=Loadbitmapfromblob(cursor1.getBlob("image"))
liste_cartes.add(carte)
Next
/code

Explain to me why the second card is unreadable on the phone. But on the computer all the cards are read well. The version of the computer will read well on the phone too. Only the written version of the phone unreadable by the phones.
Explinations?
 
Last edited:

a_carignan

Member
Licensed User
Longtime User
code here
'Here is the new code, and it fixed the problem.
sql1.BeginTransaction
Try
For a =0 To liste_cartes.Size-1
carte=liste_cartes.Get(a)
liste_arg.Clear
liste_arg.Add(carte.nom)
liste_arg.Add(carte.pos)
liste_arg.Add(renamefile_forwrite(carte.directory))
liste_arg.Add(renamefile_forwrite(carte.jpeg))
liste_arg.Add(renamefile_forwrite(carte.html))
liste_arg.Add(carte.web_code)
liste_arg.Add(writeboolean(carte.reversible))
liste_arg.Add(writeboolean(carte.inverser))
liste_arg.Add(carte.bottom)
liste_arg.Add(carte.box_left)
liste_arg.Add(carte.box_top)
liste_arg.Add(carte.height)
liste_arg.Add(carte.left)
liste_arg.Add(carte.right)
liste_arg.Add(carte.top)
liste_arg.Add(carte.widht)
liste_arg.Add(savebitmaptoblob(carte.image))
sql1.ExecNonQuery2("insert into cartes values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",liste_arg)
Next
sql1.TransactionSuccessful
Catch
Log(LastException.Message)
End Try
sql1.EndTransaction
/code
 
Last edited:
Upvote 0

sorex

Expert
Licensed User
Longtime User
I think you better stick to the old fashioned syntax like

B4X:
insert into cartes (field1,field2) values (?,?)

what error do you get? is it on the sql execution line or somewhere else?
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
you didn't place quotes for the string elements

B4X:
sql1.ExecNonQuery2("insert into cartes values ('?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?','?')",liste_arg)

but like I said before defining which value belongs to which field would help you out better to figure out where to place quotes
 
Upvote 0

a_carignan

Member
Licensed User
Longtime User
The fault line is the first line reading in the database given. The error is the same if I try to read a number or a string.
carte.nom = cursor1.GetString ("name")
 
Upvote 0

a_carignan

Member
Licensed User
Longtime User
Error exexeption is java.lang.illigalStateException: Couldn't read row 0, col,0 from CursorWindow. Make sure the cursor is initialized correctly before accessing data from it.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

a_carignan

Member
Licensed User
Longtime User
The writing seems correct, since the file is readable computer. Playback looks fine since reading a file created by a computer. In addition, most of the time the code reads the cards correctly, but every once in a while the file is unreadable.
 
Upvote 0

a_carignan

Member
Licensed User
Longtime User
/new code write
for a =0 to liste_cartes.size-1
carte=liste_cartes.get(a)
sql1.ExecNonQuery2("insert into cartes values ('"&carte.nom.Replace("'","''")&"',"&carte.pos&",'"&renamefile_forwrite(carte.directory).Replace("'","''")&"','"&renamefile_forwrite(carte.jpeg).Replace("'","''")&"','"&renamefile_forwrite(carte.html).Replace("'","''")&"','"&carte.web_code.Replace("'","''")&"',"&writeboolean(carte.reversible)&","&writeboolean(carte.inverser)&","&carte.bottom&","&carte.box_left&","&carte.box_top&","&carte.height&","&carte.left&","&carte.right&","&carte.top&","&carte.widht&",?)",Array As Object(savebitmaptoblob(carte.image)))
next
/end code
Same probleme when reading.
 
Upvote 0

a_carignan

Member
Licensed User
Longtime User
/new code reading
cursor1=sql1.ExecQuery("select name,pos,directory,jpeg,html,web_code,reversible,inverser,bottom,box_left,box_top,height,left,right,top,widht from cartes order by pos")
If cursor1.RowCount>0 Then
liste_cartes.Clear
For a =0 To cursor1.RowCount-1
cursor1.Position=a
Dim carte As type_carte
carte.Initialize
carte.nom=cursor1.GetString("name")
carte.pos=cursor1.GetLong("pos")
carte.directory=renamefile_forread(cursor1.GetString("directory"))
carte.jpeg=renamefile_forread(cursor1.GetString("jpeg"))
carte.html=renamefile_forread(cursor1.GetString("html"))
carte.web_code=cursor1.GetString("web_code")
carte.reversible=readboolean(cursor1.GetInt("reversible"))
carte.inverser=readboolean(cursor1.Getint("inverser"))
carte.bottom=cursor1.Getint("bottom")
carte.box_left=cursor1.Getint("box_left")
carte.box_top=cursor1.Getint("box_top")
carte.height=cursor1.Getint("height")
carte.left=cursor1.Getint("left")
carte.right=cursor1.Getint("right")
carte.top=cursor1.Getint("top")
carte.widht=cursor1.Getint("widht")
'carte.image=Loadbitmapfromblob(cursor1.getBlob("image"))
'carte.image=Main.icon
liste_cartes.add(carte)
Next
cursor1.close
For a =0 To liste_cartes.Size-1
'Msgbox(a,Main.Titre)
Dim carte As type_carte=liste_cartes.Get(a)
cursor1=sql1.ExecQuery("select * from cartes where pos="&carte.pos&" order by pos")
If cursor1.RowCount=1 Then
cursor1.Position=0
carte.image=Loadbitmapfromblob(cursor1.getBlob("image"))
liste_cartes.Set(a,carte)
Else
Return onerrordatabase("Il y a plus qu'une carte avec la position "&a)
End If
Next
cursor1.close
/end code

/new code saveimagetoblob
Sub savebitmaptoblob(image As Bitmap) As Byte()
Dim OutputStream1 As OutputStream
Dim Buffer() As Byte
OutputStream1.InitializeToBytesArray(1000)
image.WriteToStream(OutputStream1,50,"JPEG")
Buffer=OutputStream1.ToBytesArray
Return Buffer
End Sub
/end code

This seems to have fixed the problem. I feel that the images had too much quality to be read correctly.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
upload_2015-4-30_8-13-38.png


upload_2015-4-30_8-11-4.png



B4X:
cursor1=sql1.ExecQuery("select name,pos,directory,jpeg,html,web_code,reversible,inverser,bottom,box_left,box_top,height,left,right,top,widht from cartes order by pos")

If cursor1.RowCount>0 Then
liste_cartes.Clear
For a =0 To cursor1.RowCount-1
cursor1.Position=a
Dim carte As type_carte
carte.Initialize
carte.nom=cursor1.GetString("name")
carte.pos=cursor1.GetLong("pos")
carte.directory=renamefile_forread(cursor1.GetString("directory"))
carte.jpeg=renamefile_forread(cursor1.GetString("jpeg"))
carte.html=renamefile_forread(cursor1.GetString("html"))
carte.web_code=cursor1.GetString("web_code")
carte.reversible=readboolean(cursor1.GetInt("reversible"))
carte.inverser=readboolean(cursor1.Getint("inverser"))
carte.bottom=cursor1.Getint("bottom")
carte.box_left=cursor1.Getint("box_left")
carte.box_top=cursor1.Getint("box_top")
carte.height=cursor1.Getint("height")
carte.left=cursor1.Getint("left")
carte.right=cursor1.Getint("right")
carte.top=cursor1.Getint("top")
carte.widht=cursor1.Getint("widht")
'carte.image=Loadbitmapfromblob(cursor1.getBlob("image"))
'carte.image=Main.icon
liste_cartes.add(carte)
Next
cursor1.close
For a =0 To liste_cartes.Size-1
'Msgbox(a,Main.Titre)
Dim carte As type_carte=liste_cartes.Get(a)
cursor1=sql1.ExecQuery("select * from cartes where pos="&carte.pos&" order by pos")
If cursor1.RowCount=1 Then
cursor1.Position=0
carte.image=Loadbitmapfromblob(cursor1.getBlob("image"))
liste_cartes.Set(a,carte)
Else
Return onerrordatabase("Il y a plus qu'une carte avec la position "&a)
End If
Next
cursor1.close
 
Upvote 0
Top