本文整理汇总了C++中QSqlDatabase::connectOptions方法的典型用法代码示例。如果您正苦于以下问题:C++ QSqlDatabase::connectOptions方法的具体用法?C++ QSqlDatabase::connectOptions怎么用?C++ QSqlDatabase::connectOptions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QSqlDatabase
的用法示例。
在下文中一共展示了QSqlDatabase::connectOptions方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: connectToDatabase
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: createConnection
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: main
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();
}