sqlite3 problem?

Hi,
I have a problem with sqlite3. The database locks its self when in use this is a problem for me as I will need to access it from a http server as well and don't want to make an API. I looked at sqlite3 documentation and the database is supposed to be unlockable when you are done writing? Anyone know how to do this?. Here is my sqlite3 wrapper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <stdio.h>
#include <Windows.h>
#include <cstring>
#include "Leaf.h"

using namespace Leaf;
Record::Record()
{

}
Record::~Record()
{
    /* Free the columns */
    for (unsigned int i = 0; i < columns.size(); i++)
    {
        if (columns[i] != NULL)
        delete columns[i];
    }
}

Records::Records()
{
    this->nextIndex = 0;
}
Records::~Records()
{
    /* Free the records */
    for (unsigned int i = 0; i < record.size(); i++)
    {
        if (record[i] != NULL)
            delete record[i];
    }
}

void Records::free()
{
    delete this;
}
Record* Records::next()
{
    Record* r = NULL;
    if(this->moreRecordsAvailable())
    {
        r = this->record[nextIndex];
        nextIndex++;
    }
    return r;
}

Record* Records::getRecord(unsigned int recordIndex)
{
    if (record.size() > recordIndex)
        return this->record[recordIndex];
    return NULL;
}

int Records::numOfRecords()
{
    return record.size();
}

unsigned int Records::curIndex()
{
    return this->nextIndex - 1;
}
bool Records::moreRecordsAvailable()
{
    if(this->nextIndex < record.size())
    {
        return true;
    }
    return false;
}
Column* Record::getColumn(std::string colName)
{
    for (unsigned int i = 0; i < columns.size(); i++)
    {
        if (columns[i] != NULL)
        {
            if (columns[i]->colName == colName)
                return columns[i];
        }
    }

    return NULL;
}

LeafDatabase::LeafDatabase()
{
    zErrMsg = 0;
}

LeafDatabase::~LeafDatabase()
{

}

bool LeafDatabase::open(const char* filename)
{
    rc = sqlite3_open(filename, &db);
    if ( rc )
    {
        strcpy(zErrMsg, sqlite3_errmsg(db));
        return false;
    }

    return true;
}

bool LeafDatabase::execute(const char* query)
{
    rc = sqlite3_exec(db, query, NULL, NULL, &zErrMsg);
    if (rc != SQLITE_OK)
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
        return false;
    }
    return true;

}

unsigned int LeafDatabase::getInt(const char* query, int column)
{
    if (sqlite3_prepare_v2(db, query, -1, &stmt, 0) == SQLITE_OK)
    {
        if (sqlite3_step(stmt) == SQLITE_ERROR)
                printf("SQL ERROR %s\n", query);
            else
                return sqlite3_column_int(stmt, column);
    }
    else
        printf("SQL ERROR %s\n", query);

    sqlite3_finalize(stmt);
    return 0;
}

unsigned int LeafDatabase::getInt(const wchar_t* query, int column)
{
    if (sqlite3_prepare16_v2(db, query, -1, &stmt, 0) == SQLITE_OK)
    {
        if (sqlite3_step(stmt) == SQLITE_ERROR)
                printf("SQL ERROR %ls\n", query);
            else
                return sqlite3_column_int(stmt, column);
    }
    else
        printf("SQL ERROR %ls\n", query);

    sqlite3_finalize(stmt);
    return 0;
}

Records* LeafDatabase::getRecords(const char* query, Records* custom_rs)
{
    Records* rs = NULL;
    Record* r = NULL;
    Column* c = NULL;
    const void* dataP = NULL;

    if (sqlite3_prepare_v2(db, query, -1, &stmt, 0) == SQLITE_OK)
    {
        if (custom_rs == NULL)
            rs = new Records();
            else
                rs = custom_rs;
        rs->query = query;
        rs->record.resize(0);
        rs->db = this;
        int cols = sqlite3_column_count(stmt);
        int result;
        do
        {
            result = sqlite3_step(stmt);
            if (result == SQLITE_ROW)
            {
                r = new Record();
                for (int i = 0; i < cols; i++)
                {
                    c = new Column();
                    c->colName = sqlite3_column_name(stmt, i);
                    c->data.datai = sqlite3_column_int(stmt, i);
                    c->data.datal = sqlite3_column_int64(stmt, i);
                    dataP = sqlite3_column_text(stmt, i);
                    memcpy((void*)c->data.datac, (void*) dataP, 512);
                    r->columns.push_back(c);
                }
                rs->record.push_back(r);
            }
        } while(result == SQLITE_ROW);

    }

    sqlite3_finalize(stmt);
    return rs;
}

char* LeafDatabase::getErrMsg()
{
    return this->zErrMsg;
}

void LeafDatabase::close()
{
    sqlite3_close(db);
}
bump
What is the actual problem/error you are having? I think it will probably involve how you are actually using your wrapper.
I explained above the database is locked once used. According to sqlite documentation the database is supposed to unlock when you are done writing. Weather you have to call a particular function to unlock it I do not know. Any advice?
Last edited on
You may have to call a function to commit the transaction. This would be something I'd hope their documentation covered.
By default, SQLite is transacted at the statement level. If you don't create a transaction yourself, other processes should be able to access the database when you have it open and not using it.
Could you further explain this please?
Topic archived. No new replies allowed.