1. 程式人生 > >sqlite3實現中文路徑sql檔案匯入另類解決方法

sqlite3實現中文路徑sql檔案匯入另類解決方法

1.需求

程式要實現產品離線資料下載,故採用java端直接拼接sql檔案,客戶端下載sql檔案,合併到本地資料庫。效能還行區域網十幾秒7兆多,但是存在一個sqlite或sql檔案儲存在中文路徑下無法合併的問題

2.解決

主要原因是因為sqlite開啟資料庫時使用utf8開啟,qt普遍採用unicode編碼。網上方法較多,這裡採用另外一種方法解決,採用相對路徑避開中文路徑的問題

3.程式碼片段

1.建立資料庫
    QString strdbpath(QDir::currentPath() + "/db");
    QDir dir("");
    dir.mkpath(strdbpath);
    strdbpath += "/mysqlite.db"
; QSqlDatabase dbset = QSqlDatabase::addDatabase("QSQLITE", "file"); dbset.setDatabaseName(strdbpath); if (!dbset.open()) { LOG_ALL_ERROR(QStringLiteral("failed open mysqlite.db")); return bret; }
2.合併sql檔案到本地資料庫
std::string strstdcmd("sqlite3.exe ./db/mysqlite.db \".read ./db/mysqlite.sql\""
); QProcess process; process.start(strstdcmd.c_str()); process.waitForFinished(); String strsqlpath(QDir::currentPath() + "/db/mysqlite.sql"); QFile::remove(strsqlpath);
3.下載sql檔案部分略用libcurl很容易也很快下載檔案

4.備註

1.程式碼在win7+vs2010+qt5.40+libcurl 7.36.0下編譯通過
2.編譯或直接下載sqlite3.exe在生成目錄和原始碼目錄分別存放一份
3.常識currentPath在編譯時是指原始碼目錄,直接執行exe指exe所在目錄