Hi all,

I would like to use SQLite3 and MPI to generate many databases, and different 
processes produce and process different databases. But when I ran the program 
in Linux, I got an error message from sqlite3_exec() that ?database is locked?. 
I know this message means I tried to do incompatible things with a database at 
the same time from the same database connection. Obviously, I did not do 
incompatible things with a database at the same time. I have Googled this error 
message, but none of these web pages is about SQLite3 and MPI. The version of 
SQLite3 is 3.11.0. I am new to sqlite, and any help and information will be 
greatly appreciated! Thank you in advance!


Here is my simple code which can reproduce this error:


#include <mpi.h>
#include <iostream>
#include "sqlite3.h"
#include <fstream>
#include <sstream>

using namespace std;

int testFunc(int firstBreakPt, int secondBreakPt, char* saveFile);

int main(int argc, char* argv[])
{
    MPI_Init(&argc,&argv);
    char *saveFile = "test";
    int breakPt[2][2]={{300,500},{600,900}};
    int myid, numprocs, taskNum = 2;
    int paraSent[2];
    MPI_Status status;
        MPI_Comm_rank(MPI_COMM_WORLD,&myid);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    if (myid == 0)  // master;
    {
        // sending parameters to slaves;        MPI_Bcast(saveFile, 
sizeof(saveFile), MPI_CHAR, 0, MPI_COMM_WORLD);
        for(int i =0; i<taskNum; i++)
        {
            paraSent[0] = breakPt[i][0];
            paraSent[1] = breakPt[i][1];
            MPI_Send(&paraSent[0], 2, MPI_INT, i+1, i+1, MPI_COMM_WORLD);
        }
    }   
    else    // slaves;
    {
        // receive parameters from master;         MPI_Recv(&paraSent[0], 2, 
MPI_INT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
                int firstBreakPt, secondBreakPt;
        firstBreakPt = paraSent[0];
        secondBreakPt = paraSent[1];
        testFunc(firstBreakPt, secondBreakPt, saveFile);    // generate and 
process database;
    }
    MPI_Finalize();
    return 0;
}

int testFunc(int firstBreakPt, int secondBreakPt, char* saveFile)
{
    // convert int to string;
    ostringstream temp1;
    temp1<<firstBreakPt;
    string firstBreakPtStr = temp1.str();
        ostringstream temp2;
    temp2<<secondBreakPt;
    string secondBreakPtStr = temp2.str();
    string fileNameTemp = saveFile; // convert char* to string;
    string fileName = fileNameTemp + "_" + firstBreakPtStr + "_" + 
secondBreakPtStr + ".db";
    cout<<"fileName is: "<< fileName <<endl;    
       // open a new db;
    sqlite3* dropTableDB;
    cout << "sqlite3_open("", &dropTableDB): " << sqlite3_open( 
fileName.c_str(), &dropTableDB) << endl;
        if(dropTableDB == 0)
        {
            printf("\nCould not open database.");
            return 1;
        }
        char *errmsg;
        int result;
               // DROP TABLE PrimaryForLateralDistribution;
        result = sqlite3_exec ( dropTableDB,
                                "Drop TABLE IF EXISTS 
PrimaryForLateralDistribution",  // stmt
                                0,
                                0,
                                &errmsg
                );
        if ( result != SQLITE_OK )
        {
            cout << "\nCould not prepare statement: Drop TABLE: " << result << 
endl;
            cout << "errmsg: " << errmsg << endl;
           return 1;
        }
}

Reply via email to