PDA

View Full Version : Sqlite data base



sabbu
23rd May 2011, 09:01
Hi
My database coonection is working but my Query is not executed.please see my process may be i wrong in add existing file.
->i have a sqlit database CabBookingDatabase.sqlite
->i copy this database in c drive.
->i make a new project after make my project i add existing file choose CabBookingDatabase.sqlite,from c drive .
->is any other method to add database please help me because my Query will not executed.
please see my code
#include "ui_mainwindow.h"
#include <QtCore>
#include<QtGui>
#include <QtSql/QSqlDatabase>
#include<QtSql/QSqlError>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
db=QSqlDatabase::addDatabase("QSQLITE");
//QSqlDatabase::addDatabase("CabBookingDatabase.sqlite");
db.setDatabaseName("C:\CabBookingDatabase.sqlite");
if (db.open())
{
QMessageBox msgBox;
msgBox.setText("Database cOnnected.");
msgBox.exec();
}
else{
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
}

QSqlQuery query(QString("SELECT * FROM City"));
query.exec();
if (query.next())
{

QMessageBox msgBox;
msgBox.setText(query.value(2).toString());
msgBox.exec();

}

}

////////////.pro//////
QT += sql


please help me

Thanks

stampede
23rd May 2011, 09:44
Please use the
tags, it's hard to read your code now.
You have to assign the database to query, it won't work otherwise:
[CODE]QSqlQuery query("SELECT * FROM City", db);
// or
QSqlQuery query(db);
query.prepare("SELECT * FROM City");

sabbu
23rd May 2011, 13:31
sir ,
i used this code but Query will not execute.may be file is not add in directory.please see this point
My database coonection is working but my Query is not executed.please see my process may be i wrong in add existing file.
i have a sqlit database CabBookingDatabase.sqlite
i copy this database in c drive.
i make a new project after make my project i add existing file choose CabBookingDatabase.sqlite,from c drive .
is any other method to add database please help me because my Query will not executed.
please help Me

Thanks

DanH
23rd May 2011, 13:49
What platform are you on? What error are you seeing? If you're simply not retrieving the data then most likely the DB file isn't where SQLite is looking for it. It's best to check first with QFileInfo to make sure the file exists(), isReadable(), and isWriteable(). Then you know you have the right location.

(Also, you need to double up your backslashes, or use forward slashes and QDir::toNativeSeparators() to create the file path for setDatabaseName().)

stampede
23rd May 2011, 13:51
You may have problems with using the backslash in string constant here:

db.setDatabaseName("C:\CabBookingDatabase.sqlite") ;
Try to change it to slash:

db.setDatabaseName("C:/CabBookingDatabase.sqlite") ;
Or use double backslash:

db.setDatabaseName("C:\\CabBookingDatabase.sqlite") ;
Check for the error description with QSqlQuery::lastError as well (after calling query.exec() ).

sabbu
23rd May 2011, 14:44
sir ,
Again not execute Query ,and not show any error
if you have possible please help with small source code.
Thanks

DanH
24th May 2011, 04:12
Though Qt in general is perfectly happy with forward slashes for path separators, SQLite is not. One should use QDir::toNativeSeparators to convert a forward-slashed path to backslashes.


sir ,
Again not execute Query ,and not show any error
if you have possible please help with small source code.
Thanks
What do you mean "not execute Query"?? How do you know it did not execute??

ChrisW67
24th May 2011, 04:13
Here are some tips:

Use
tags around code. Your persistence in ignoring this politely requested courtesy to readers is wearing very thin with this reader.
Using an sqlite command line tool connect to the same database and execute the query manually. If it doesn't work here then it certainly won't work from your code.
Read the QSqlQuery and QSqlError docs.
Do not ignore the return value from QSqlQuery::exec()
If the QSqlQuery::exec() call fails then do something with the QSqlQuery::lastError() and you will be very much wiser.


If your database looks like the one you are defining here then the problem is that you have no table called City. Follow the advice above and you will know precisely inside a minute.

While you are at it, have a look at all the bits of advice you have received on this same program fragment: here, here, and here

sabbu
24th May 2011, 08:24
thanks
my query executed

sabbu
25th May 2011, 07:34
when i execute query and print qDebug() << query.lastError();
then find this error QSqlError(-1, "", "") .how to solve this problem please help me

ChrisW67
25th May 2011, 08:11
how to solve this problem please help me
What problem?

Did you look at the return value from QSqlQuery::exec()?
Did that indicate that there was a problem?

Did you read the QSqlError docs? They tell you what the error number -1 might mean.

You really need to help yourself more.

sabbu
25th May 2011, 12:38
QSqlError(1, "Unable to execute statement", "no such table: CityType") this error is find how to solve this problem please help me

Thanks

stampede
25th May 2011, 12:44
"no such table: CityType"
I think this sentence is clear enough.

ChrisW67
26th May 2011, 01:33
I told you the likely problem here (http://www.qtcentre.org/threads/41829-Sqlite-data-base?p=191607#post191607). You don't have a table called CityType, so you cannot query it. Pretty simple really. If you don't understand why you don't have a table called CityType given the database creation code I referenced then there is little we can do to help.

From your earlier post http://www.qtcentre.org/threads/41747-Sqlite-data-base


QSqlQuery query;
query.exec("create table person (Id int primary key,name varchar(20), typeid int)");
query.exec("insert into person values(1, 'Delhi',101)");
query.exec("insert into person values(2, 'Gurgaon',102)");
query.exec("insert into person values(3, 'Noida',103)");
query.exec("insert into person values(4, 'Gaziabad',104)");
query.exec("insert into person values(5, 'faridabad',105)");
//! [Set up the main table]



I am more confused how you go from "My query executed" with no error in your posts 9 and 10 of this thread, to this this very specific and informative error message. Are you working through this thing in any sort of methodical matter, or are you thrashing around in the hope that eventually you will get lucky, or someone will succumb and fix your code and post a complete implementation? The first approach will give you an understanding, the second and third will leave you with similar problems in a few days/months/years time and no idea how to deal with them.

Take a step back from the code, look at what you are trying to achieve and write down in English/your native tongue the broad things you need to achieve to get there. Then break each down into smaller, more specific things, until each item is a manageable, small bit of C++/SQL/shell script code or manual activity. Then work through each in turn, making sure it works before moving on.

DanH
26th May 2011, 02:17
Either you're command of English is far worse than it appears, or you are not cut out to be a programmer. I'd strongly suggest that you consider other occupations.