Creating temporary table while still accessing other tables - c++

I've got 2 or more databases ATTACHed to one SQLite database connection. Each database consists of 3 tables. To have better access for searching/filtering, I create a huge temporary table over all tables and databases like this:
"CREATE TEMPORARY TABLE temp_table AS "
"SELECT * FROM pb.name_table "
"LEFT JOIN pb.phone_table ON (pb.name_table.id=pb.phone_table.id) " \
"LEFT JOIN pb.email_table ON (pb.name_table.id=pb.email_table.id) " \
"UNION SELECT * FROM name_table " \<br>
"LEFT JOIN phone_table ON (name_table.id=phone_table.id) " \
"LEFT JOIN email_table ON (name_table.id=email_table.id);";
If something changes in a table, I have to recreate the temporary table. With an increasing amount of data, creating a temporary table takes some time, but since I'm having continuous read access to the table, my idea was as follows:
Create a thread, which creates a second temp table in background
Block access for the reading clients
DROP first temp table
Rename the second temp table
The problem is now: Creating a temporary table is a write access to the database, which blocks automatically all reading threads also.
Has anyone a good idea how I can handle this? I need read access while recreating the temporary table.

As long as all threads are part of the same program, you have control over all database connections.
So you can write the data in a completely separate database, and ATTACH is quickly later.

