This will probably get mangled by email systems line wrapping everything, but it looks like on the forum it keeps the format. Below is the C code I wrote for basic access of my database. If you have any suggestions, or looks like I missed something, feel free to comment. There is a header file not included here that has an enumeration of error codes, but they are easily replaced and obvious. ``` /* ********************** */ /* SQLite Access routines */ /* ********************** */ #include <stdio.h> #include <ctype.h> #include <errno.h> #include <math.h> #include <stdlib.h> #include <string.h> #include <mem.h> #include <io.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include "sqlite3.h" #define SQLMAXSIZE BUFSIZE /* max number of bytes in a sql statement */ typedef struct { char fn[MAXFILENAMESIZE + 1]; /* name of the database */ char isopen; /* flag whether the database is open */ char exists; /* flag set if database exists on disk */ char sql[SQLMAXSIZE]; /* the sql string */ int sqllen; /* length of the sql string */ sqlite3 *db; /* the opaque sqlite database structure */ sqlite3_stmt *stmt; /* the prepared statement */ int rows; /* number of rows returned by statement */ } TXTDB; TXTDB txtdb; /* the database */ char txtdbtemp[SQLMAXSIZE + 2]; /* temp string */ /* ********************************************************** */ /* open txtdb and init variables. called at start of program */ /* returns 0 if everything OK */ /* returns 1 if file did not exist and was created */ /* this means the database tables need to be created */ /* ********************************************************** */ int txtdb_open(void) { int i; int f; char cflag; /* create flag if set then file does not exist */ if (txtdb.isopen) { write_errfile("Attempt to open txtdb which is already open"); exit(TXTTLS_ERROR_31); } if (txtdb.db) { write_errfile("txtdb.db not null in txtdb_open"); exit(TXTTLS_ERROR_31); } /* first try opening the file to see if it exists */ cflag = 0; f = open(txtdb.fn, O_RDWR | O_BINARY, S_IREAD | S_IWRITE); if (f == -1) { /* if an error occurs then cannot proceed, so write error message */ switch (errno) { case EACCES: write_errfile("txtdb Open Error: Permission Denied"); exit(TXTTLS_ERROR_33); case EINVACC: write_errfile("txtdb Open Error: Invalid Access Code"); exit(TXTTLS_ERROR_33); case EMFILE: write_errfile("txtdb Open Error: Too Many Files Open"); exit(TXTTLS_ERROR_33); case ENOENT: write_errfile("txtdb file does not exist. Creating txtdb"); cflag = 1; /* set flag indicating that file is being created */ break; default: write_errfile("txtdb Open Error: Unknown Error"); exit(TXTTLS_ERROR_33); } } close(f); /* close the handle */ i = sqlite3_open(txtdb.fn, &txtdb.db); /* open the database */ if (i != SQLITE_OK) { sprintf(txtdbtemp, "txtdb Open Error: error during database open %d", i); write_errfile(txtdbtemp); exit(TXTTLS_ERROR_33); } if (!txtdb.db) { write_errfile("txtdb Open Error: txtdb is null after open"); exit(TXTTLS_ERROR_33); } txtdb.isopen = 1; /* set open flag */ txtdb.exists = cflag; /* save the create flag */ return cflag; } /* ************************************ */ /* Close the previously opened database */ /* ************************************ */ void txtdb_close(void) { int i; if (!txtdb.isopen) { if (txtdb.db) { write_errfile("txtdb Close Error: txtdb.db not null and not flagged as open"); exit(TXTTLS_ERROR_34); } write_errfile("txtdb Close Error: txtdb.db is null and not flagged as open"); exit(TXTTLS_ERROR_34); } do { i = sqlite3_close(txtdb.db); if ((i != SQLITE_BUSY) && (i != SQLITE_OK)) { sprintf(txtdbtemp, "txtdb Close Error: error during database Close %d", i); write_errfile(txtdbtemp); exit(TXTTLS_ERROR_34); } } while (i == SQLITE_BUSY); txtdb.isopen = 0; txtdb.db = 0; txtdb.exists = 0; } /* ***************************************************** */ /* does the sqlite3_prepare_v2() with error checking */ /* returns SQLITE_OK if ready for a step. */ /* writes to errfile, but does not exit if sql too long */ /* if sql is too long then returns SQLITE_ERROR */ /* any other error, the error code is returned */ /* **************************************************** */ int txtdb_prepare(char *sql) { int i; txtdb.sqllen = strlen(sql); /* get and check length of sql */ if (txtdb.sqllen > SQLMAXSIZE - 1) { write_errfile("SQL Too Long:"); write_errfile(sql); return SQLITE_ERROR; } strcpy(txtdb.sql, sql); /* save the sql into the structure */ if (!txtdb.isopen) { write_errfile("txtdb prepare without database open"); exit(TXTTLS_ERROR_35); } if (txtdb.db == 0) { write_errfile("txtdb prepare with null database handle"); exit(TXTTLS_ERROR_36); } i = sqlite3_prepare_v2(txtdb.db, sql, (txtdb.sqllen + 1), &txtdb.stmt, 0); return i; } ```