SQL, Blob and List

Ramirez

Member
Licensed User
Longtime User
Hello.

I want to write a list of parameters into a database SQL.

In a column, I want to put some parameters, but I don't know exactly how many parameters I have to pass to this record. Some times 1, some times 10 etc... (depending the data row to record). To be more explicit, each parameter is a primary key of another table (the record of this table have one or more attributes stored in another table).
The parameters are always Interger type.

I try to create a table with the third column type set to Blob and try to write a List to this place:

B4X:
Dim liste As List
Dim answer As List
Dim SQL1 As SQL

' Création fichier
'-------------------
Log("création fichier")
SQL1.Initialize(File.DirDefaultExternal, "Database.db", True)

Log("Création table")
SQL1.ExecNonQuery("CREATE TABLE [Test] ([Clé] INTEGER PRIMARY KEY, [Réel] REAL, [Texte] TEXT, [Blob] BLOB)")
   
' Création Liste
'----------------
Log("création liste")
liste.Initialize
For n=1 To 5
   liste.Add(n)
   Log("Ajout entier: " & n)
Next

Log("taille de la liste: " & liste.Size)
Log("liste: " & liste)

' Remplissage table
'-------------------
Log("Remplissage table")
SQL1.ExecNonQuery("INSERT INTO Test VALUES (NULL ,1.23,'a' ,'" & liste & "')")

' Lecture table
'---------------
Dim curseur As Cursor
Log("Lecture fichier SQL")
curseur=SQL1.ExecQuery("SELECT * FROM Test")
curseur.Position=0
Log("Première colonne: " & curseur.GetInt("Clé"))
Log("Deuxième colonne: " & curseur.GetDouble("Réel"))
Log("Troisième colonne: " & curseur.GetString("Texte"))
Log("Dernière colonne: " & curseur.GetBlob("Blob"))

But that don't work. The last Log (dernière colonne) make an error during compiling Java code.

B4X:
Compiling code.                         0.46
Generating R file.                      0.00
Compiling generated Java code.          Error
B4A line: 58
Log(\
javac 1.6.0_21
src\test\SQL\main.java:293: cannot find symbol
symbol  : method NumberToString(byte[])
location: class anywheresoftware.b4a.BA
anywheresoftware.b4a.keywords.Common.Log("Dernière colonne: "+BA.NumberToString(_curseur.GetBlob("Blob")));
                                                                ^
1 error

I try to make another list and charging it with the GetBlob command:

B4X:
Dim answer as list
answer.Initialize2(curseur.GetBlob("Blob")
Log("Answer: " & answer)

But the answer values are strange (maybe because GetBlob return byte, but not sure):

B4X:
liste: (ArrayList) [1, 2, 3, 4, 5]
Answer: (ArrayList) [40, 65, 114, 114, 97, 121, 76, 105, 115, 116, 41, 32, 91, 49, 44, 32, 50, 44, 32, 51, 44, 32, 52, 44, 32, 53, 93, 0]

I see that "liste" contains 27 characters and "Answer" contains 27 codes (plus the last one: 0). Codes of "Answer" should be the values of each characters of "Liste" list, but how translate it to a real list ?

Where is (are?) my mistake(s) ?

What is the better way to store this kind of data ?

Thanks for helping.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are converting the string representation of the list to a bytes array. This will not give you any meaningful results.

You should instead convert the list to bytes yourself and then add it to the blob field. See the SQL tutorial for an example of adding blob fields: http://www.b4x.com/forum/basic4android-getting-started-tutorials/6736-sql-tutorial.html#post39108

You can convert the list to bytes array by using Basic4android - ByteConverter
You will need to first convert it to an array of integers.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…