Hello, The reason why im asking for this is becouse im currently making a project that needs data storage.
And sqlite seems to be perfect for that matter.

I've already read the documentation of Sqlite3 but i found that insufficient.
It didn't really explain much besides the obvious.

I can really use some tutorials that explain me everything.

Especially the callback funciton which seems to be really important.

In a nutshell i need to be able to get data from the database and store it in variables.

Any advice would be highly apriciated.

Thanks in advance,

Recommended Answers

All 14 Replies

Have you made an attempt at all?

Yes I've made various attempts.

Show your code here, some explanation of your problems, and we may be able to help.

@rubberman
I've already tried it the code worked it printed out the contents of the table.

But the documentation didn't explain how to store that data in variables.

It really lacked that information.
All im really asking for is a alternative documentation or instructions of waht the functions do and what i can use them for.

Like a tutorial.

Which documentation did you read?

One thing I can tell you about daniweb is that the replies you receive are usually directly proportional to the information you give, and the effort you put in.

Let me explain what im looking for.

Not a how to but a guide to understanding Sqlite3.
The steps you took to understand it so i can interpret that to my understanding.

Did you read this site? From the looks of it you need to create a connection with sqlite3_open() then pass that connection with a sql string to sqlite3_prepare() and it will give you a statement. You can use that statement with sqlite3_step() to step through each row of the results and use sqlite3_column() to grab each column from the row.

I've already been able to get that far.
But i really need ot know what exacly the callback funciton does.

And how to store data from the Database into a Variable.

The callback function is used to store the results of the query into a 2d char array. This explains how to use the execute function and what the parameters are.

There are subdivision of SQLite interfaces which are Stable, Experimental and Deprecated. The Stable interfaces are maintained indefinitely in a backwards compatible way. The second one , that is the Experimental interfaces are subject to change and/or removal at any time. And the Deprecated interfaces cannot be used in the new code and might be removed in some of the future releases.

I found a wrapper called Kompex that seems to work.
But i can't get it to compile and i also cant seem to find out the library's name to add to g++.
Heres my current compile syntax.

g++ main.cpp -o sqlite3_test -lpthread -lsqlite3 -L/usr/lib -lkompex-sqlite-wrapper

Result

terminate called after throwing an instance of 'Kompex::SQLiteException'
Aborted (core dumped)

Current Code

#include <iostream>
#include <kompex/KompexSQLiteDatabase.h>
#include <kompex/KompexSQLiteStatement.h>

int main(int argc, char* argv[])
{
    //Open database
    Kompex::SQLiteDatabase *pDatabase = new Kompex::SQLiteDatabase("Test.db", SQLITE_OPEN_READWRITE, 0);

    //Create statement for sql queries/statements
    Kompex::SQLiteStatement *pStmt = new Kompex::SQLiteStatement(pDatabase);

    //Create table and insert some data
    pStmt->SqlStatement("CREATE TABLE users                 \
                        (                                   \
                         id INTEGER NOT NULL PRIMARY KEY,   \
                         uid VARCHAR(50) NOT NULL);");

    pStmt->SqlStatement("INSERT INTO users (uid) VALUES ('502520')");

    pStmt->Sql("SELECT * FROM users;");
    while(pStmt->FetchRow())
    {
        std::cout << "uid: " << pStmt->GetColumnString(1) << std::endl;
    }
    pStmt->FreeQuery();
   return 0;
}

That's strange!
Are you saying that compiling the program causes the wrapper-library to throw an exception?? That doesn't seem at all right!

From what you've posted, it looks more like your code compiled just fine and that you're getting a crash at runtime, due to an unhandled exception thrown by a call to one of the library functions.

So one of the lines of code in your program is calling a function in the wrapper library with one or more bad parameters, causing it to throw an exception.

I recommend recompiling your code using the -g flag (which makes gcc/g++ generate debug info) and run your program through a debugger like gdb in order to determine exactly which function call is causing the exception to be thrown.

Once you've located the offending line of code, it might be worth enclosing it in a try..catch block, in order to allow your program to more gracefully handle any exceptions thrown by the library.

It might also be worth taking a look at whatever documentation comes with the library you are using (if any) to determine why the exception is being thrown at that part of your code and what you can do to avoid it.

Another thing of note in your code is that you have not performed any checks on any of the pointers which point to objects that you are dynamically allocating via calls to new. So you are taking it for granted that the creation of a new object is succeeding every time and that your pointers are all pointing to valid objects.... Which isn't necessarily the case!

So it could be a bad pointer being passed to one of the library functions which is causing an exception to be thrown.

Before using or dereferencing any pointer, you always need to be sure that what it points to is valid.

e.g.

// Attempt to create a new database
if (Kompex::SQLiteDatabase *pDatabase = new Kompex::SQLiteDatabase("Test.db", SQLITE_OPEN_READWRITE, 0))
{
    // We now know we have a valid pointer to the database
    //Create statement for sql queries/statements
    if(Kompex::SQLiteStatement *pStmt = new Kompex::SQLiteStatement(pDatabase))
    {
        // We now know we have a valid pointer to a statement
        // etc.
    }
    else
    {
        // failed to create statement
    }
}
else
{
    // failed to create database
}

One other thing occurs to me:
Do you have a sqlite3 database called "Test.db" already set up? Or are you expecting the call to Kompex::SQLiteDatabase to create a new database for you?
If the answer to the above questions are "No" and "Yes" respectively, then that could very well be where your problem lies!

This is pure conjecture on my part, but it seems quite likely that the call to Kompex::SQLiteDatabase at line 8 is trying to connect to a non-existent database and is returning a null or invalid pointer. And if it is not this call that is throwing the exception, it will be the call to Kompex::SQLiteStatement at line 11, where you are blindly passing the unchecked, invalid pointer created at line 8!

It might be worth checking the documentation for the library to see what functionality it possesses and whether it can create a new database, or merely connect to an existing one. Otherwise, you need to ensure that a database called "Test.db" already exists before running your program!

EDIT:
From taking a quick look at the Kompex documentation, if you want to create and open a new database, you should be using the flag combo SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE. If all of my previous assumptions about your problem are correct, using that flag combo at line 8 should hopefully fix your problem.

But I still recommend that you check all pointers before using them!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.