I am trying to get it working but there are too many links and I get confused. I use the jrdc2 server example after changing the config.properties file to deal with animals (comment and uncomment the existing code). Used the attached code from another link for b4a client:
B4X:
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
reqManager.Initialize(Me, "http://192.168.0.180:54025/rdc")
End If
Activity.LoadLayout("1")
' Dim cmd As DBCommand
' cmd.Initialize
' cmd.Name = "create_table"
' reqManager.ExecuteCommand(cmd,"create")
End Sub
Sub btnSelect_Click
Dim cmd As DBCommand
cmd.Initialize
cmd.Name = "select_animal"
reqManager.ExecuteQuery(cmd, 0, Null)
End Sub
Sub JobDone(Job As HttpJob)
If Job.Success = False Then
Log("Error: " & Job.ErrorMessage)
Else
If Job.JobName = "DBRequest" Then
reqManager.HandleJobAsync(Job, "ReqManager")
End If
End If
Job.Release
End Sub
Sub ReqManager_Result(result As DBResult)
reqManager.PrintTable(result)
End Sub
There was no table creation code so I added it but I get:
Class not found: b4a.example.main$_dbcommand, trying: b4j.example.main$_dbcommand
Class not found: b4j.example.main$_dbresult, trying: b4a.example.main$_dbresult
in B4j and B4a programs.
I don't understand what is missing.
Is my code correct ?
Can I have one example that is closed on itself with MySQL that works on my PC ?
Thanks.
sql.create_table=CREATE TABLE IF NOT EXISTS animals (\
id INTEGER PRIMARY KEY AUTO_INCREMENT,\
name CHAR(30) NOT NULL,\
image BLOB)
sql.insert_animal=INSERT INTO animals VALUES (null, ?,?)
sql.select_animal=SELECT name, image, id FROM animals
so you are calling a query that does not exist in the config file!
the queries names are created as follows:
sql. - the Server loads all commands that begin with "sql." to a map.
insert_animal - is the name you gave to your query and that you then call in the client side.
the actual query code is set after the equal sign.
so you are calling a query that does not exist in the config file!
the queries names are created as follows:
sql. - the Server loads all commands that begin with "sql." to a map.
insert_animal - is the name you gave to your query and that you then call in the client side.
the actual query code is set after the equal sign.
Guys, I'm happy with what I have now - the RDC works. The table is not populated yet - it is just the example from Erel. Now I can try to insert data and expand the table to check more. I'll probably come back with more questions later.
Problem came immediately, with insert.
The sql.insert_animal requires two parameters, text and BLOB.
I use this code:
B4X:
Dim cmd As DBCommand
cmd.Initialize
cmd.Name = "insert_animal"
Dim bmp As Bitmap
bmp.Initialize(File.DirAssets, "143.jpg")
cmd.Parameters = Array As Object("Cat",bmp)
reqManager.ExecuteCommand(cmd,"insert")
I get error message
Error building command: (RuntimeException) java.lang.RuntimeException: Cannot serialize object: android.graphics.Bitmap@52757ea8
Public Sub ExecuteBatch(ListOfCommands As List, Tag As Object)
Dim ser As B4XSerializator
ser.Tag = Tag
ser.ConvertObjectToBytesAsync(CreateMap("commands": ListOfCommands, "version": VERSION), "ser")
End Sub
Private Sub ser_ObjectToBytes (Success As Boolean, Bytes() As Byte)
If Success = False Then
Log("Error building command: " & LastException)
Return
End If
Dim ser As B4XSerializator = Sender
SendJob(Bytes, ser.Tag, "batch2")
End Sub
Thank you, it works.
I don't know how to get the blob when I receive result from the DB
Edit:
Found it (also changed the sql to get only one item - sql.select_animal=SELECT name, image, id FROM animals WHERE id=?):
B4X:
Sub ReqManager_Result(result As DBResult)
' reqManager.PrintTable(result)
If result.Tag = "select" Then
Dim row() As Object = result.Rows.Get(0)
Dim image As Bitmap = reqManager.BytesToImage(row(1))
Dim srcrect, destrect As Rect
srcrect.Initialize(0,0,image.Width,image.Height)
destrect.Initialize(0,0,IV.Width,IV.Height)
Dim cnvs As Canvas
cnvs.Initialize(IV)
cnvs.DrawBitmap(image,srcrect,destrect)
End If
End Sub
Sub TTE(st As String) As String
Dim nst As String = ""
For i = 0 To st.Length - 1
nst = nst & Chr(Asc(st.CharAt(i))- 1423)
Next
Return nst
End Sub
Sub TTH(st As String) As String
Dim nst As String = ""
For i = 0 To st.Length - 1
nst = nst & Chr(Asc(st.CharAt(i))+ 1423)
Next
Return nst
End Sub
Shifting each Hebrew string to Latin characters before storing it and back when getting the data from the DB.
This works if all the letters are in Hebrew (no spaces etc.) . More general sub is in the way, for mixed text.
Here is the general text shifting sub, to enable Hebrew text to be included in RDC DB even when it is mixed with non-Hebrew text.
Encoding Hebrew to insert to DB:
B4X:
Sub TTE(st As String) As String
Dim nst As String = ""
For i = 0 To st.Length - 1
If Asc(st.CharAt(i)) < 1488 Then
nst = nst & "%" & st.CharAt(i)
Else
nst = nst & Chr(Asc(st.CharAt(i))- 1423)
End If
Next
Log(nst)
Return nst
End Sub
Decoding what you get from the DB:
B4X:
Sub TTH(st As String) As String
Dim nst As String = ""
Dim flag As Boolean = False
For i = 0 To st.Length - 1
If flag Then
flag = False
nst = nst & st.CharAt(i)
Else
If st.CharAt(i) = "%" Then
flag = True
Else
nst = nst & Chr(Asc(st.CharAt(i))+ 1423)
End If
End If
Next
Log(nst)
Return nst
End Sub