Thanks a lot for your answer. I changed my code and found out that I need a new database connection (sqlite3_open) in the working thread not to block the other thread. Also creating a TEMPORARY TABLE in the attached "temporary database" was not possible, because creating a temporary table doesn't allow a qualifier (like: x.temp_table), so I had to create a real table which consumes a lot of memory in our flash file system (which is not allowed).
But wait! I've got an idea
(2 hours later)
I did it! I open an empty database and attach alll relevant databases to the connection. I create a temporary table "in" the empty database which consumes no memory in flash, because it's temporary.
When I have to create a new table, I open another empty database and attach the relevant databases to the connection. When the operation is finished, I switch the old and the new connection. Code as follows:
#include <iostream>
#include "dbAccess.h"
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
bool inProgress = true;
DWORD WINAPI createTempTableThread(void *param);
int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
cout << "*";
return 0;
}
int main(void)
{
sqlite3* db = NULL;
HANDLE hThreadHandle = NULL;
CdbAccess *dba = new CdbAccess();
int i = 0;
db = dba->dbaConnect();
dba->dbaSetDatabase(db);
cout << "INFO: Creating initial temporary table. " << endl;
sqlite3_exec(dba->dbaGetDatabase(), "CREATE TEMPORARY TABLE temp_table AS " \
"SELECT * FROM pb.name_table " \
"LEFT JOIN pb.phone_table ON (pb.name_table.id=pb.phone_table.id) " \
"LEFT JOIN pb.email_table ON (pb.name_table.id=pb.email_table.id) " \
"UNION SELECT * FROM intern.name_table " \
"LEFT JOIN intern.phone_table ON (intern.name_table.id=intern.phone_table.id) " \
"LEFT JOIN intern.email_table ON (intern.name_table.id=email_intern.table.id);", NULL, NULL, NULL);
cout << "INFO: Creating initial temporary table finished. " << endl;
while(1)
{
hThreadHandle = CreateThread(0, 0, createTempTableThread, dba, 0, 0);
while(inProgress)
{
sqlite3_exec(dba->dbaGetDatabase(), "SELECT * FROM name_table WHERE id LIKE 1;", callback, NULL, NULL);
}
for(i = 0; i < 5; i++)
{
sqlite3_exec(dba->dbaGetDatabase(), "SELECT * FROM name_table WHERE id LIKE 2;", callback, NULL, NULL);
}
inProgress = true;
CloseHandle(hThreadHandle);
}
dba->dbaDisconnect();
return 0;
}
CdbAccess::CdbAccess()
{
hSemaphore = CreateSemaphore(NULL, 1, 1, 0);
}
CdbAccess::~CdbAccess()
{
}
sqlite3 *CdbAccess::dbaConnect()
{
sqlite3 *db;
static int num = 1;
int err = SQLITE_OK;
string attach = "ATTACH DATABASE \"";
string internal = "cbInternal.db";
if(num == 1)
{
cout << endl << "INFO: cbTemp1.db";
err = sqlite3_open("cbTemp1.db", &db);
num = 2;
}
else
{
cout << endl << "INFO: cbTemp2.db";
err = sqlite3_open("cbTemp2.db", &db);
num = 1;
}
if(err == SQLITE_OK)
{
cout << endl << "INFO: Temp database opened.";
err = sqlite3_exec(db, "ATTACH DATABASE \"cbInternal.db\" AS intern;", NULL, NULL, NULL);
if(err == SQLITE_OK)
{
cout << endl << "INFO: Internal database attached.";
err = sqlite3_exec(db, "ATTACH DATABASE \"0123456789.db\" AS pb;", NULL, NULL, NULL);
if(err == SQLITE_OK)
{
cout << endl << "INFO: Phone book attached.";
}
else
{
cout << endl << "ERROR: Attaching phone book: " << sqlite3_errmsg(db);
}
}
else
{
cout << endl << "ERROR: Attaching internal database: " << sqlite3_errmsg(db);
}
}
else
{
cout << endl << "ERROR: Opening database: " << sqlite3_errmsg(db);
}
return db;
}
int CdbAccess::dbaDisconnect(void)
{
int err = SQLITE_OK;
err = sqlite3_exec(db, "DETACH DATABASE pb;", NULL, NULL, NULL);
if(err == SQLITE_OK)
{
cout << endl << "INFO: Phone book detached.";
err = sqlite3_exec(db, "DETACH DATABASE intern;", NULL, NULL, NULL);
if(err == SQLITE_OK)
{
cout << endl << "INFO: Internal database detached.";
err = sqlite3_close(db);
if(err == SQLITE_OK)
{
cout << endl << "INFO: Database connection closed.";
}
else
{
cout << endl << "ERROR: Could not close database: " << sqlite3_errmsg(db);
}
}
else
{
cout << endl << "ERROR: Could not detach internal database: " << sqlite3_errmsg(db);
}
}
else
{
cout << endl << "ERROR: Could not detach phone book: " << sqlite3_errmsg(db);
}
return err;
}
sqlite3* CdbAccess::dbaGetDatabase(void)
{
return db;
}
void CdbAccess::dbaSetDatabase(sqlite3 * sqldb)
{
db = sqldb;
}
int CdbAccess::dbaGetTempTableAccess(void)
{
cout << endl << "INFO: Access requested.";
WaitForSingleObject(hSemaphore, INFINITE);
return 0;
}
int CdbAccess::dbaReleaseTempTableAccess(void)
{
cout << endl << "INFO: Access released.";
ReleaseSemaphore(hSemaphore, 1, NULL);
return 0;
}
DWORD WINAPI createTempTableThread(void *param)
{
int err = SQLITE_OK;
CdbAccess *d = (CdbAccess *)param;
sqlite3 *db;
cout << endl << "INFO: createTempTable: IN";
inProgress = true; // global variable for test porpose only
db = d->dbaConnect();
if(db != NULL)
{
cout << endl << "Thread: INFO: Creating temporary table. ";
err = sqlite3_exec(db, "CREATE TEMPORARY TABLE temp_table AS " \
"SELECT * FROM pb.name_table " \
"LEFT JOIN pb.phone_table ON (pb.name_table.id=pb.phone_table.id) " \
"LEFT JOIN pb.email_table ON (pb.name_table.id=pb.email_table.id) " \
"UNION SELECT * FROM intern.name_table " \
"LEFT JOIN intern.phone_table ON (intern.name_table.id=intern.phone_table.id) " \
"LEFT JOIN intern.email_table ON (intern.name_table.id=intern.email_table.id);", NULL, NULL, NULL);
}
if(err != SQLITE_OK)
{
cout << endl << "Thread: ERROR: Creating temporary table: " << sqlite3_errmsg(db);
}
else
{
cout << endl << "Thread: INFO: Creating temporary table finished. ";
}
d->dbaSetDatabase(db);
inProgress = false; // global variable for test porpose only
cout << endl << "Thread: INFO: createTempTable: OUT";
return 0;
}

Related

sqlite transaction, database table is locked (c++)

