### Export Table to Textfiles

This is an example of exporting a SQLite Table.
Each record is exported to a Textfile in an Exportfolder.
The sub is assigned to a button btnExport.
In case of an error, a message is shown using a MessageDialog (own form).
' Export table anyitems using sql and going over the resultset of a select statement.
' Each record will be exported to a textfile nr.txt whereas nr is a counter starting with 0.
' SQL Table: CREATE TABLE [anyitems] ([ID] INTEGER PRIMARY KEY, [Subject] TEXT, [Description] TEXT);
`Sub btnExport_Action
Log("btnExport :: Start")
If lvwItems.Items.Size = 0 Then
gMsg.Show("Export all Items", "

## No items found.

Add items first.")
Return
End If
'
File.MakeDir(File.DirApp, "Export")
'
' Select all records using query
Dim cur As ResultSet
Try
cur = gSQL.ExecQuery2("SELECT * FROM " & CTABLETODOS & " ORDER BY Subject", Null)
Catch
Log(LastException.Message)
gMsg.Show("Error export", LastException.Message)
Return
End Try
' Go over the resultset record-by-record and save to file in export folder
Dim i As Int = 0
Do While cur.NextRow
' Create the export text file
Try
Dim tw As TextWriter
tw.Initialize(File.OpenOutput(CEXPFOLDER, i & ".txt", False))
tw.WriteLine(cur.GetString("Subject"))
tw.WriteLine(cur.GetString("Description"))
tw.Flush
tw.Close
lblInformation.Text = "Item exported: " & lvwItems.SelectedItem
Catch
gMsg.Show("Error export", LastException.Message)
Log(LastException.Message)
If tw.IsInitialized Then tw.Close
Return
End Try
i = i + 1
Loop
cur.Close
lblInformation.Text = "All Items exported (#" & lvwItems.Items.Size & ")"
Log("All Items exported (#" & lvwItems.Items.Size & ")")
Log("btnExport :: End")
End Sub`

Changed: , Category: Strings, File: T6C255M1847.htm, ID: 1847