Android Question [Solved] simple sqlite table to json and vice versa codes

AnandGupta

Expert
Licensed User
Longtime User
I am trying to create simple sqlite table to json and vice versa

Below code gives the json from sqlite table (here I use the strings.db of localizator as example). Ok here.
B4X:
    Dim JSONGen As JSONGenerator
    JSONGen.Initialize(DBUtils.ExecuteJSON(sql, "SELECT key, lang, value FROM data", Null, 0, Array As String(DBUtils.DB_TEXT, DBUtils.DB_TEXT, DBUtils.DB_TEXT)))
    Dim JSONString As String
    JSONString = JSONGen.ToPrettyString(4)
    Log(JSONString)

Below code I try to insert the above json to similar db "strings_empty.db
B4X:
    Dim lstRecords2 As List
    lstRecords2.Initialize
    Dim parser2 As JSONParser
    parser2.Initialize(JSONString)
    Dim mapRoot2 As Map = parser2.NextObject
    Dim lstRoot2 As List = mapRoot2.Get("root")
    Log(lstRoot2)

    Dim result2 As Boolean = DBUtils.InsertMaps(sql2, "data", lstRoot2)
    Log(result2)
    Log(LastException)

But not working. I think the "lstRoot2" is not correct format for InsertMaps.
Any idea how to solve it ?

BTW I got the above codes from below, thanks to @LucaMs
 
Solution
I am trying to create simple sqlite table to json and vice versa

Below code gives the json from sqlite table (here I use the strings.db of localizator as example). Ok here.
B4X:
    Dim JSONGen As JSONGenerator
    JSONGen.Initialize(DBUtils.ExecuteJSON(sql, "SELECT key, lang, value FROM data", Null, 0, Array As String(DBUtils.DB_TEXT, DBUtils.DB_TEXT, DBUtils.DB_TEXT)))
    Dim JSONString As String
    JSONString = JSONGen.ToPrettyString(4)
    Log(JSONString)

Below code I try to insert the above json to similar db "strings_empty.db
B4X:
    Dim lstRecords2 As List
    lstRecords2.Initialize
    Dim parser2 As JSONParser
    parser2.Initialize(JSONString)
    Dim mapRoot2 As Map = parser2.NextObject
    Dim lstRoot2 As List =...

teddybear

Well-Known Member
Licensed User
I am trying to create simple sqlite table to json and vice versa

Below code gives the json from sqlite table (here I use the strings.db of localizator as example). Ok here.
B4X:
    Dim JSONGen As JSONGenerator
    JSONGen.Initialize(DBUtils.ExecuteJSON(sql, "SELECT key, lang, value FROM data", Null, 0, Array As String(DBUtils.DB_TEXT, DBUtils.DB_TEXT, DBUtils.DB_TEXT)))
    Dim JSONString As String
    JSONString = JSONGen.ToPrettyString(4)
    Log(JSONString)

Below code I try to insert the above json to similar db "strings_empty.db
B4X:
    Dim lstRecords2 As List
    lstRecords2.Initialize
    Dim parser2 As JSONParser
    parser2.Initialize(JSONString)
    Dim mapRoot2 As Map = parser2.NextObject
    Dim lstRoot2 As List = mapRoot2.Get("root")
    Log(lstRoot2)

    Dim result2 As Boolean = DBUtils.InsertMaps(sql2, "data", lstRoot2)
    Log(result2)
    Log(LastException)

But not working. I think the "lstRoot2" is not correct format for InsertMaps.
Any idea how to solve it ?

BTW I got the above codes from below, thanks to @LucaMs
I don't know what issue you encountered, but it works for me
 
Upvote 0
Solution

Mahares

Expert
Licensed User
Longtime User
II know but "data" is an SQLite keyword, so it's very likely that it's not accepted as a table name.
No. Data is not a reserved word in SQLite. You are probably thinking such because Erel uses it as the table name in the internal database used by B4XTable. Erel could have used any other name and it would have worked.
 
Upvote 0
Top