Android Question export sql table data to csv file

gjoisa

Active Member
Licensed User
Longtime User
Is there a ready template to export sql table data to csv file . I could not find in dbutils .
 

DonManfred

Expert
Licensed User
Longtime User
No there is no template. As the structure is based on your query you need to do it by yourself.
 
Upvote 0

gjoisa

Active Member
Licensed User
Longtime User
this code does not work > No file is created .
B4X:
Sub btnecsv_Click
    Dim mlist As List
    mlist.Initialize
    mlist=DBUtils.ExecuteMemoryTable(Main.SQL,"SELECT ScripName,Date,TradeType,Price,Qty FROM TBook1",Null,0)
    Log("mlist" & mlist)
    Dim su As StringUtils
    su.SaveCSV(File.DirInternal,"MyStocks.csv","|",mlist)
End Sub
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Is there a ready template to export sql table data to csv file . I could not find in dbutils .

Something like this should do it:

B4X:
Sub Cursor2CSV(Cursor1 As Cursor, strFolder As String, strCSVFileName As String)
 Dim i As Long
 Dim c As Long
 Dim lstCSV As List
 Dim UB As Long
 Dim UB2 As Int
 UB = Cursor1.RowCount - 1
 UB2 = Cursor1.ColumnCount - 1
 Dim arrFields(UB2 + 1) As String
 lstCSV.Initialize
 Cursor1.Position = 0
 For c = 0 To UB2
  arrFields(c) = Cursor1.GetColumnName(c)
 Next
 lstCSV.Add(arrFields)
 For i = 0 To UB
  Dim Cols(UB2 + 1) As String
  Cursor1.Position = i
  For c = 0 To UB2
   Cols(c) = Cursor1.GetString2(c)
   If Cols(c) = Null Then 'to avoid an error at su.SaveCSV
    Cols(c) = ""
   End If
  Next
  lstCSV.Add(Cols)
  Next
 sUtils.SaveCSV(strFolder, strCSVFileName, ",", lstCSV)
End Sub


RBS
 
Upvote 1

gjoisa

Active Member
Licensed User
Longtime User
Something like this should do it:

B4X:
Sub Cursor2CSV(Cursor1 As Cursor, strFolder As String, strCSVFileName As String)
 Dim i As Long
 Dim c As Long
 Dim lstCSV As List
 Dim UB As Long
 Dim UB2 As Int
 UB = Cursor1.RowCount - 1
 UB2 = Cursor1.ColumnCount - 1
 Dim arrFields(UB2 + 1) As String
 lstCSV.Initialize
 Cursor1.Position = 0
 For c = 0 To UB2
  arrFields(c) = Cursor1.GetColumnName(c)
 Next
 lstCSV.Add(arrFields)
 For i = 0 To UB
  Dim Cols(UB2 + 1) As String
  Cursor1.Position = i
  For c = 0 To UB2
   Cols(c) = Cursor1.GetString2(c)
   If Cols(c) = Null Then 'to avoid an error at su.SaveCSV
    Cols(c) = ""
   End If
  Next
  lstCSV.Add(Cols)
  Next
 sUtils.SaveCSV(strFolder, strCSVFileName, ",", lstCSV)
End Sub


RBS
This worked fine . Thank you very much .
 
Upvote 0
Top