使用SQLite3和MPI生成不同的数据库时,数据库被锁定

时间:2016-05-06 13:17:01

标签: sqlite mpi

我想使用SQLite3MPI生成许多数据库,不同的进程会生成和处理不同的数据库。但是当我在Linux中运行该程序时,我收到来自database is locked sqlite3_exec()的错误消息。我知道这个消息意味着我试图从同一个数据库连接同时与数据库做不兼容的事情。显然,我没有同时做不兼容的事情。 SQLite3的版本是3.11.0。那么如何处理这种情况呢?提前谢谢!

这是我的简单代码:

#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;
    {
        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;
    {
        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);
    }

    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;
        }

}

0 个答案:

没有答案