本质上,这是问题的Qt / C++变体:
How do I programmatically locate my Google Drive folder using C#?
我到目前为止已经尝试过的内容如下
QString gDrivePath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
gDrivePath += "\\Google\\Drive\\sync_config.db";
bool result = QFile::copy(gDrivePath, "gdrive.db");
QFile gdrivefile("gdrive.db");
if(!gdrivefile.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::information(0, "Google Drive path read error", gdrivefile.errorString());
}
QTextStream gin(&gdrivefile);
gin.setCodec("ASCII");
while(!gin.atEnd()) {
qDebug() << gin.readLine().toLocal8Bit();
}
gdrivefile.close();
就像上面链接的问题中给出的那样,该代码复制了db文件,并尝试逐行读取它。唯一的问题是,它正在从文件中跳过包含所需的“local_sync_root_pathvalue”的数据块
有任何想法吗 ?
最佳答案
我最终使用来自qt的sql类来实现这一壮举。
码:
QString gDrivePath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
gDrivePath += "\\Google\\Drive\\sync_config.db";
QFile::copy(gDrivePath, "gdrive.db");
QFile gDriveFile("gdrive.db");
if(!gDriveFile.open(QIODevice::ReadOnly)) {
qDebug() << "Error in opening Google Drive File" << gDriveFile.errorString();
}
else {
QSqlDatabase gDrivedb = QSqlDatabase::addDatabase("QSQLITE");
gDrivedb.setDatabaseName("gdrive.db");
if (gDrivedb.open()) {
QSqlQuery query(gDrivedb);
if (query.exec("select * from data where entry_key='local_sync_root_path'")) {
while (query.next()) {
QString gDrivePath = query.value(2).toString().remove(0,4);
qDebug() << "Google Drive at - " << gDrivePath;
}
} else {
qDebug() << "Error in querying Google Drive db" << query.lastError();
}
}
else
qDebug() << "Error in Opening Google Drive db";
gDriveFile.close();
}