@wysota
I have header file with static methods:
/**
* Static function for databse connection;
*/
//**************************************************************************
static bool createConLocalDb()
//**************************************************************************
{
dbpath.append("/");
dbpath.append("../db/localdb.db");
db.setDatabaseName(dbpath);
if (!db.open())
{
QMessageBox::critical(0, qApp
->tr
("Cannot open database localdb"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
return false;
}
qDebug() << "SQLITE LocalDB" << db.driver();
qDebug() << "SQLITE LocalDB" << db.driverName();
qDebug() << "SQLITE LocalDB" << db.connectionName();
return true;
}
//**************************************************************************
static bool createConDB2
(QString driver
= "QODBC",
QString connectionstr
="DRIVER={SQL Server Native Client 10.0};" "Server=locationToserver;Trusted_Connection=no;"
"Database=mydatabasename;Uid=myuid;Pwd=mypass"
)
//**************************************************************************
{
db.setDatabaseName(connectionstr);
if (!db.open()) {
return false;
}
qDebug() << "MSSQL DB2" << db.driver();
qDebug() << "MSSQL DB2" << db.driverName();
qDebug() << "MSSQL DB2" << db.connectionName();
return true;
}
//**************************************************************************
static bool createConDummy()
//**************************************************************************
{
dbpath.append("/");
dbpath.append("../db/dummydb.db");
db.setDatabaseName(dbpath);
if (!db.open())
{
QMessageBox::critical(0, qApp
->tr
("Cannot open database dummy"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
return false;
}
qDebug() << "SQLITE Dummy" << db.driver();
qDebug() << "SQLITE Dummy" << db.driverName();
qDebug() << "SQLITE Dummy" << db.connectionName();
return true;
}
/**
* Static function for databse connection;
*/
//**************************************************************************
static bool createConLocalDb()
//**************************************************************************
{
QString dbpath;
dbpath = QCoreApplication::applicationDirPath();
dbpath.append("/");
dbpath.append("../db/localdb.db");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","LocalDB");
db.setDatabaseName(dbpath);
if (!db.open())
{
QMessageBox::critical(0, qApp->tr("Cannot open database localdb"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
qDebug() << "SQLITE LocalDB" << db.driver();
qDebug() << "SQLITE LocalDB" << db.driverName();
qDebug() << "SQLITE LocalDB" << db.connectionName();
return true;
}
//**************************************************************************
static bool createConDB2(QString driver= "QODBC",
QString connectionstr="DRIVER={SQL Server Native Client 10.0};"
"Server=locationToserver;Trusted_Connection=no;"
"Database=mydatabasename;Uid=myuid;Pwd=mypass"
)
//**************************************************************************
{
QSqlDatabase db = QSqlDatabase::addDatabase(driver,"DB2");
db.setDatabaseName(connectionstr);
if (!db.open()) {
QSqlError err = db.lastError ();
QMessageBox::information(0, QObject::tr("Cannot open database DB2"), err.text());
return false;
}
qDebug() << "MSSQL DB2" << db.driver();
qDebug() << "MSSQL DB2" << db.driverName();
qDebug() << "MSSQL DB2" << db.connectionName();
return true;
}
//**************************************************************************
static bool createConDummy()
//**************************************************************************
{
QString dbpath;
dbpath = QCoreApplication::applicationDirPath();
dbpath.append("/");
dbpath.append("../db/dummydb.db");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbpath);
if (!db.open())
{
QMessageBox::critical(0, qApp->tr("Cannot open database dummy"),
qApp->tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
return false;
}
qDebug() << "SQLITE Dummy" << db.driver();
qDebug() << "SQLITE Dummy" << db.driverName();
qDebug() << "SQLITE Dummy" << db.connectionName();
return true;
}
To copy to clipboard, switch view to plain text mode
After in the constructor I made this:
this->createStatusBar();
qDebug() << " Creating Connections to Databases";
int i_conDummy=createConDummy();//"qt_sql_default_connection"
int i_conlocaldb = createConLocalDb();//"LocalDB"
int i_conMssql = createConDB2();//"DB""
...
...
this->createStatusBar();
qDebug() << " Creating Connections to Databases";
int i_conDummy=createConDummy();//"qt_sql_default_connection"
int i_conlocaldb = createConLocalDb();//"LocalDB"
int i_conMssql = createConDB2();//"DB""
...
...
To copy to clipboard, switch view to plain text mode
The integer values after connection are changed to '1';
After connections to my databses I have this output for the static method I call:
Creating Connections to Databases
SQLITE Dummy QSQLiteDriver(0x9fff208)
SQLITE Dummy "QSQLITE"
SQLITE Dummy "qt_sql_default_connection"
SQLITE LocalDB QSQLiteDriver(0xa000548)
SQLITE LocalDB "QSQLITE"
SQLITE LocalDB "LocalDB"
MSSQL Proleas QODBCDriver(0xa0016e0)
MSSQL Proleas "QODBC"
MSSQL Proleas "DB2"
Creating Connections to Databases
SQLITE Dummy QSQLiteDriver(0x9fff208)
SQLITE Dummy "QSQLITE"
SQLITE Dummy "qt_sql_default_connection"
SQLITE LocalDB QSQLiteDriver(0xa000548)
SQLITE LocalDB "QSQLITE"
SQLITE LocalDB "LocalDB"
MSSQL Proleas QODBCDriver(0xa0016e0)
MSSQL Proleas "QODBC"
MSSQL Proleas "DB2"
To copy to clipboard, switch view to plain text mode
@tangential
could you also supply us with the lastError() on the model (after the select?)
This is the output:
Last Error
: QSqlError(1,
"Unable to execute statement",
"near ",
": syntax error")
Last Error: QSqlError(1, "Unable to execute statement", "near ",": syntax error")
To copy to clipboard, switch view to plain text mode
@all
with the mssql database I don´t have this select() problem on the modelside !
Bookmarks