PDA

View Full Version : Sqlite data base



sabbu
18th May 2011, 12:22
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

bibhukalyana
18th May 2011, 12:42
Hi sabbu,

Here is my code which is working for me.


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("db");
if(!db_local.open())
{
qDebug()<<db_local.lastError();
qDebug("\ndatabase open error");
qDebug()<<QSqlDatabase::drivers ();
return false;
}


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

DanH
18th May 2011, 19:04
The second addDatabase doesn't belong there (or anywhere).

sabbu
19th May 2011, 11:19
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

ChrisW67
19th May 2011, 12:30
This is the same broken code from back here (http://www.qtcentre.org/threads/41300-Sqlite-Database). It still contains the same addDatabase() error and no
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:


db_local.setDatabaseName("the_file_name_of_the_sqlite_database");

bibhukalyana
19th May 2011, 12:57
sorry for the code
actually db_local should be replace by db.

ChrisW67 is correct .