PDA

View Full Version : how to show my sqlite database table on mainwindow with tableview widget



prasad1001
25th February 2014, 10:03
Hello All,
I am new to Qt and Sql database, now i am trying to create a sqlite database file and table.
And now i want to display the table in my mainwindow tableview....
Everything is ok but table is not displaying on the window...and no errors are comming...but when i clicked the submit button i got the error as shown in fig.
here is my code in attachments..

Thanks in advance..

4Tech
25th February 2014, 11:56
Hello prasad1001
First of all, sorry for my english.

you have set the database name as "hari", so try this:


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("hari");
...
db.open();
...
QSqlQuery query(db);

or


QSqlQuery query(QSqlDatabase::database("hari", false));

in elsewhere.
false makes it not open a new database even if the database named "hari" does not exist.

anda_skoa
25th February 2014, 13:26
And now i want to display the table in my mainwindow tableview....
Everything is ok but table is not displaying on the window...and no errors are comming...but when i clicked the submit button i got the error as shown in fig.


Your QSqlTableModel does not have a database connection to work with, see second constructor argument.
Also the given table name is wrong (your table is not called "library data", just "library").

Cheers,
_

P.S.: A Sqlite database is a single file, you don't need username or password for that.

prasad1001
26th February 2014, 04:56
Thanks for the reply sir,
i was changed the table name and it works fine..

Thanks..