Since I do so much programming with SQL, I decided to try my hand at SQLite. SQLite uses SQL syntax to interact with a file as a database. It is used in millions of applications and software across the world. It is quicker than standard file operations and quicker than algorithms you could write yourself to get data from a file. I've started using it and all the testing so far has been magnificent. SQLite could be used to replace random access files and possibly even INIs. If you already know SQL then you will feel right at home using SQLite. I've kept the function names the same as the ones I'm using in my ODBC project and my MySQL rewrite.
Here is the code I've been using in my tests:
Const SQLITE_CANTOPEN
= 14 Const SQLITE_CONSTRAINT
= 19 Const SQLITE_CORRUPT
= 11 Const SQLITE_INTERNAL
= 2 Const SQLITE_INTERRUPT
= 9 Const SQLITE_MISMATCH
= 20 Const SQLITE_NOTFOUND
= 12 Const SQLITE_PROTOCOL
= 15 Const SQLITE_READONLY
= 8 Const SQLITE_WARNING
= 28
If DB_QUERY
("SELECT * FROM test") = SQLITE_OK
Then Print , GetDataType
(DB_Result
(column
, row
).
type), DB_Result
(column
, row
).columnName
, DB_Result
(column
, row
).value
'If DB_QUERY("INSERT INTO test(column2) VALUES ('And now, a fifth row!');") = SQLITE_OK Then
' Print DB_AffectedRows
'End If
'If DB_QUERY("UPDATE test SET column2 = 'And now, a fourth row!' WHERE column1 = '4';") = SQLITE_OK Then
' Print DB_AffectedRows
'End If
DB_Close
If sqlite3_open
(sqlitedb
, Offset(hSqlite
)) = SQLITE_OK
Then DB_Open
= -1 Else DB_Open
= 0
sqlite3_open sqlitedb
, Offset(hSqlite
)
If sqlite3_prepare
(hSqlite
, sql_command
, Len(sql_command
), Offset(hStmt
), 0) = SQLITE_OK
Then Dim As Long colCount: colCount
= sqlite3_column_count
(hStmt
) ret = sqlite3_step(hStmt)
row = row + 1
For column
= 0 To colCount
- 1 DB_Result
(column
+ 1, row
).
type = sqlite3_column_type
(hStmt
, column
) DB_Result(column + 1, row).columnName = sqlite3_column_name(hStmt, column)
DB_Result(column + 1, row).value = sqlite3_column_text(hStmt, column)
ret = sqlite3_step(hStmt)
'do some error catching
sqlite3_finalize hStmt
If sqlite3_prepare
(hSqlite
, sql_command
, Len(sql_command
), Offset(hStmt
), 0) = SQLITE_OK
Then Dim As Long colCount: colCount
= sqlite3_column_count
(hStmt
) ret = sqlite3_step(hStmt)
DB_QUERY = SQLITE_OK
row = row + 1
For column
= 0 To colCount
- 1 DB_Result
(column
+ 1, row
).
type = sqlite3_column_type
(hStmt
, column
) DB_Result(column + 1, row).columnName = sqlite3_column_name(hStmt, column)
DB_Result(column + 1, row).value = sqlite3_column_text(hStmt, column)
ret = sqlite3_step(hStmt)
ElseIf ret
= SQLITE_DONE
Then DB_QUERY
= SQLITE_OK
Else DB_QUERY
= SQLITE_ERROR
'do some error catching
sqlite3_finalize hStmt
DB_AffectedRows = sqlite3_changes(hSqlite)
GetDataType = "INTEGER"
GetDataType = "FLOAT"
GetDataType = "BLOB"
GetDataType = "NULL"
GetDataType = "TEXT"
sqlite3_close hSqlite
A screenshot of the above code running:
I've attached the database file I'm working with. You can run regular SQL queries (INSERT, UPDATE, DELETE, SELECT, etc.) against it and test it out for yourself. Personally, SQLite will be something I end up incorporating in future projects for myself.