[使用libcurl函数传输Sqlite DB文件,但无法立即打开db文件以将数据发送到数据库

问题描述 投票:1回答:1

我正在运行与基于PLC的计算机通信的C ++应用程序。

基于PLC的机器一旦处理了零件,就会创建一个.db(SQLITE)文件。

我编写了一个函数,使用libcurl函数将sqlite db从PLC机器传输到PC。

传输完成后,我需要遍历db文件并将数据传输到公司网络数据库表。

传输完成后,我在遍历文件时遇到问题。

读取时文件抛出异常“(11)SQLITE_CORRUPT”-“磁盘映像格式不正确”。

但是一旦我关闭我的c ++应用程序并再次打开,最后一个sqlite db文件就可以打开并循环通过。

我附上下面的代码,让我知道是否有一种方法可以使通过libcurl库传输的db文件可以读取。我是否应该使用一些额外的命令使其可用。

Information of the ftp server.

char dbfile[128];
char ofile[512];

//Pulling all the information about the ftp server information from parsed.xml -> Request/DataBlock/LogFile/Host, Path, File

char *host = tagTextValue(doc, "REQUEST.DATABLOCK.LOGFILE.HOST");// -- 172.16.1.100
if (host) 
{
    if (char *path = tagTextValue(doc, "REQUEST.DATABLOCK.LOGFILE.PATH")) // -- /pub/export/mdt
    {
        if (char *file= tagTextValue(doc, "REQUEST.DATABLOCK.LOGFILE.FILE"))// --  20200226_004636_90180.db
        {
            sprintf_s( dbfile, sizeof(dbfile), "ftp://%s/%s/%s", host, path, file );//Host Address  
            sprintf_s(ofile, sizeof(ofile), "c:\\xxxxx\\xxxxx\\%s", file);//Destination Address
        }
    }
}

///////////Start of FTP///////////////////
struct FtpFile 
{
  const char *filename;
  FILE *stream;
};

FtpFile ftp = 
{
    ofile, /* name to store the file as if successful */ 
    NULL
};



CURL *url = curl_easy_init();
curl_easy_setopt(url,  CURLOPT_URL, dbfile);// dbfile has the complete path where to pull the file from.

FILE *ffp = fopen(ofile, "w"); // ofile mentions the destination on the receiving end

curl_easy_setopt(url, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(url, CURLOPT_WRITEDATA, &ftp);
curl_easy_setopt(url, CURLOPT_USERPWD, "ftpuser:xxxx");// Passing the login details to FTP server to pull the information from the 'server/machine'

CURLcode  res = curl_easy_perform(url);

fclose(ffp);
curl_easy_cleanup(url);

////////////////End of FTP/////////////////////////

sqlite3 *db;

sqlite3_stmt *stmt;

char sql[1024];

Sleep(20000);

WireBondDataUploadType      rec = rec2;         
const char* sql1 = "select xxxxxxxxxx";
bool storeSuccessful = false;

const char *ofile2 = "c:\\xxxxx\\xxxxxxx\\20200310_021605_20039.db";
int rc;
rc = sqlite3_open(ofile, &db);  //ST Opening a new Database connection

if (rc == SQLITE_OK)
{
    rc=sqlite3_prepare(db, sql1, -1, &stmt, NULL);
    if(rc != 0)
    {
        sqlite3_close(db);
    } 
    else if(rc == SQLITE_OK) 
    {
      //Process the information by reading the db file.
      xxxxxxxx
      xxxxxxxx
    }
}
c++ sqlite ftp libcurl
1个回答
0
投票

解决问题的方法是我们忘记了关闭上述代码中的“打开的” ftp文件(ofile)。

参考链接-[https://curl.haxx.se/libcurl/c/ftpget.html][1]

© www.soinside.com 2019 - 2024. All rights reserved.