本文整理汇总了C++中QSqlDatabase::databaseName方法的典型用法代码示例。如果您正苦于以下问题:C++ QSqlDatabase::databaseName方法的具体用法?C++ QSqlDatabase::databaseName怎么用?C++ QSqlDatabase::databaseName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QSqlDatabase
的用法示例。
在下文中一共展示了QSqlDatabase::databaseName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: primaryIndex
void tst_QSqlDriver::primaryIndex()
{
QFETCH_GLOBAL(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
QString tablename = qTableName("relTEST1");
//check that we can get primary index using unquoted mixed case table name
QSqlIndex index = db.driver()->primaryIndex(tablename);
QCOMPARE(index.count(), 1);
if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
QCOMPARE(index.fieldName(0), QString::fromLatin1("ID"));
else
QCOMPARE(index.fieldName(0), QString::fromLatin1("id"));
//check that we can get the primary index using a quoted tablename
if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
tablename = tablename.toUpper();
else if (db.driverName().startsWith("QPSQL"))
tablename = tablename.toLower();
if(!db.driverName().startsWith("QODBC") && !db.databaseName().contains("PostgreSql")) {
index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName));
QCOMPARE(index.count(), 1);
}
if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
QCOMPARE(index.fieldName(0), QString::fromLatin1("ID"));
else
QCOMPARE(index.fieldName(0), QString::fromLatin1("id"));
//check that we can not get the primary index using a quoted but incorrect table name casing
if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2"))
tablename = tablename.toLower();
else if (db.driverName().startsWith("QPSQL"))
tablename = tablename.toUpper();
index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName));
if (tst_Databases::isMySQL(db)
|| db.driverName().startsWith("QSQLITE")
|| db.driverName().startsWith("QTDS")
|| tst_Databases::isSqlServer(db)
|| tst_Databases::isMSAccess(db))
QCOMPARE(index.count(), 1); //mysql will always find the table name regardless of casing
else
QCOMPARE(index.count(), 0);
}
示例2: QObject
MyInfoScheme::MyInfoScheme(const QString &connectname, QObject *parent) :
QObject(parent)
{
connectionname = connectname;
QSqlDatabase db = QSqlDatabase::database(connectname);
if(!db.open())
{
sqlerror = db.lastError();
error = true;
return;
}
dbname = db.databaseName();
query = new QSqlQuery(db);
error = false;
}
示例3: saver
QDebug operator<<(QDebug dbg, const QSqlDatabase &d)
{
QDebugStateSaver saver(dbg);
dbg.nospace();
dbg.noquote();
if (!d.isValid()) {
dbg << "QSqlDatabase(invalid)";
return dbg;
}
dbg << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\""
<< d.databaseName() << "\", host=\"" << d.hostName() << "\", port=" << d.port()
<< ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ')';
return dbg;
}
示例4: onCreate
void CoreDbHelper::onCreate(QSqlDatabase &database) {
database.transaction();
QSqlQuery query(database);
if ( !query.exec(CREATE_TABLE_BOOKMARK) ) {
QWARNING_EXP("CoreDbHelper::onCreate()", query.lastError().text());
}
if ( !query.exec(CREATE_TABLE_HISTORY) ) {
QWARNING_EXP("CoreDbHelper::onCreate()", query.lastError().text());
}
if ( !database.commit() ) {
QDEBUG_EXP("CoreDbHelper::onCreate()", "DB: " << database.databaseName() << " - " << query.lastError().text());
QDEBUG_EXP("CoreDbHelper::onCreate()", "Last Query: " << query.lastQuery());
QWARNING_EXP("CoreDbHelper::onCreate()", "Couldn't create tables");
} else {
QDEBUG_EXP("CoreDbHelper::onCreate()", "Done.");
}
}
示例5: addDatabase
/**
* Ajoute une base au pool de connexions.
*
* @param driver
* Voir doc Qt
* @param host
* Hôte distant ou local
* @param user
* Nom d'utilisateur
* @param pswd
* Mot de passe
* @param dbnm
* Base de données
* @param save
* Déclenche l'enregistrement de la liste ou non
*/
int DbManagerPrivate::addDatabase(QString driver, QString host, QString user,
QString pswd, QString dbnm, bool save)
{
QSqlDatabase db = QSqlDatabase::addDatabase(driver, genConnectionName());
db.setHostName(host);
db.setUserName(user);
db.setPassword(pswd);
db.setDatabaseName(dbnm);
foreach(QSqlDatabase *d, dbList)
{
// on contrôle les éventuels doublons
if (d->hostName() == db.hostName() &&
d->userName() == db.userName() &&
d->password() == db.password() &&
d->databaseName() == db.databaseName())
{
LogDialog::instance()->append(
QObject::tr("Attempting to add an existing connection."));
return indexOf(d);
}
}
QSqlDatabase *newDb = new QSqlDatabase(db);
dbList.append(newDb);
dbMap[newDb] = new QStandardItem(dbTitle(newDb));
dbMap[newDb]->setEditable(false);
dbMap[newDb]->setData(DbManager::DbItem, Qt::UserRole);
dbMap[newDb]->setIcon(IconManager::get("connect_no"));
dbMap[newDb]->setToolTip(dbToolTip(newDb));
// if(!isRunning())
// start();
m_model->appendRow(dbMap[newDb]);
if(save)
{
saveList();
LogDialog::instance()->append(QObject::tr("Database %1 on %2 added")
.arg(dbnm)
.arg(host));
}
return dbList.size() - 1;
}
示例6: connection
ImporterSqlConnection::~ImporterSqlConnection()
{
if( isTransaction() )
{
QSqlDatabase db = connection();
if( db.isOpen() )
{
warning() << __PRETTY_FUNCTION__ << "Rolling back unfinished transaction for"
<< "database" << db.databaseName() << "(" << db.hostName() << ":"
<< db.port() << ")";
db.rollback();
}
}
QSqlDatabase::removeDatabase( m_connectionName );
}
示例7: QString
/*!
\internal
Constructs a connection for the calling thread. The resulting class must
never be accessed from any other thread than the one for which it was
constructed. The \a masterName must reference an existing database
connection which is already open.
*/
QxtSqlThreadManager::QxtSqlThreadManager(const QString &masterName)
{
// Build a name for the new connection
this->name = QString("$qxt$tc_%1$%2")
.arg((unsigned long)(void*)QThread::currentThreadId(), 0, 36)
.arg(masterName);
// Clone the primary thread's connection
Q_ASSERT(QSqlDatabase::contains(masterName));
Q_ASSERT(QSqlDatabase::database(masterName).isOpen() == true);
QSqlDatabase conn = QSqlDatabase::cloneDatabase(
QSqlDatabase::database(masterName), this->name);
// Open the connection (should not fail but ...)
if(!conn.open())
qWarning() << Q_FUNC_INFO
<< "Failed to open connection to database" << conn.databaseName()
<< ", error: " << conn.lastError().text();
qDebug() << "Constructed database connection" << this->name;
}
示例8: locker
LibraryDB::LibraryDB(Proxy *pr) :QObject(0)
{
proxy = pr;
QMutexLocker locker(&lock);
db = QSqlDatabase::addDatabase("QSQLITE", "LibraryDB");
db.setDatabaseName(QDir::homePath()+"/.cuberok/library.db");
if(QFile::exists(db.databaseName())) {
if(!db.open()) {
qDebug("Can not open library database");
proxy->error("Can not open library database");
open = false;
} else {
open = true;
QSqlQuery q1("select value from Version", db);
int ver = 0;
if(q1.next()) ver = q1.value(0).toString().toInt();
if(ver < DB_VERSION) {
open = updateDatabase(ver);
if(!open) db.close();
} else if(ver > DB_VERSION) {
open = false;
db.close();
qDebug("Wrong database version (%d)", ver);
proxy->error("Wrong database version -" + QString::number(ver));
}
}
} else {
if(!QDir().mkpath(QDir::homePath()+"/.cuberok") || !db.open()) {
qDebug("Can not create database");
proxy->error("Can not create database");
open = false;
} else {
QSqlQuery q0("create table Version (value integer)", db);
QSqlQuery q1("insert into Version (value) values ("+QString::number(DB_VERSION)+")", db);
QSqlQuery q2("create table Playlist (ID integer primary key autoincrement, value varchar(200), refs integer, rating integer, art varchar(250), list varchar(250))", db);
FillFromCollection();
open = true;
}
}
if(open) proxy->message("LibraryDB ready");
}
示例9: setupDatabase
bool Hourglass::setupDatabase()
{
bool ok = true;
QSettings settings;
QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
if (settings.contains("databasePath")) {
database.setDatabaseName(settings.value("databasePath").toString());
}
else {
#ifdef Q_OS_LINUX
QDir path(QDir::home().path());
if ((path.exists(".hourglass") || path.mkdir(".hourglass")) && path.cd(".hourglass")) {
QString databasePath = path.absoluteFilePath("hourglass.db");
database.setDatabaseName(databasePath);
settings.setValue("databasePath", databasePath);
}
#else
// Non-Linux
database.setDatabaseName("hourglass.db");
#endif
}
if (database.databaseName().isEmpty() || !database.open()) {
// Try to use in-memory database
database.setDatabaseName(":memory:");
if (database.open()) {
QMessageBox::warning(NULL, "In memory database", "Couldn't create a local database; using an in-memory database instead. Your activities will not be saved.");
}
else {
QMessageBox::critical(NULL, "Database", "Couldn't create a local database or use an in-memory database. Closing...");
ok = false;
}
}
if (ok) {
migrateDatabase(database);
}
return ok;
}
示例10: convertString
QString TSocket::convertString(const QString &str)
{
//get_currency_properties(1)
QSqlDatabase db = QSqlDatabase::database();
if( !db.isValid() )
return str;
QString string = db.databaseName();
QString a = "CALL ";
if( !string.isNull() && !string.isEmpty() )
{
a += string;
a += ".";
}
a += "p_";
a += str;
a += ";";
return a;
}
示例11: validDatabaseAndAccept
void DatabaseConnectionDialog::validDatabaseAndAccept() {
try {
QSqlDatabase database = QSqlDatabase::addDatabase("QMYSQL");
database.setHostName(currentProperties->getHostAddress());
if(database.hostName().isEmpty())
throw QString("hostName cannot be empty !");
database.setDatabaseName(currentProperties->getDatabaseName());
if(database.databaseName().isEmpty())
throw QString("databaseName cannot be empty !");
database.setUserName(currentProperties->getUserName());
if(database.userName().isEmpty())
throw QString("userName cannot be empty !");
database.setPassword(currentProperties->getPassword());
if(!database.open())
throw QString("unable to reach the database...");
else
qDebug() << "database properties OK. Ready to be sent !";
database.close();
emit this->propertiesValidated(currentProperties);
qDebug() << "properties sent !";
//this->accept();
this->close();
}
catch(const QString& exception) {
QMessageBox msgBox;
msgBox.setText(exception);
msgBox.setInformativeText("Check the connection information");
msgBox.setStandardButtons(QMessageBox::Ok);
int ret = msgBox.exec();
switch(ret) {
case QMessageBox::Ok:
ui->passwordEdit->setText("");
currentProperties->setPassword("");
break;
default:
;
}
}
}
示例12: on_Update_clicked
void MainWindow::on_Update_clicked()
{
QSqlDatabase* updatedB = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE","updatedB"));
updatedB->setDatabaseName(QString(QDir::homePath() + "/.sqlitedb/symbol_list.sqlite"));
if(!updatedB->open())
{
qDebug() << "Error opening" << updatedB->databaseName();
}
if(updatedB->isValid() && updatedB->isOpen())
{
QSqlQueryModel *model = new QSqlQueryModel(this);
QSqlQuery *query = new QSqlQuery(*updatedB);
query->prepare("SELECT * FROM symbol_list");
query->exec();
model->setQuery(*query);
ui->integratedStocks->setModel(model);
// ui->integratedStocks->sortByColumn(0);
delete query;
}
}
示例13: locker
Database::Database() :QObject(0), subset(false), ssAlbum(0), fillingProgress(0), filling(false)
{
QMutexLocker locker(&lock);
db = QSqlDatabase::addDatabase("QSQLITE", "CollectionDB");
db.setDatabaseName(QDir::homePath()+"/.cuberok/collection.db");
// db.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE=1");
if(QFile::exists(db.databaseName())) {
if(!db.open()) {
qDebug("Can not open database");
proxy->error("Can not open database");
open = false;
} else {
open = true;
QSqlQuery q1("select value from Version", db);
int ver = -1;
if(q1.next()) ver = q1.value(0).toString().toInt();
if(ver == -1) {
open = createDatabase();
} else if(ver < DB_VERSION) {
open = updateDatabase(ver);
if(!open) db.close();
} else if(ver > DB_VERSION) {
open = false;
db.close();
qDebug("Wrong database version (%d)", ver);
proxy->error("Wrong database version -" + QString::number(ver));
}
}
} else {
if(!QDir().mkpath(QDir::homePath()+"/.cuberok") || !db.open()) {
qDebug("Can not create database");
proxy->error("Can not create database");
open = false;
} else {
open = createDatabase();
}
}
if(open) proxy->message("Database ready");
}
示例14: QObject
TractionTypeView::TractionTypeView(Configuration *cfg, QObject *parent) :
QObject(parent)
{
m_parent = parent;
config = Configuration::instance();
//sql->setConfig(config);
sql = SQL::instance();
mainWindow* myParent = qobject_cast<mainWindow*>(m_parent);
tableView = myParent->ui->tblTractionTypes;
tableView->verticalHeader()->resize(2,20);
QSqlDatabase db = QSqlDatabase::database();
qDebug()<<db.databaseName();
connect(tableView->verticalHeader(), SIGNAL(sectionCountChanged(int,int)), this, SLOT(Resize(int,int)));
tableView->setAlternatingRowColors(true);
model = new MyTractionTypesTableModel(this, db);
model->setTable("TractionTypes");
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->query().setForwardOnly(false);
model->select();
QString name = model->record(0).value("description").toString();
tableView->setModel(model);
menu = new QMenu();
addAct = new QAction(tr("Add Traction Type"),this);
addAct->setToolTip(tr("Add a new traction type"));
connect(addAct, SIGNAL(triggered()), this, SLOT(newRecord()));
delAct = new QAction(tr("Delete Traction Type"),this);
delAct->setToolTip(tr("Delete an existing traction type"));
connect(delAct, SIGNAL(triggered()), this, SLOT(delRecord()));
tableView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(tableView, SIGNAL(customContextMenuRequested( const QPoint& )), this, SLOT(tablev_customContextMenu( const QPoint& )));
tableView->show();
}
示例15: FillFromCollection
void LibraryDB::FillFromCollection()
{
QSqlDatabase cdb;
cdb = QSqlDatabase::addDatabase("QSQLITE");
cdb.setDatabaseName(QDir::homePath()+"/.cuberok/collection.db");
if(QFile::exists(db.databaseName())) {
if(cdb.open()) {
QSqlQuery q1("select value from Version", cdb);
int ver = 0;
if(q1.next()) ver = q1.value(0).toString().toInt();
if(ver <= 7) {
QSqlQuery q("", cdb);
q.prepare("select value, art from Playlist order by value ASC");
q.exec();
while(q.next()) {
QString list = q.value(0).toString();
AddPlaylist(list);
ArtForPlaylist(list, q.value(1).toString());
}
}
cdb.close();
}
}
}