NO

Author Topic: SQLite Example  (Read 26862 times)

tbohon

  • Guest
SQLite Example
« on: April 14, 2011, 04:53:47 PM »
I've looked but am unable to find on this board so ...

Do we have, somewhere, a simple-but-complete example of accessing an SQLite database using PellesC?  I'd like to see examples of creating a database, accessing it for read, accessing it for write and closing it.  I don't need a step-by-step-in-exquisite-painful-detail but any hints/idea/suggestions would be most appreciated.

Tnx.

Tom

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: SQLite Example
« Reply #1 on: April 16, 2011, 07:47:33 AM »
Small SQLite3 example:
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"

#pragma lib "SQLite3.lib"

static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
NotUsed = 0;
int i;
for (i = 0; i < argc; i++)
{
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}

int main(int argc, char **argv)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;

rc = sqlite3_open("test.db3", &db);
if (rc == SQLITE_OK)
{
rc = sqlite3_exec(db,
"CREATE TABLE test (id INTEGER NOT NULL, text VARCHAR(100))"
, callback, 0, &zErrMsg);
if (rc != SQLITE_OK) fprintf(stderr, "SQL error: %s\n", zErrMsg);
rc = sqlite3_exec(db,
"INSERT INTO test VALUES (1, 'text1')"
, callback, 0, &zErrMsg);
if (rc != SQLITE_OK) fprintf(stderr, "SQL error: %s\n", zErrMsg);
rc = sqlite3_exec(db,
"SELECT * FROM test"
, callback, 0, &zErrMsg);
if (rc != SQLITE_OK) fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_close(db);
} else {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
return 0;
}
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3.h"

#ifdef _WIN64
#pragma lib "SQLite364.lib"
#else
#pragma lib "SQLite3.lib"
#endif

int __cdecl main(int argc, char **argv)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;

rc = sqlite3_open("test2.db3", &db);
if (rc == SQLITE_OK)
{
rc = sqlite3_exec(db, "CREATE TABLE IF NOT EXISTS test (id INTEGER NOT NULL PRIMARY KEY, text VARCHAR(100))", NULL, 0, &zErrMsg);
if (rc != SQLITE_OK) fprintf(stderr, "SQL error: %s\n", zErrMsg);
rc = sqlite3_exec(db, "INSERT INTO test VALUES (1, 'text1')", NULL, 0, &zErrMsg);
if (rc != SQLITE_OK) fprintf(stderr, "SQL error: %s\n", zErrMsg);

sqlite3_stmt *stmt;
rc = sqlite3_prepare_v2(db, "SELECT * FROM test", -1, &stmt, 0);
if (rc == SQLITE_OK) {
int nCols = sqlite3_column_count(stmt);
if (nCols)
{
for (int nCol = 0; nCol < nCols; nCol++)
printf("%s\t", sqlite3_column_name(stmt, nCol));
printf("\n");
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
for (int nCol = 0; nCol < nCols; nCol++)
printf("%s\t", sqlite3_column_text(stmt, nCol));
printf("\n");
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
} else {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
return 0;
}
Another example:
http://forum.pellesc.de/index.php?topic=3110.0

SQLite API
http://www.sqlite.org/quickstart.html
http://www.sqlite.org/cintro.html

http://milky.manishsinha.net/2009/03/30/sqlite-with-c/
« Last Edit: April 17, 2011, 01:43:06 PM by timovjl »
May the source be with you

czerny

  • Guest
Re: SQLite Example
« Reply #2 on: December 10, 2014, 04:04:44 PM »
I would like to use SQLite in a __stdcall environment. SQLite is compiled with __cdecl.
The functions in sqlite3.h are not declared explicit as _cdecl anymore. So, what can I do?

SQLITE_VERSION  "3.8.7.4"

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: SQLite Example
« Reply #3 on: December 11, 2014, 10:35:55 AM »
Use the new #pragma default_convention of version 8.00 to define convention for the whole header.
Code: [Select]
#pragma default_convention(push, __cdecl)
#include "sqlite3.h"
#pragma default_convention(pop)
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: SQLite Example
« Reply #4 on: December 11, 2014, 11:09:17 AM »
Another way is create own modified version with this code.
Code: [Select]
#include <stdio.h>
#include <string.h>

/*
SQLITE_API SQLITE_EXTERN const char sqlite3_version[];
SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
SQLITE_API void sqlite3_interrupt(sqlite3*);
SQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory;
*/

int main(int argc, char **argv)
{
FILE *fi, *fo;
char buf[4096], tmp[4096];
int iCmt = 0;

fi = fopen("sqlite3.h", "r");
fo = fopen("sqlite3c.h", "w");
while(fgets(buf, sizeof(buf), fi)) {
if (!iCmt && buf[0] == '/' && buf[1] == '*') iCmt = 1;
if (!strncmp(buf, "SQLITE_API", 10)) {
char *p;
int nlen = strlen(buf);
if (buf[nlen-3] == ']' || buf[11] == 'S') {
fputs(buf, fo);
continue;
}
p = strstr(buf, "sqlite3_");
nlen = (int)p-(int)buf;
if (nlen < 12)
p = strstr(p+1, "sqlite3_");
nlen = (int)p-(int)buf;
strncpy(tmp, buf, nlen);
tmp[nlen] = 0;
strcat(tmp, "__cdecl ");
strcat(tmp, p);
fputs(tmp, fo);
} else
if (!iCmt) fputs(buf, fo);
if (iCmt && buf[0] == '*' && buf[1] == '/') iCmt = 0;
}
fclose(fi);
fclose(fo);
return 0;
}
callbacks are not supported with this code.
« Last Edit: December 11, 2014, 03:44:52 PM by TimoVJL »
May the source be with you

czerny

  • Guest
Re: SQLite Example
« Reply #5 on: December 11, 2014, 11:31:52 AM »
Thank you Timo and Frankie,

I will use the hard way (though Timo has made the hard part) because I have to use Pelles C 7.

czerny

Grincheux

  • Guest
Re: SQLite Example
« Reply #6 on: March 18, 2015, 08:16:10 PM »
Here is an example of using SqLite64
The fiirst example is for extracting datas
and the second is for initializig and exiting SQLite

Grincheux

  • Guest
Re: SQLite Example
« Reply #7 on: March 18, 2015, 08:17:21 PM »
I forgot the headers files and the library