sqlite3實現中文路徑sql檔案匯入另類解決方法
阿新 • • 發佈:2019-01-24
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);