### Export each Record to single Textfile

This is an example of exporting each record from a SQLite Database table to a textfile.
Each record will be exported to a textfile "nr.txt" whereas nr is a counter starting with 0.
Database and Table
The example Database use is called anyitems.db with table anyitems:
SQL: CREATE TABLE [anyitems] ([ID] INTEGER PRIMARY KEY, [Subject] TEXT, [Description] TEXT);
Example Export Sub assigned to a Button Action
' Export table anyitems using sql and go over the resultset.
' Each record will be exported to a textfile nr.txt whereas nr is a counter starting with 0.
' SQL: 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`
Notes
gMsg.Show is a simple MessageDialog Form (Class Module)

Changed: , Category: SQLite, File: T6C254M1821.htm, ID: 1821