Results 1 to 6 of 6

Thread: Sqlite data base

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

    Post Sqlite data base

    i have a sqlite database but i am tired to open this database my cod is below please help me .i am more tired
    manager.pro
    QT += sql
    //////////////header//////////

    #ifndef MANAGER_H
    #define MANAGER_H

    #include <QMainWindow>
    #include <QWidget>
    #include<QtGui>
    #include <QtSql/QSqlDatabase>
    //#include <QtSql/QSqlQueryModel>
    #include<QtSql/QSqlQuery>
    #include<QtSql/QSqlTableModel>



    namespace Ui {
    class Manager;
    }

    class Manager : public QMainWindow
    {
    Q_OBJECT
    QSqlDatabase db;
    QSqlQuery query;
    public:
    explicit Manager(QWidget *parent = 0);
    ~Manager();
    bool createConnection();

    private:
    Ui::Manager *ui;
    };

    #endif // MANAGER_H


    ////////////////.cppp////////////////
    #include "Manager.h"
    #include "ui_manager.h"
    #include<QDebug>
    #include<qdebug.h>
    #include<QtGui>
    #include<QtGui>
    #include <QtSql/QSqlDatabase>
    //#include <QtSql/QSqlQueryModel>
    #include<QtSql/QSqlQuery>
    #include<QtSql/QSqlTableModel>



    Manager::Manager(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Manager)
    {
    ui->setupUi(this);
    createConnection();
    LoadData();
    }
    bool Manager::createConnection()
    {
    db = QSqlDatabase::addDatabase("QSQLITE");
    QSqlDatabase::addDatabase("C:/NokiaQtSDK/project/Manager","CabBookingDatabase.Sqlite");
    db.setDatabaseName("CabBookingDatabase.sqlite");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    "This program 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;
    }
    return true;
    }
    but my driver is not loaded please help me how to open sqlite database

  2. #2
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite data base

    Hi sabbu,

    Here is my code which is working for me.
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("db");
    3. if(!db_local.open())
    4. {
    5. qDebug()<<db_local.lastError();
    6. qDebug("\ndatabase open error");
    7. qDebug()<<QSqlDatabase::drivers ();
    8. return false;
    9. }
    To copy to clipboard, switch view to plain text mode 

    From here you can know which drivers are there and what is the last error.

  3. #3
    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

    The second addDatabase doesn't belong there (or anywhere).

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

    Default Re: Sqlite data base

    thanks sir,i have a database whose name is (city.sqlite) but not open .is neccessary to file name is (city.db) .please give me feed back .please mention what is db_local

  5. #5
    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

    This is the same broken code from back here. It still contains the same addDatabase() error and no [code][/code] tags. You have even cut and pasted from the working example in the documentation "connection.h".

    If your database is called "city.sqlite" or "city.db" then why don't you try to use that as the file name in a call to setDatabaseName()?

    In bibhukalyana's answer db_local is the name of the QSqlDatabase object. Line 2 should read:
    Qt Code:
    1. db_local.setDatabaseName("the_file_name_of_the_sqlite_database");
    To copy to clipboard, switch view to plain text mode 
    "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.

  6. #6
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Sqlite data base

    sorry for the code
    actually db_local should be replace by db.

    ChrisW67 is correct .

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. Replies: 4
    Last Post: 19th December 2010, 06:15
  3. Sqlite and UTF8 data
    By kroenecker in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2009, 14:49
  4. QTable - Data Base Problem
    By esq in forum Qt Tools
    Replies: 9
    Last Post: 27th May 2007, 23:08
  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.