I don't understand why this code shows me:
database table is locked
when I try to delete the *_old table. I use this code to change the type of a table column dynamically.
This is my code:
sqlite3_exec(this->connection, "BEGIN TRANSACTION;", NULL, NULL, NULL);
string query = "ALTER TABLE " + tableName + " RENAME TO " + tableName + "_old;";
cout << "8 " << query << endl;
if(sqlite3_exec(this->connection, query.c_str(), NULL, NULL, NULL) != SQLITE_OK)
error = 1;
query = "CREATE TABLE " + tableName + " (";
for(int i = 0; i < (int)colName.size(); i++)
{
query += colName.at(i) + " " + colType.at(i);
for(int j = 0; j < (int)primaryKey.size(); j++)
{
if(strcmp(colName.at(i).c_str(), primaryKey.at(j).c_str()) == 0)
{
query += " PRIMARY KEY ";
break;
}
}
query += " " + colParam.at(i);
if(i < ((int)colName.size() - 1))
query += ", ";
}
query += ");";
cout << "9" << query << endl;
if(sqlite3_exec(this->connection, query.c_str(), NULL, NULL, NULL) != SQLITE_OK)
error = 1;
query = "INSERT INTO " + tableName + " SELECT * FROM " + tableName + "_old;";
cout << "10" << query << endl;
if(sqlite3_exec(this->connection, query.c_str(), NULL, NULL, NULL) != SQLITE_OK)
error = 1;
query = "DROP TABLE " + tableName + "_old;";
cout << "11" << query << endl;
if(sqlite3_exec(this->connection, query.c_str(), NULL, NULL, NULL) != SQLITE_OK)
error = 1;
cout << sqlite3_errmsg(this->connection) << endl;
if(error > 0)
{
query = "ROLLBACK TRANSACTION;";
cout << "12" << query << endl;
if(sqlite3_exec(this->connection, query.c_str(), NULL, NULL, NULL) != SQLITE_OK)
error = 1;
}
else
{
query = "COMMIT TRANSACTION;";
cout << "13" << query << endl;
if(sqlite3_exec(this->connection, query.c_str(), NULL, NULL, NULL) != SQLITE_OK)
error = 1;
}
I solved, there was some previous queries with open statement, I use this code to close all before drop the table
sqlite3_stmt *stmt=0;
while((stmt=sqlite3_next_stmt(this->connection,0))!=0)
{
sqlite3_finalize(stmt);
}

sqlite3: Table schema not copying from main database to second database

I am attempting to copy the same table schema and a select number of entries from my main database to a new secondary database. I am able to create the second database START.sql and inside it, a table called copied, however the schema between that and the main masterDatabaseTest differ and as a result I receive the error:
table START.copied has 1 columns but 5 values were supplied
The code:
void newLog(std::string tableName, std::string timeStart, std::string timeEnd)
{
char *err_msg = NULL;
int rc= -1;
std::string dbLogName = timeStart;
std::string dbLogName2 = dbLogName + ".sql";
std::string attachQuery = "ATTACH DATABASE 'START.sql' AS 'START';";
std::string copyTable = "CREATE TABLE START.copied AS SELECT sql FROM sqlite_master WHERE type='table' and name='masterDatabaseTest';";
std::string insertCopied = "INSERT INTO START.copied SELECT * from sqlite_master where type='table' and name='masterDatabaseTest';";
std::string detach = "DETACH DATABASE START;";
std::cout << dbLogName << "\n"
<< attachQuery << "\n"
<< copyTable << "\n"
<< insertCopied << "\n"
<< detach << std::endl;
sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, 0);
rc = sqlite3_open(databaseName.c_str(), &db); // CREATES DATABASE
std::cout << "OPEN: " << rc << std::endl;
rc = sqlite3_exec(db, attachQuery.c_str(), NULL, 0, &err_msg);
std::cout << "ATTACH: " << rc << std::endl;
if(rc != 0){
std::cout << err_msg << std::endl;
}
rc = sqlite3_exec(db, copyTable.c_str(), NULL, 0, &err_msg);
std::cout << "copyTable: " << rc << std::endl;
if(rc != 0){
std::cout << err_msg << std::endl;
}
rc = sqlite3_exec(db, insertCopied.c_str(), NULL, 0, &err_msg);
std::cout << "insertCopied: " << rc << std::endl;
if(rc != 0){
std::cout << err_msg << std::endl;
}
rc = sqlite3_exec(db, detach.c_str(), NULL, 0, &err_msg);
std::cout << "detach: " << rc << std::endl;
if(rc != 0){
std::cout << err_msg << std::endl;
}
sqlite3_exec(db, "END TRANSACTION;", NULL, NULL, 0);
}
inside commandline sqlite3 database.sql:
sqlite> .schema
CREATE TABLE masterDatabaseTest (ID INTEGER,RECORDTIME BIGINT,TYPE INTEGER,TIMESTAMP BIGINT,ENCODER1 INTEGER,ENCODER2 INTEGER,ENCODER3 INTEGER,ENCODER4 INTEGER,ENCODER5 INTEGER);
inside commandline sqlite3 START.sql
sqlite> .schema
CREATE TABLE copied(sql TEXT);
If you want "an exact copy of the schema of masterDatabaseTest", you do not need to use sqlite_master. The DDL query
CREATE TABLE START.copied AS SELECT * from masterDatabaseTest
will accomplish that. If you want a select number of entries, add a WHERE clause. If you do not want any rows from the source table, add a WHERE 0; it will create the schema and will not select any rows.
I have figured out my problem :)
the db instance was not pointing to the correct database, so although I was able to execute it correctly in commandline, the actual program has no initialised the database connection so the sqlite3_exec command was pointing to nothing.
Fixing that I was able to execute the commands with no problem.

