Introduction:
In this how-to article, we will explore how to efficiently process CSV data using SQLite. Subsequently, we'll demonstrate how to access SQLite databases using the USqlStatement class to execute arbitrary SQL commands.Prerequisites:
Before we dive into the process, make sure you have SQLite tool binaries for Windows in place. You can download it from the official website (https://www.sqlite.org/download.html). In this article, we utilize the command-line tool sqlite3.exe.
Step 1: Importing CSV Data into SQLite
To process CSV data using SQL, you need to first import the CSV data into an SQLite database. Follow these steps:Create the import script and store it on drive. In SQLite, you can import a CSV file with tab-separated columns and CRLF-separated rows using the .import command:
Xbase++:
cSql := ""
cSql += '.mode tabs' + CRLF
cSql += '.import input.csv mytable' + CRLF
cSql += '.quit' + CRLF
MemoWrit( "importtable.sql", cSql )
Xbase++:
RunShell( '/C sqlite3.exe file.db ".read "importtable.sql""' )
Step 2: Accessing SQLite Database with the USqlStatement class
Now that your CSV data is in an SQLite database, you can access it using the Xbase++ USqlStatement class. Follow these steps:Attach the SQLite database to the USqlStatement class using the :attachDatabase() class method:
Xbase++:
USqlStatement():attachDatabase( "file.db" )
Xbase++:
oStmt := UsqlStatement():new()
oStmt:fromChar( "select * from mytable limit 5;" )
oStmt:build()
Xbase++:
oStmt:query(USQL_RESULT_OBJECTS, @aData)
FOR n := 1 TO Len( aData )
? Var2Json( aData[n] )
NEXT