Hi,
I have CSV-Files to load into SQLite DB, I have made some helper functions reading the header line in an array and data in maps (line by line). Those CSV-Files normaly have the same contense, but I have to handle older (less fields=no problem) and newer with more fields as in the DB.
If i could scan a array independend from upper or lower or scan a list in same way, live would be easier ;-)
Does some have some hints how to get it faster, robuster or even more elegant ?
I am missing array functions like IndexOfIgnoreCase() ... in lists too ;-)
PS: it works and I hope on faster devices even fast enough
My Galaxy S+ is a little bit slow
I have CSV-Files to load into SQLite DB, I have made some helper functions reading the header line in an array and data in maps (line by line). Those CSV-Files normaly have the same contense, but I have to handle older (less fields=no problem) and newer with more fields as in the DB.
If i could scan a array independend from upper or lower or scan a list in same way, live would be easier ;-)
Does some have some hints how to get it faster, robuster or even more elegant ?
B4X:
Sub FixFelderCSV(cDB As String) ' cDB ist the name of the table in the DB.
Dim cur As Cursor
Dim DBFields As List
Dim x, nMax As Int
Dim sSQL As StringBuilder
Dim sCMD, sFeld As String
Try
cur = Main.SQL1.ExecQuery( "SELECT * FROM " & cDB & " LIMIT 1" )
DBFields.Initialize
nMax = cur.ColumnCount-1
' read all the fieldnames in LowerCase
For x = 0 To nMax
sFeld = cur.GetColumnName(x).Trim.ToLowerCase
DBFields.Add( sFeld )
Next
For x = 0 To nFieldCount-1 ' CSV fieldnames
sFeld = sHeader(x).Trim.ToLowerCase
If DBFields.IndexOf( sFeld ) = -1 Then
sSQL.Initialize
sSQL.Append( "ALTER TABLE ")
sSQL.Append( cDB )
sSQL.Append( " ADD COLUMN ")
sSQL.Append( sFeld )
sSQL.Append( " TEXT DEFAULT '' ;" )
Log ("New field" & cDB & " " & sFeld )
sCMD = sSQL.ToString
Log ("WRITE " & sCMD )
Try
Main.SQL1.ExecNonQuery( sCMD )
Log( "OK ! " & sCMD )
Catch
Log( "Fehler ! " & sCMD )
Log( LastException.Message )
Msgbox("Error '" & sFeld & "' not in LG !","Structure error")
ExitApplication
End Try
End If
Next
cur.close
Catch
Log( "Error CSV header " )
Log( LastException.Message )
Msgbox("Error CSV header ","Structure error")
ExitApplication
End Try
End Sub
I am missing array functions like IndexOfIgnoreCase() ... in lists too ;-)
PS: it works and I hope on faster devices even fast enough
My Galaxy S+ is a little bit slow