SQLITE with QT Application...
-
wrote on 5 Jul 2012, 14:00 last edited by
- How to add SQLITE suporte to my application?
- I will need to compile the SQLITE driver?
- Have any step-by-step example?
Thanks everyone.
-
wrote on 5 Jul 2012, 14:18 last edited by
-
wrote on 5 Jul 2012, 16:16 last edited by
Hi
do not have to compile the SQLite driver.simple example:
in *.pro file add line
@
QT += sql
@
and
@
#include <QtSql/QtSql>
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("fileName.db3");
db.open();
// db.close(); // for close connection
@more, depending on the task.
-
wrote on 5 Jul 2012, 16:35 last edited by
Thanks!
Skyrim, where you find this example?
-
wrote on 5 Jul 2012, 19:45 last edited by
Another question...
I'm trying to open my generated database with some tables on SQLITEMAN and the SQLITEMAN does not show any table on the file...
I have used sqlite on android apps and I could open the SQLITE FILES on SQLITEMAN withtout any problems....It's a problem, it could not be done... anyone with the same "problem"?
thanks -
wrote on 5 Jul 2012, 20:22 last edited by
- different sqlite versions?
- did you at least generate any table in the database (qsqlquery, qsqltablemodel, ...)?
;-) .. frank
-
wrote on 5 Jul 2012, 20:25 last edited by
Could be the version, I read something that says Sqliteman just open sqlite3 files...
I make this code, case study code?
@
#include "banco.h"
#include "QtSql/QtSql"
#include "QDebug"Banco::Banco(QObject *parent) :
QObject(parent)
{
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
if (db.open()) {
QSqlQuery query(db);
if (query.exec("CREATE TABLE usuario ( INTEGER oid PRIMARY KEY);")) {
qDebug() << "criado tabela com sucesso.";
} else {
qDebug() << "erro ao criar tabela." << query.lastError();
}
} else {
qDebug() << "DB Nao aberto.";
}
db.close();
@- brazillian portuguese debug messages.
If I run this code twice, I got error saying that already exists the table "usuario".
-
wrote on 5 Jul 2012, 20:41 last edited by
Hmm, code looks quite good. Have you tried the Firefox Plugin "SQLite Manager"? Sqlite3 is Qt-default as far as I know.
-
wrote on 5 Jul 2012, 21:16 last edited by
I read the documentation, and the Qt uses Sqlite2 only if you include the sqlite2, by default really appers to be sqlite3.
I will try try Sqlite Manager. -
wrote on 5 Jul 2012, 21:18 last edited by
With Firefox Plugin show my "usuario" table! So my code works!
Another detail: Sqliteman uses QT! -
wrote on 27 Aug 2013, 10:45 last edited by
Can someone give me and example or complete code of an app with sqlite please?
-
Hi and welcome to devnet,
Please don't revive old thread with new questions, open your own.
As for your question, have a look at the "Sql Example in Qt's documentation":http://qt-project.org/doc/qt-4.8/examples-sql.html
-
wrote on 15 Dec 2013, 21:30 last edited by
Its working. Just SQL part you using will generate error second time becouse table already exists. You could replace with CREATE TABLE IF NOT EXISTS ....
[quote author="dcbasso" date="1341519956"]Could be the version, I read something that says Sqliteman just open sqlite3 files...
I make this code, case study code?
@
#include "banco.h"
#include "QtSql/QtSql"
#include "QDebug"Banco::Banco(QObject *parent) :
QObject(parent)
{
QSqlDatabase db;
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
if (db.open()) {
QSqlQuery query(db);
if (query.exec("CREATE TABLE usuario ( INTEGER oid PRIMARY KEY);")) {
qDebug() << "criado tabela com sucesso.";
} else {
qDebug() << "erro ao criar tabela." << query.lastError();
}
} else {
qDebug() << "DB Nao aberto.";
}
db.close();
@- brazillian portuguese debug messages.
If I run this code twice, I got error saying that already exists the table "usuario".[/quote]