Problema connessione SQLite
-
ciao!
scusate ma forse sono io che sono stanco, ma non riesco ad usare un db sqlite.
ho aggiunto il riferimento nel file .pro e lanciato il qmake:QT += core sql gui
poi ho creato questa semplice classe:
#include "database.h" Database::Database() { db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("/home/matte/Desktop/sync.db"); if (db.isOpen()) { qDebug() << "OK"; } else { qDebug() << db.lastError(); } } Database::~Database() { //db.close(); }
quando la lancio mi esce sempre questo:
QSqlError("", "", "")
inuitile dire che il file sta li dove l'ho impostato nel codice.
ed è funzionante.
dove sto sbagliando??? -
Il plugin per SQLite viene trovato dall'applicazione?
P.S.
db = QSqlDatabase::addDatabase
. Sperodb
non sia un membro diDatabase
. da http://doc.qt.io/qt-5/qsqldatabase.html :Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.
-
non avevo visto quel warning.
cmq ho modificato così:
#ifndef DATABASE_H #define DATABASE_H #include <QSqlDatabase> #include <QSqlError> #include <QDebug> class Database { public: Database(); virtual ~Database(); }; #endif // DATABASE_H #include "database.h" Database::Database() { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("/run/media/matte/PUFFA/Project/CPP/QRysnc/sync.db"); if (db.isOpen()) { qDebug() << "OK"; } else { qDebug() << db.lastError(); } qDebug() << db.isDriverAvailable("QSQLITE"); } Database::~Database() { //db.close(); }
in console mi esce questo:
QSqlError("", "", "") true
quindi mi sembra che il driver lo trovi.
però continua a darmi l'errore! -
ciao!
mi sembra sia tutto installato.
cmq ti posto l'output (sto su arch linux):# pacman -Ss sqlite core/sqlite 3.25.2-1 [installato] A C library that implements an SQL database engine core/sqlite-analyzer 3.25.2-1 An analysis program for sqlite3 database files core/sqlite-doc 3.25.2-1 most of the static HTML files that comprise this website, including all of the SQL Syntax and the C/C++ interface specs and other miscellaneous documentation core/sqlite-tcl 3.25.2-1 sqlite Tcl Extension Architecture (TEA) extra/gom 0.3.3-2 A GObject to SQLite object mapper extra/perl-dbd-sqlite 1.58-2 Self-contained RDBMS in a DBI driver extra/php-sqlite 7.2.11-3 sqlite module for PHP extra/redland-storage-sqlite 1:1.0.17-4 SQLite storage support for Redland community/gambas3-gb-db-sqlite3 3.11.4-2 (gambas3) Sqlite3 database access component community/haskell-persistent-sqlite 2.8.2-39 Backend for the persistent library using sqlite3 community/javasqlite 20150419-1 Java support for SQLite database engine community/libdbi-drivers 0.9.0-6 Database drivers for libdbi (MySQL, PostgreSQL, and SQLite) community/libspatialite 4.3.0.a-4 SQLite extension to support spatial data types and operations community/perl-dbd-sqlite2 0.37-6 Perl/CPAN Module DBD::SQLite2 community/perl-djabberd-rosterstorage-sqlite 1.00-7 DJabberd sqlite roster storage module community/python-apsw 3.24.0-2 Python wrapper for SQLite community/python-minidb 2.0.2-2 A simple SQLite3 store for Python objects community/python-sqlitedict 1.6.0-1 Persistent dict in Python, backed up by sqlite3 and pickle, multithread-safe community/python2-apsw 3.24.0-2 Python2 wrapper for SQLite community/python2-pysqlite 2.8.3-1 A Python DB-API 2.0 interface for the SQLite embedded relational database engine community/sqlcipher 3.4.2-1 SQLite extension that provides transparent 256-bit AES encryption of database files community/sqlitebrowser 3.10.1-1 [installato] SQLite Database browser is a light GUI editor for SQLite databases, built on top of Qt community/vsqlite++ 0.3.13-5 SQLite wrapper library for C++ community/wxsqlite3 4.2.0-1 wxWidgets wrapper for SQLite3 multilib/lib32-sqlite 3.25.2-1 A C library that implements an SQL database engine (32-bit)
da questo codice:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("/run/media/matte/PUFFA/Project/CPP/QRysnc/sync.db"); qDebug() << db.isOpen(); qDebug() << db.lastError(); qDebug() << db.isDriverAvailable("QSQLITE"); qDebug() << db.drivers();
mi esce questo:
false QSqlError("", "", "") true ("QIBASE", "QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7", "QTDS", "QTDS7")
giusto per dare qualche info in più!
-
ultimo tentativo prima di abbandonare / cambiare db (ma non saprei quale visto che non deve essere un db remoto).
per levare ogni dubbio, faccio anche un controllo sull'esistenza del file db (giusto per debug):
Database::Database() { QString fileDb = QDir::currentPath() + "sync.db"; if (QFileInfo(fileDb).exists()) { QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(fileDb); qDebug() << "Database aperto: " << db.isOpen(); qDebug() << "Ultimo errore: " << db.lastError(); qDebug() << "Il driver è disponibile: " << db.isDriverAvailable("QSQLITE"); qDebug() << "Lista dei drivers: " << db.drivers(); } else { qDebug() << "Database non trovato"; } }
ma la risposta è sempre la stessa:
Databse aperto: false Ultimo errore: QSqlError("", "", "") Il driver è disponibile: true Lista dei drivers: ("QIBASE", "QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7", "QTDS", "QTDS7")
sqlite è installato, e riesco ad aprire il db:
$ sqlite3 sync.db SQLite version 3.25.2 2018-09-25 19:08:10 Enter ".help" for usage hints. sqlite> .tables sync sqlite>
a me non viene in mente più altro!
-
ciao!
se intendi questo:
Database::Database() { QString fileDb = "sync.db"; QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(fileDb); qDebug() << "Database aperto: " << db.isOpen(); qDebug() << "Ultimo errore: " << db.lastError(); qDebug() << "Il driver è disponibile: " << db.isDriverAvailable("QSQLITE"); qDebug() << "Lista dei drivers: " << db.drivers(); }
non è cambiato nulla.
non viene creato niente nella cartella dell'eseguibile.
e l'output è sempre lo stesso! -
non so se è perchè ho visto vari esempi in giro sbagliati, o semplicemente perchè mi sono concentrato su altro.
ma in verità mi sembra di aver risolto semplicemente aggiungendo questa riga:db.open();
tornando all'esempio iniziale:
QString fileDb = QDir::currentPath() + "/sync.db"; QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(fileDb); db.open(); if (db.isOpen()) { qDebug() << "OK"; } else { qDebug() << db.lastError(); }
questo funziona senza problemi.
possibile che non ci abbiamo fatto caso??