how to load extensions in sqlite3? - c

I'm trying to load regexp.c extension in order to add REGEXP operator into sqlite. What I have done:
Download sqlite3 amalgamation file
Found regexp.c loadable extension
Created test project in VS2015
#include <stdio.h>
#include "stdafx.h"
#include "sqlite3.h"
const char* SQL = "SELECT load_extension('sqlite3.obj', 'sqlite3_regexp_init');";
int main(int argc, char **argv) {
sqlite3 *db = 0; // the hande of connection object to db
char *err = 0;
char *errMsg = "unable to load extension";
// open connection
if (sqlite3_open("my_cosy_database.dblite", &db))
fprintf(stderr, "Error during opening/creation DB: %s\n", sqlite3_errmsg(db));
/*else if (sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, 0))
{
fprintf(stderr, "Cannot enable extensions loading: %s\n", sqlite3_errmsg(db));
}*/
else if (sqlite3_enable_load_extension(db, 1))
{
fprintf(stderr, "Cannot enable extensions loading: %s\n", sqlite3_errmsg(db));
}
// execute SQL
else if (sqlite3_load_extension(db, "sqlite3.obj", "sqlite3_regexp_init", &errMsg))
{
fprintf(stderr, "Cannot load sqlite3_regexp_init extension: %s\n", sqlite3_errmsg(db));
}
else if (sqlite3_exec(db, SQL, callback, 0, &err))
{
fprintf(stderr, "Ошибка SQL: %sn", err);
sqlite3_free(err);
}
// close connection
sqlite3_close(db);
return 0;
}
Official documentation says: "For security reasons, extension loading is turned off by default. In order to use either the C-language or SQL extension loading functions, one must first enable extension loading using the sqlite3_db_config(db,SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,1,NULL) C-language API in your application." So this is what I've done. The problem is that I got every time sqlite3_load_extension or sqlite3_exec is called. The same problem appears with commented sqlite3_enable_load_extension and uncommented sqlite3_db_config. I have no idea what causes this exception and there is no clear example of C api usage (even in 'Using Sqlite' book). One thing i could find out is the statement which causes this exception: "(pCtx->pFunc->xSFunc)(pCtx, pCtx->argc, pCtx->argv);/ IMP: R-24505-23230 */" in the SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe p / The VDBE */).
So please, help.

Related

How to fix : Sql logic error sqlite3 in Clion?

I'm trying to link sqlite3 library to CMakeList in Clion using the following code:
find_package(SQLite3)
target_link_libraries(IIWProject SQLite::SQLite3)
Library has been loaded but when i Run the code, Clion shows the following error:
SQL logic error
The database has been loaded but when sqlite3_prepare_v2(db, query, -1, &stmt, NULL) was called return SQL logic error
void get_db(sqlite3 **db){
int rc;
if ((rc = sqlite3_open("db_project.db", db)) != SQLITE_OK){
//fprintf(stderr, "Failed to open DB.\n");
fprintf(stderr,"Failed to open DB: %s\n\r", sqlite3_errstr(rc));
exit(EXIT_FAILURE);
}
}
int clear_table(sqlite3 *db){
sqlite3_stmt *stmt;
int rc;
char* query = "DELETE FROM resources";
if ((rc=sqlite3_prepare_v2(db, query, -1, &stmt, NULL)) != SQLITE_OK){
fprintf(stderr,"Failed to prepare statement: %s\n\r", sqlite3_errstr(rc));
return 1;
}
if ((rc = sqlite3_step(stmt)) != SQLITE_DONE){
fprintf(stderr,"Delete failed: %s\n\r", sqlite3_errstr(rc));
return 1;
}
return 0;
}
How can I try to fix it?
EDIT:
If I compile, without Clion manually, with gcc -lsqlite3,it work
Now that we've established that the table doesn't exist when you run your program though your IDE...
The current working directory when you run your program through a command line is different than the current working directory your IDE runs it in. Since you're using a relative path to the database file, this means you're using a different one depending on how you run your program. Only one of the databases actually has the table(s) you're trying to use.
Some solutions:
Use an absolute path to the database.
Configure your IDE to use the same working directory as when you're running your program through a command line.

Failed to execute sqlite3 in C application

