Load CSV into Sqlite

anaylor01

Well-Known Member
Licensed User
Longtime User
Here is what I am trying to do. Load a csv file into a sqlite database. This database will be used to read/write records to/from.
I have found this.
Dim su As StringUtils
Dim Table As List
Table = su.LoadCSV(File.DirAssets, "Trivia.csv", ",")

But I can't find the code to actually load the data into a table.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend you to use DBUtils: http://www.basic4ppc.com/forum/basi...475-dbutils-android-databases-now-simple.html

The first step it to create the table with DBUtils.CreateTable.
Then you need to iterate over the loaded data and convert it to a list of maps:
B4X:
Dim su As StringUtils
Dim Table As List
Table = su.LoadCSV(File.DirAssets, "Trivia.csv", ",")
Dim Table2 As List
Dim Items() As String
Table2.Initialize
For i = 0 To Table.Size - 1
 Items = Table.Get(i)
 Dim m As Map
 m.Initialize
 m.Put("column 1", Items(0)) 'You will need to replace the columns names with the correct names
 m.Put("column 2", Items(1))
 Table2.Add(m)
Next

DBUtils.InsertMaps(SQL, "YourTable", Table2)
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Compiling code. Error
Error parsing program.
Error description: Undeclared variable 'dbutils' is used before it was assigned any value.
Occurred on line: 113
DBUtils.InsertMaps(SQL, "Trivia", Table2)
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
That worked. I was missing the DBUtils.
Now my next question. Does the table have to be loaded every time the app loads?
 
Upvote 0
Top