Jose's Read Only Forum 2023

Archive => Archived Posts => Topic started by: José Roca on June 01, 2011, 12:31:06 AM

Title: SQLite v. 3.7.6.3 Headers
Post by: José Roca on June 01, 2011, 12:31:06 AM
 
About SQLite

SQLite is a small C library that implements a self-contained, embeddable, zero-configuration SQL database engine. Features include:


The SQLite distribution comes with a standalone command-line access program (sqlite) that can be used to administer an SQLite database and which serves as an example of how to use the SQLite library.

SQLite website: http://www.sqlite.org/

The attached file contains my translation of the SQLite headers to PowerBASIC.
Title: SQLite v. 3.7.6.3 Example: Creating a database
Post by: José Roca on June 01, 2011, 12:31:54 AM
 
The following example demonstrates how to create a database using SQLite.


' ########################################################################################
' Sqlite example
' Creates a dabase.
' ########################################################################################

' SED_PBCC
#COMPILE EXE
#DIM ALL
#INCLUDE "WINDOWS.INC"
#INCLUDE "SQLITE3.INC"

$TEST_DATABASE = "TESTDB.SQLITE"

' ========================================================================================
' Returns an string describing in English the error condition for the most recent
' sqlite3_* API call.
' ========================================================================================
FUNCTION SQL3_ErrMsg (BYVAL hSqlite AS DWORD) AS STRING
   LOCAL pErrMsg AS ASCIIZ PTR
   IF hSqlite = %NULL THEN EXIT FUNCTION
   pErrMsg = sqlite3_errmsg(hSqlite)
   FUNCTION = @pErrMsg
END FUNCTION
' ========================================================================================

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN () AS LONG

   LOCAL hr AS LONG
   LOCAL hLite AS DWORD
   LOCAL szDbName AS ASCIIZ * %MAX_PATH
   LOCAL szSQL AS ASCIIZ * 256
   LOCAL pTable AS DWORD PTR
   LOCAL cRows AS LONG
   LOCAL cCols AS LONG
   LOCAL pErrMsg AS ASCIIZ PTR

   ' Delete the database if already exists
   szDbName = $TEST_DATABASE
IF DIR$(szDbName) <> "" THEN
KILL szDbName
END IF

   ' Create the database
   hr = sqlite3_open(szDbName, hLite)
   IF hr <> %SQLITE_OK OR hLite = %NULL THEN
      PRINT "Error " & STR$(hr) & " creating the database"
      IF hLite THEN sqlite3_close(hLite)
      EXIT FUNCTION
   END IF

' Create a table
   szSQL = "CREATE TABLE Table1 (ID INT, Name TEXT, Age TEXT)"
   hr = sqlite3_get_table(hLite, szSQL, pTable, cRows, cCols, pErrMsg)
   IF hr <> %SQLITE_OK THEN
      PRINT @pErrMsg
      sqlite3_free(pErrMsg)
      GOTO Terminate
   END IF

   ' Discard the result set
   if pTable THEN sqlite3_free_table (pTable)

   PRINT "Database created"

Terminate:

   ' Close the database
   IF hLite THEN sqlite3_close(hLite)

   WAITKEY$

END FUNCTION
' ========================================================================================