本质上这是这里问题的 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();
}
关于c++ - 使用 Qt/C++ 确定 Google Drive 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18649631/