コード例 #1
0
ファイル: globalfunctions.cpp プロジェクト: vvim/routetool
bool connectToDatabase()
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName(settings.value("db/host").toString());
    db.setDatabaseName(settings.value("db/databasename").toString());
    db.setUserName(settings.value("db/username").toString() );
    db.setPassword(settings.value("db/password").toString());
    qDebug() << QObject::tr(" ++ 1) original connect options:") << db.connectOptions();
    db.setConnectOptions( "MYSQL_OPT_RECONNECT=true;" ) ;
    qDebug() << QObject::tr(" ++ 2) new connect options:") << db.connectOptions();

    if( !db.open() )
    {
        /*
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
            qApp->tr("Unable to establish a database connection.\n"
                     "This is the error recieved: \n\n").append(db.lastError().text()), QMessageBox::Cancel);
                     */
        QMessageBox::critical(0, QObject::tr("Cannot open database"),
            QObject::tr("Unable to establish a database connection.\n"
                     "This is the error recieved: \n\n").append(db.lastError().text()), QMessageBox::Cancel);
        qCritical( "Failed to connect to database" );
        return false;
    }

    qDebug() << " ++ 3) connect options after opening:" << db.connectOptions();
    vvimDebug() << "Connected to database at " << settings.value("db/host").toString();

    return true;
}
コード例 #2
0
ファイル: dictdbfactory.cpp プロジェクト: al-indigo/Dialeqt
QSqlDatabase DictDbFactory::createConnection(const QString &connectionName) {
    QDir dictsDir = QDir();
    dictsDir.mkpath("dictionaries");
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", connectionName);
    db.setDatabaseName(connectionName);
    if (!db.open()) {
        qDebug() << "Can't create db " << db.databaseName();
        db.lastError();
//        QMessageBox::critical(0, qApp->tr("Не удаётся открыть/создать файл словаря."),
    }
    if (!db.isValid()) {
        qDebug() << "Connection to database is invalid " << db.connectOptions() << db.databaseName() << db.isOpenError();
    }
//    db.driver()->hasFeature(QSqlDriver::foreign)
    qDebug() << "Sql driver supports Transactions:" << db.driver()->hasFeature(QSqlDriver::Transactions);
    qDebug() << "Sql driver supports PreparedQueries:" << db.driver()->hasFeature(QSqlDriver::PreparedQueries);
    qDebug() << "Sql driver supports SimpleLocking:" << db.driver()->hasFeature(QSqlDriver::SimpleLocking);
    qDebug() << "Sql driver supports EventNotifications:" << db.driver()->hasFeature(QSqlDriver::EventNotifications);
    qDebug() << "Sql driver supports FinishQuery:" << db.driver()->hasFeature(QSqlDriver::FinishQuery);
    qDebug() << "Sql driver supports MultipleResultSets:" << db.driver()->hasFeature(QSqlDriver::MultipleResultSets);
    qDebug() << "Sql driver supports CancelQuery:" << db.driver()->hasFeature(QSqlDriver::CancelQuery);
    qDebug() << "Sql driver supports Unicode:" << db.driver()->hasFeature(QSqlDriver::Unicode);
    qDebug() << "Sql driver supports LastInsertId:" << db.driver()->hasFeature(QSqlDriver::LastInsertId);
    qDebug() << "Sql driver supports BatchOperations:" << db.driver()->hasFeature(QSqlDriver::BatchOperations);
    return db;
}
コード例 #3
0
QString QSqlDatabaseProto::connectOptions()   const
{
  QSqlDatabase *item = qscriptvalue_cast<QSqlDatabase*>(thisObject());
  if (item)
    return item->connectOptions();
  return QString();
}
コード例 #4
0
QString QSqlDatabaseProto::connectOptions()   const
{
  QSqlDatabase *item = qscriptvalue_cast<QSqlDatabase*>(thisObject());
  if (item)
    return item->connectOptions();
  else if (! thisObject().isNull())
    return thisObject().toVariant().value<QSqlDatabase>().connectOptions();
  return QString();
}
コード例 #5
0
ファイル: main.cpp プロジェクト: joilar/CivAdvisor
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QTextStream out(stdout);
    out << "Hello world!" << endl;

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("basic.sqlite");
    bool ok = db.open();

    out << "What is the database name? " << db.databaseName() << endl;
    out << "Did I connect to " << db.databaseName() << "? " << ok << endl;
    out << "Is the connection valid? " << db.isValid() << endl;
    out << "Is the connection open? " << db.isOpen() << endl;
    out << "What is the driver name? " << db.driverName() << endl;
    out << "What is the host name? " << db.hostName() << endl;
    out << "What is the connection name? " << db.connectionName() << endl;
    out << "What are the connection options? " << db.connectOptions() << endl;

    bool hasQuerySize = db.driver()->hasFeature(QSqlDriver::QuerySize);
    out << "Does sqlite support querying the result set size? " << hasQuerySize << endl;

    out << "Tables:";
    QStringList tables = db.tables();
    foreach (QString table, tables) {
        out << " " << table;
    }
    out << endl;

    QSqlQuery query;
    query.setForwardOnly(true);
    query.exec("SELECT * from advances");

    if (hasQuerySize) {
        out << "Querying advances (" << query.size() << " results):" << endl;
    } else {
        out << "Querying advances:" << endl;
    }

    QStringList columns;

    QSqlRecord record = query.record();
    out << "Number of columns: " << record.count() << endl;
    for (int i = 0; i < record.count(); i++) {
        out << "column " << i << ": " << record.fieldName(i) << endl;
        columns.append(record.fieldName(i));
    }

    QList<QObject*> advances;

    while(query.next()) {
        Civilization::Advance *advance = new Civilization::Advance();

        out << "advance " << advances.size() << ":";

        for (int i = 0; i < columns.size(); i++) {
            out << " " << columns[i] << ": " << query.value(i).toString();
            QVariant v(query.value(i).toString());
            advance->setProperty(columns[i].toLatin1().data(), v);
        }

        out << endl;

        advances.append(advance);
    }

    out << advances.size() << " results." << endl;

    db.close();

    QQmlApplicationEngine engine;
    engine.rootContext()->setContextProperty("advanceListModel", QVariant::fromValue(advances));
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}