Results 1 to 15 of 15

Thread: Sqlite data base

  1. #1
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Post Sqlite data base

    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.sqli te");
    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

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Sqlite data base

    Please use the [CODE] tags, it's hard to read your code now.
    You have to assign the database to query, it won't work otherwise:
    Qt Code:
    1. QSqlQuery query("SELECT * FROM City", db);
    2. // or
    3. QSqlQuery query(db);
    4. query.prepare("SELECT * FROM City");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: Sqlite data base

    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

  4. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Sqlite data base

    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().)

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Sqlite data base

    You may have problems with using the backslash in string constant here:
    Qt Code:
    1. db.setDatabaseName("C:\CabBookingDatabase.sqlite") ;
    To copy to clipboard, switch view to plain text mode 
    Try to change it to slash:
    Qt Code:
    1. db.setDatabaseName("C:/CabBookingDatabase.sqlite") ;
    To copy to clipboard, switch view to plain text mode 
    Or use double backslash:
    Qt Code:
    1. db.setDatabaseName("C:\\CabBookingDatabase.sqlite") ;
    To copy to clipboard, switch view to plain text mode 
    Check for the error description with QSqlQuery::lastError as well (after calling query.exec() ).

  6. #6
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: Sqlite data base

    sir ,
    Again not execute Query ,and not show any error
    if you have possible please help with small source code.
    Thanks

  7. #7
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Sqlite data base

    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.

    Quote Originally Posted by sabbu View Post
    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??

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sqlite data base

    Here are some tips:
    • Use [code][/code] 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
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  9. #9
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: Sqlite data base

    thanks
    my query executed

  10. #10
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: Sqlite data base

    when i execute query and print qDebug() << query.lastError();
    then find this error QSqlError(-1, "", "") .how to solve this problem please help me

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sqlite data base

    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.

  12. #12
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: Sqlite data base

    QSqlError(1, "Unable to execute statement", "no such table: CityType") this error is find how to solve this problem please help me

    Thanks

  13. #13
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Sqlite data base

    "no such table: CityType"
    I think this sentence is clear enough.

  14. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sqlite data base

    I told you the likely problem here. 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
    Qt Code:
    1. QSqlQuery query;
    2. query.exec("create table person (Id int primary key,name varchar(20), typeid int)");
    3. query.exec("insert into person values(1, 'Delhi',101)");
    4. query.exec("insert into person values(2, 'Gurgaon',102)");
    5. query.exec("insert into person values(3, 'Noida',103)");
    6. query.exec("insert into person values(4, 'Gaziabad',104)");
    7. query.exec("insert into person values(5, 'faridabad',105)");
    8. //! [Set up the main table]
    To copy to clipboard, switch view to plain text mode 


    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.

  15. #15
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: Sqlite data base

    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.

Similar Threads

  1. Update changes in QTableView to sqlite data base
    By nagabathula in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2011, 01:18
  2. Sqlite data base
    By sabbu in forum Newbie
    Replies: 2
    Last Post: 19th May 2011, 23:43
  3. Sqlite data base
    By sabbu in forum Qt Programming
    Replies: 5
    Last Post: 19th May 2011, 11:57
  4. Replies: 4
    Last Post: 19th December 2010, 06:15
  5. program for working with Data Base(i need it)
    By banakil in forum Qt Programming
    Replies: 2
    Last Post: 10th January 2007, 22:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.