Would it be possible to include functions to access SQLite database (and/or
another database) from within tinyfugue? I have included a patch which
includes some basic support for this, and a brief description of the
functions follows.
Thanks,
Mark
Functions:
dbopen(filename)
- Opens a database.
- Returns the database handle if successful.
- Returns -1 if failed.
dbclose(db_handle)
- Closes a previously opened database.
- Returns 0 if successful.
- Returns -1 if an invalid handle is given.
dbprepare(db_handle, query)
- Sets up a prepared statement (SQL query) for later execution.
- Returns -1 if an invalid database handle is given.
- Returns -2 if there is another problem with the query.
- Otherwise returns a handle to the prepared statement for later use.
- SQL statements here can contain question marks '?' which can have
values bound to them later using dbbind().
dbstep(statement_handle)
- Executes a prepared statement, returning a row at a time.
- Columns for each row are stored in the variables D0-D9, then DA-DZ
if there are more than 10 columns returned. If more than 36
columns are returned, further columns are discarded.
- Return values:
0 - Row returned
1 - Done. No more rows to return.
2 - Busy. Database may be locked by another process. Try again
later. No rows have been returned.
-1 - Invalid statement handle given
-2 - Called incorrectly. E.g. after all rows returned and before
a reset or if the database has been closed.
-255 - Other error. Error message is printed.
dbbind(statement_handle, value, value, ...)
- Binds one or more values to a statement. If you bind too many
values, an error occurs. If you bind too few, the remaining ones
are null.
Example of a statement to bind values to:
SELECT * FROM tablename WHERE id=? and name=?;
- Return values:
0 - Success
-1 - Invalid statement handle given
-2 - Tried to bind too many paramaters
-3 - Called incorrectly. Perhaps the statement was finalized or
database closed.
-4 - Out of memory. Shouldn't happen.
-255 - Other error. Error message is printed.
dbreset(statement_handle)
- Resets a prepared statement so it can be executed again.
- Note: dbstep will automatically reset the statement once all rows
have been returned, but this is still useful if you don't want to
fetch any further rows and just want to start again.
- Returns 0 if successful, -1 if an invalid handle is given.
dbfinalize(statement_handle)
- Finalizes (closes/removes) a prepared statement when you have
finished using it.
- Sort of akin to dbclose() but for prepared statements.
- Returns 0 if successful, -1 if an invalid handle is given.
Example code:
/let db_handle=$[dbopen('filename.db')]
/let st_handle=$[dbprepare(db_handle, "SELECT * FROM tablename")]
/while (dbstep(st_handle) == 0) \
/echo %D0 %D1 %D2%;\
/done
/let retval=dbfinalize(st_handle)
/let retval=dbclose(db_handle)
Or, with binding variables:
/let db_handle=$[dbopen('filename.db')]
/let st_handle=$[dbprepare(\
db_handle, "SELECT * FROM tablename WHERE id=?")]
/test db_bind(st_handle, 3)
/while (dbstep(st_handle) == 0) \
/echo %D0 %D1 %D2%;\
/done
/let somevar=4
/test db_bind(st_handle, somevar)
; Note - no need to call dbreset as this is done automatically when all
; rows are returned in the previous run through.
/while (dbstep(st_handle) == 0) \
/echo %D0 %D1 %D2%;\
/done
/test dbfinalize(st_handle)
/test dbclose(db_handle)
Nobody/Anonymous
None
None
Public
|
Date: 2007-10-29 16:32
|
|
Date: 2007-10-29 16:23
|
| Filename | Description | Download |
|---|---|---|
| sqlite-patch.diff | Sqlite Patch | Download |
| Field | Old Value | Date | By |
|---|---|---|---|
| File Added | 235323: sqlite-patch.diff | 2007-07-02 10:08 | mivok |
Copyright © 2009 Geeknet, Inc. All rights reserved. Terms of Use