yemerra
yemerra

Reputation: 1322

Qt: DB connection won't open

I am trying to get a cookie out of my cookie-db of firefox. However, for some reason the database won't open.

QString tgc;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("C:\Users\myaccount\AppData\Roaming\Mozilla\Firefox\Profiles\234f25fs.default\cookies.sqlite");
bool ok = db.open();
if (!ok)
{
    // qDebug() << "Error: connection with database fail";
}
else
{
    QSqlQuery query("SELECT value WHERE name='TGC' FROM moz_cookies");
    if (query.next())
    {
        tgc = query.value(0).toString();
    }
}
db.close();
return tgc;

However, db.open() returns false. What are the possible reasons for that?

Upvotes: 0

Views: 141

Answers (1)

Mike
Mike

Reputation: 8355

In C and C++, \ characters are by default used as escape characters, they are used to represent some special characters (like '\n' which means the newline character). You need to escape them when you mean to actually use them in a string literal.

So, your setDatabaseName call should look something like this:

db.setDatabaseName("C:\\Users\\myaccount\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\234f25fs.default\\cookies.sqlite");

Upvotes: 3

Related Questions