Android Question Export SQL Lite to TEXT File

MarkusR

Well-Known Member
Licensed User
pseudo code
B4X:
    Dim sql1 As SQL = ... open database
 
    Dim rs As ResultSet = sql1.ExecQuery2("SELECT * FROM TableName WHERE PhotoId = ?",Array As String(PhotoId))
 
    Do While rs.NextRow

   'build a string here with the fields rs.GetString(index)
    'and use a sub around to replace the delimiter char
    'read sql docu
      
   Loop
 
    rs.Close
 
    '... Close database
sql/resultset docu access all field names
https://www.b4x.com/android/help/sql.html#resultset

read "[B4X] Smart String Literal"
https://www.b4x.com/android/forum/threads/b4x-smart-string-literal.50135/

file
https://www.b4x.com/android/help/files.html

save csv
https://www.b4x.com/android/help/stringutils.html


i believe u can get all tables names with a query.
 
Last edited:

Mashiane

Expert
Licensed User
Why do you need a database converted into a text file
As an example, for my needs, I need to embed an existing sqlite db as a JSON text string for 'read' only access to a webapp im using, for example, banano without having to re-create the whole thing.

I however have been experimenting with sql.js in this instance. #just regressing.
 

RB Smissaert

Well-Known Member
Licensed User
Hi Everyone,

Do we have clear sample for sql lite to text file format?

Thanks,
Maloyski
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 'needs to be in the loop, to add new array at every iteration
  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
Needs a reference to StringUtils.


RBS
 

Marlou Fin

Member
Licensed User
Hi Guys,

Already have idea for csv with delimiter. My issue now if do we have some sample to export data from my sqlite to text format fix length.

Thanks,
Marlou
 

MarkusR

Well-Known Member
Licensed User
u need to process this data later?
if not u can count each column size and fill it with spaces.
i not saw any field size commands in the sql class but maybe u can get the field size with a query.
 

RB Smissaert

Well-Known Member
Licensed User
u need to process this data later?
if not u can count each column size and fill it with spaces.
i not saw any field size commands in the sql class but maybe u can get the field size with a query.
select max(length(field1)) from table1

Works on integer and real (double) data as well.

RBS
 
Top