View Full Version : Problem wit 2010.01
codeman
20th January 2010, 14:01
Hello friends,
after deinstall 2009.05 and install 2010.01 the qsqltabelmodel in combination with qtableview is still empty. My local Sqlite DB, which I use in my program is fullfilled correct, afterwards I show it in a QTabelview with my QSqlTableModel, but it shows nothing my view is still emty
and model->select() returns false. Have anybody the same problem?? With the 2009.05 everything was ok???
codeman
20th January 2010, 23:23
So in addition I do some tests.:
QSqlDatabase db;
db = QSqlDatabase::database("LocalDB");
qDebug() << "LocalDB is Valid?:"<< db.isValid();
QSqlQuery mytest("select * from localDBtable",db);
while(mytest.next())
{
qDebug() << mytest.value(2).toString() << endl;
}
QSqlTableModel mymodel;
mymodel.setTable("localDBtable");
bool loctest = mymodel.select();
So first of all I test the db validation here I get a <true> value
The second I test, if I could trace over a select statement from the table. An I get the value in the while loop. So everything seems ok.
Last I take a QSqlTableModel and try to select() the table, but I get an <false> value.
So I am at the end of my knowledge?
Could anybody image what wrong here???
wysota
21st January 2010, 02:13
Did you initialize the database driver properly using QSqlDatabase::addDatabase()? Can we see the exact call?
tangential
21st January 2010, 03:37
could you also supply us with the lastError() on the model (after the select?)
if(!loctest)
qDebug() << mymodel().lastError();
codeman
21st January 2010, 11:21
@wysota
I have header file with static methods:
/**
* 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;
}
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""
...
...
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"
@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")
@all
with the mssql database I don´t have this select() problem on the modelside !
wysota
21st January 2010, 18:09
And how does the main() function look like?
codeman
21st January 2010, 18:32
#include <QApplication>
#include <QCoreApplication>
#include <QMainWindow>
#include "../headers/tableeditor.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Q_INIT_RESOURCE(myresourcefile);
TableEditor editor;
editor.showMaximized();
return app.exec();
}
This is my main.cpp
wysota
22nd January 2010, 01:46
So where do you create those connections? Also please inject the following code after each statement of the code you pasted in post #5 of this thread that operates on the database (creates it, queries it, operates on an sql model) and show us results:
qDebug() << db.lastError().text();
venomj
22nd January 2010, 04:09
I think you should init your model like this
QSqlTableModel *model = new QSqlTableModel(this, QSqlDatabase::database("LocalDB"));
Since you specified a name for your database connection, you should make sure the model connect to "LocalDB" instead of the default db.
codeman
22nd January 2010, 13:10
Ok I solve it. I had a folder in my bin folder named "plugins" with the old libs. Sorry sorry for the trouble, shame on me ;o(.
I just forget to change the lib in the folder for deploy purposes....
codeman
22nd January 2010, 13:28
Another question by the way I don´t want to open new thread.
I have only one error ore misfunction.
When I read from mssql database a field with type decimal,
the line:
QString testdt = q.value(2).toString();
return an empty string. In the mssql database the field is filled with 0.24 ???
codeman
22nd January 2010, 16:09
Thanx, for your attention. I solve it.
In the Docu stands :
Constant Value Description
QSql::LowPrecisionInt32 0x01 Force 32bit integer values. In case of floating point numbers, the fractional part is silently discarded.
QSql::LowPrecisionInt64 0x02 Force 64bit integer values. In case of floating point numbers, the fractional part is silently discarded.
QSql::LowPrecisionDouble 0x04 Force double values.
QSql::HighPrecision 0 The default behavior - try to preserve maximum precision.
High precision is declared here as default, but I must set explicitly the policy to QSql::HighPrecision.
Curious?!
In my Program the default precisionPolicy is QSql::LowPrecisionDouble
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.