I'm trying to display history of firefox and chrome using C Application .For the history of chrome , it works well because the Sql command doesn't contain symbols but with chrome My sql request contains symbols , So this function doesn't give me result .It give me error :
no such table :moz_historyvisits
Or ,when i test this request from the command prompt it works well . What i think that the problem is the sql request contains (_)symbols .
Rq : For chrome history it works well . CallbackFirefox is to function to display result
I'm using windows and Code blocks as an IDE . When i execute select name from sqlite_master
int DisplayFirefoxHistory()
{
sqlite3 *db;
char *err_msg = 0;
system("cd C:/Users/******/AppData/Roaming/Mozilla/Firefox/Profiles/*.default");
int rc = sqlite3_open("places.sqlite", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "Cannot open database: %s\n",sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
char *sql = "select url,datetime(visit_date/1000000-11644473600,'unixepoch') from moz_historyvisits,moz_places order by visit_date desc";
rc = sqlite3_exec(db, sql, callbackFirefox, NULL, &err_msg);
if (rc != SQLITE_OK ) {
fprintf(stderr, "Failed to select data\n");
fprintf(stderr, "SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return 1;
}
sqlite3_close(db);
return 0;
}
system("cd C:/Users/******/AppData/Roaming/Mozilla/Firefox/Profiles/*.default");
system() starts a separate shell that is terminated after the command was executed, so this does not change the current directory of your actual program.
You should find out the directory name manually, and include it in the database file name.

undefined reference to sqlite_open

I know there have been questions asked before about this problem but none seem to shine a light on my problem which is, I am trying to compile a C application and want to access SQLite from within the code (as per test app below) using Eclips as a compile and debugging environment.
I know the .h files are being accessed. the code has as many lines commented out to do with iostream as I have tried to compile this as a C++ app as well.
I get errors one for each of 2 the SQL API.
The real question is do I have to set and How do I set a dependency in Eclipse to allow the api to resolve. Thanks
the code
#include <sqlite3.h>
int main()
{
int RetVal;
RetVal = OpenDB();
return RetVal;
}
int OpenDB()
{
sqlite3 *db; // database connection
int rc; // return code
char *errmsg; // pointer to an error string
/*
* open SQLite database file test.db
* use ":memory:" to use an in-memory database
*/
rc = sqlite3_open(":memory:", &db); //fails on this line
if (rc != SQLITE_OK)
{
goto out;
}
/* use the database... */
out:
/*
* close SQLite database
*/
sqlite3_close(db); //fails on this line
return 0;
}
You need to link the sqlite3 library along with your program:
gcc main.c -lsqlite3

Problems using WNetGetConnection

According to Microsoft this function should retrieve the name of the network resource associated with a local device.
I am using an adaptation of the code on this Microsoft page, compiling in C. But even though the drives ("J:", and "X:") are mapped on my PC, this function does not return the share for either of them. Rather it results in either ERROR_NOT_CONNECTED or ERROR_CONNECTION_UNAVAIL.
Note: As a double check, to eliminate the possibility that I was bumping up against UAC, or some other access problem, I called WNetGetConnection(,,) from within MS Excel using a macro with code from this site. It worked just fine.
My exact C code implementation is below.
Any ideas?
My adaptation using ANSI C, built and run on Windows 7, (must include mpr.lib)
#include <windows.h>
#include <ansi_c.h>
LPTSTR szDeviceName[260]; //MAX_PATHNAME_LEN
DWORD dwResult, cchBuff = sizeof(szDeviceName);
void main(int argc, char *argv[])
{
// Call the WNetGetConnection function.
dwResult = WNetGetConnection(argv[1],
(LPTSTR) szDeviceName,
&cchBuff);
switch (dwResult)
{
//
// Print the connection name or process errors.
//
case NO_ERROR:
printf("Connection name: %s\n", szDeviceName);
break;
//
// The device is not a redirected device.
//
case ERROR_NOT_CONNECTED:
printf("Device %s is not connected.\n", argv[1]);
break;
//
// The device is not currently connected, but it is a persistent connection.
//
case ERROR_CONNECTION_UNAVAIL:
printf("Connection to %s is unavailable.\n", argv[1]);
break;
//
// Handle the error.
//
default:
printf("WNetGetConnection failed.\n");
}
getchar();
}

Using MariaDB in C

I'm trying to connect to a MariaDB database in a C script and I can't find the necessary documentation. I installed libmariadbclient-dev, but I couldn't find any accompanying documentation such as a man page. There's a basic description and limited documentation here, but the documentation only includes descriptions of functions. The fact is, despite having scoured all sorts of Google results, I don't even know what to import to get this to work, much less how to use it. Is there any guide or documentation on how to use a MariaDB database in C?
The MariaDB Client Library for C has exactly the same API as the MySQL
Connector/C for MySQL 5.5
Here it is: http://dev.mysql.com/doc/refman/5.5/en/c-api-function-overview.html
Another one:
http://zetcode.com/db/mysqlc/
You can compile a minimal test like
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "root_pswd",
NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "CREATE DATABASE testdb"))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_close(con);
exit(0);
}
using
gcc -o mysql-test mysql-test.c $(mysql_config --libs)

Resources