Pelles C forum

Pelles C => General discussions => Topic started by: tbohon on April 14, 2011, 04:53:47 pm

Title: SQLite Example
Post by: tbohon 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
Title: Re: SQLite Example
Post by: TimoVJL 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 (http://forum.pellesc.de/index.php?topic=3110.0)

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

http://milky.manishsinha.net/2009/03/30/sqlite-with-c/ (http://milky.manishsinha.net/2009/03/30/sqlite-with-c/)
Title: Re: SQLite Example
Post by: czerny 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"
Title: Re: SQLite Example
Post by: frankie 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)
Title: Re: SQLite Example
Post by: TimoVJL 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.
Title: Re: SQLite Example
Post by: czerny 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
Title: Re: SQLite Example
Post by: Grincheux 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
Title: Re: SQLite Example
Post by: Grincheux on March 18, 2015, 08:17:21 pm
I forgot the headers files and the library