mardi 7 juillet 2015

Store class in sqlite database

I am trying to figure out how to store a class object in a SQLite database for a c++ project.

From what I have learned online I will need to store the object in a blob field in a SQLite table. What steps do you need to take to perform this? I believe you have to serialize (correct term?) the object to be inserted in the database.

For example, I am trying to convert a program from using an array to a database, where each array[index] is a class object.

Currently being used:

public class Example
{
    private int x;
    Example(int a)
    {
        x=a;
    }
}

main()
{
    Example array[10];
    for(int i=0; i<10; i++)
    {
        array[i] = new Example(i);
    }
}

Similar to how I'd like:

public class Example
{
    private int x;
    Example(int a)
    {
        x=a;
    }
}
main()
{
    sqlite3 *db;
    int rc;
    char *sql;

    rc = sqlite3_open("test.db", &db);
    for(int i=0; i<10; i++)
    {
        sql = "INSERT INTO INFORMATION(ID,DATA) VALUES ('i', new Example(i));"
        rc = sqlite3_exec(db, sql, notused, notused, notused);
    }


}

Is this possible? Or is there something similar to this to store objects in a sqlite database?

Aucun commentaire:

Enregistrer un commentaire