SQLite, SELECT and max function

Why does the following code gives me the different result when using max function? I thought that it should return SQLITE_DONE in this case too.
#include <boost/scope_exit.hpp>
#include <sqlite3.h>
#include <cstdlib>
#include <iostream>
int main()
{
sqlite3* db;
int rc = sqlite3_open(":memory:", &db);
BOOST_SCOPE_EXIT_ALL(&db)
{
sqlite3_close(db);
};
if (rc != SQLITE_OK)
{
std::cerr << "Can't open database."
<< " Error code: " << rc
<< " Error description: " << sqlite3_errmsg(db);
return EXIT_FAILURE;
}
char* errMsg;
rc = sqlite3_exec(db, "CREATE TABLE foo (bar INTEGER)", NULL, NULL, &errMsg);
if (rc != SQLITE_OK)
{
std::cerr << "Can't create \"foo\" table."
<< " Error code: " << rc
<< " Error description: " << errMsg;
sqlite3_free(errMsg);
return EXIT_FAILURE;
}
{
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2(db, "SELECT bar FROM foo WHERE bar = 1", -1, &stmt, NULL);
if (rc != SQLITE_OK)
{
std::cerr << "Can't prepare SELECT statement."
<< " Error code: " << rc
<< " Error description: " << sqlite3_errmsg(db);
return EXIT_FAILURE;
}
BOOST_SCOPE_EXIT_ALL(&stmt)
{
sqlite3_finalize(stmt);
};
rc = sqlite3_step(stmt);
std::cout << rc << std::endl; // 101 -- SQLITE_DONE
}
{
sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2(db, "SELECT max(bar) FROM foo WHERE bar = 1", -1, &stmt, NULL);
if (rc != SQLITE_OK)
{
std::cerr << "Can't prepare SELECT statement."
<< " Error code: " << rc
<< " Error description: " << sqlite3_errmsg(db);
return EXIT_FAILURE;
}
BOOST_SCOPE_EXIT_ALL(&stmt)
{
sqlite3_finalize(stmt);
};
rc = sqlite3_step(stmt);
std::cout << rc << std::endl; // 100 -- SQLITE_ROW
}
}
Thanks in advance.
When you are using GROUP BY, you get one group for each unique value of the grouped column(s).
If there does not exist any row to be grouped, there are no groups, and the query does not return any rows.
When you are using an aggregate function like max() without GROUP BY, the entire table becomes a single group.
This happens even if the table is empty, i.e., you then get a single group that aggregates over the empty set.
If you don't want to get a result when there are not bar = 1 rows, add GROUP BY bar.

Null pointer sqlite3 handle after passed to function call

When i use a function in c++ to init sqlite3, when it comes out of the function the handle is Null. Any idea what might cause this? I simply hand the pointer over as a parameter. If I move the open to the main function it works fine. What happens that causes this? Is something hidden and going out of scope?
#include <iostream>
#include "sqlite3.h"
using namespace std;
int init_table(sqlite3 *dbH, string db_name)
{
if (sqlite3_open(db_name.c_str(), &dbH) != SQLITE_OK)
{
cout << "Failed to open DB : " << sqlite3_errmsg(dbH) << endl;
abort();
}
else
{
cout << "Opened database: " << db_name << endl;
}
if (sqlite3_exec(dbH, "PRAGMA synchronous = OFF", NULL, NULL, NULL) != SQLITE_OK)
{
cout << "Failed to set synchronous: " << sqlite3_errmsg(dbH) << endl;
}
if (sqlite3_exec(dbH, "PRAGMA journal_mode = WAL", NULL, NULL, NULL) != SQLITE_OK)
{
cout << "Failed to set journal mode: " << sqlite3_errmsg(dbH) << endl;
}
cout << "dbH 2: " << dbH << endl;
}
int main()
{
sqlite3 * dbH;
dbH = NULL;
cout << "dbH 1: " << dbH << endl;
string dbName = "foo1.db";
init_table(dbH, dbName);
cout << "dbH 3: " << dbH << endl;
}
And when Run
$ ./a.out
dbH 1: 0
Opened database: foo1.db
dbH 2: 0x5baa048
dbH 3: 0
Should it be
int init_table(sqlite3 **dbH, string db_name)
And pass pointer to pointer?
May be it has no problem with sqliter handling. It is either you pass pointer as reference or as pointer to pointer.
Ofcourse, while passing, You need to pass &dbH to the init_table after modification.

sqlite c++ program values get inserted only first time second time they don't

when i try to insert values everything works fine for the first time but second time it doesn't take values,what should i do.?
after creating table i try to insert values which works fine,now second time again if i try to insert it doesn't take values
also when i want to select it is only displaying 1st column that is user id but i want the whole column to be displayed
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
#include "sqlite3.h"
int main (int argc, const char * argv[]) {
int ch;
int userid;
string name;
string sName;
int rc;
sqlite3 *db;
sqlite3_open("custom1.db", & db);
string createQuery = "CREATE TABLE IF NOT EXISTS items3 (uid INTEGER primary key ,name TEXT);";
std::stringstream insertQuery;
std::stringstream selectQuery;
std::stringstream removeQuery;
while(ch!=5){
cout<<endl<<"1:create table"<<endl<<"2:insert data"<<endl<<"3:select data"<<endl<<"4:remove"<<endl<<"5:exit"<<endl;
cout<<"enter choice"<<endl;
cin>>ch;
switch(ch) {
case 1 :
sqlite3_stmt *createStmt;
cout << "Creating Table Statement" << endl;
sqlite3_prepare(db, createQuery.c_str(), createQuery.size(), &createStmt, NULL);
cout << "Stepping Table Statement" << endl;
if (sqlite3_step(createStmt) != SQLITE_DONE) cout << "Didn't Create Table!" << endl;
break;
case 2 :
sqlite3_stmt *insertStmt;
cout << "Creating Insert Statement" << endl;
cout<<"userid:";cin>>userid;cout<<"name:";cin>>name;
insertQuery << "INSERT INTO items3 (uid,name)"
" VALUES (" << userid << ", '" << name<<"')";
sqlite3_prepare(db, insertQuery.str().c_str(), insertQuery.str().size(), &insertStmt, NULL);
cout << "Stepping Insert Statement" << endl;
if (sqlite3_step(insertStmt) != SQLITE_DONE) cout << "Didn't Insert Item!" << endl;
sqlite3_reset(insertStmt);
sqlite3_close(db);
break;
case 3:
sqlite3_stmt *selectStmt;
cout << "Creating select Statement" << endl;
cout<<"userid:";
cin>>userid;
selectQuery<<"select * from items3 where uid="<<userid;
rc= sqlite3_prepare(db, selectQuery.str().c_str(), selectQuery.str().size(), &selectStmt, NULL);
cout << "Stepping select Statement" << endl;
while (sqlite3_step(selectStmt) == SQLITE_ROW) {
sName = (char*)sqlite3_column_text(selectStmt, 0);
// Obj.Display(sName); //<== this is not display
cout << "userid" << sName << endl;
}
sqlite3_step(selectStmt);
if (sqlite3_step(selectStmt) != SQLITE_DONE) cout << "Didn't Select Item!" << endl;
else
cout << "Success!" << endl;
sqlite3_close(db);
break;
case 4 :sqlite3_stmt *removeStmt;
cout<<"creating remove statement"<<endl;
cout<<"userid:";
cin>>userid;
removeQuery<<"delete from items3 where uid="<<userid;
sqlite3_prepare(db,removeQuery.str().c_str(),removeQuery.str().size(),&removeStmt,NULL);
cout<<"stepping remove statement"<<endl;
if(sqlite3_step(removeStmt)!=SQLITE_DONE)
cout<<"didn't remove item!"<<endl;
else
cout<<"success"<<endl;
sqlite3_close(db);
break;
}
}
return 0;
}
Because you close the database connection after each insert.
int main (int argc, const char * argv[])
{
...
sqlite3 *db;
sqlite3_open("custom1.db", & db);
...
while(ch!=5)
{
...
switch(ch) {
...
case 2:
sqlite3_stmt *insertStmt;
...
if (sqlite3_step(insertStmt) != SQLITE_DONE)
cout << "Didn't Insert Item!" << endl;
sqlite3_reset(insertStmt);
sqlite3_close(db); // <-- ERROR HERE
break;
You open the database connection once at the program start, so you you should close the database connection once at the program end.
And to echo what Emil said, for God's sake indent your code properly! You might have spotted this yourself if your code wasn't such a mess.