How to use SQLite in Qt 5.7 with Android [closed]

0

Would you like to know how to communicate with the SQLite database on android using QSqlDatabase ? I have done some tests and for desktop I can communicate with DB, but with android I am not able to make the same communication.

    
asked by anonymous 20.09.2016 / 17:21

1 answer

1

The author found the solution:

After much breaking the head can find the problem! The problem occurred because I performed a check if the bank existed and as the bank on the android is not specifically in the same folder as the app it returned that the bank did not exist. This way I'm posting is working on Android and Windows.

DBConnection header

#ifndef DBCONNECTION_H
#define DBCONNECTION_H

#include <QObject>
#include <QSqlDatabase>
#include <QSqlError>

enum TypeDBConnection{
 QDB2,
 QIBASE,
 QMYSQL,
 QOCI,
 QODBC,
 QPSQL,
 QSQLITE,
 QSQLITE2,
 QTDS
};

class DBConnection
{
protected:
  QSqlDatabase db;
  QString dbname;
  TypeDBConnection type;
public:
  QString getDBName();
  TypeDBConnection getTypeConnection();
  DBConnection *setDBName(QString value);
  DBConnection *setTypeConnetion(TypeDBConnection value);
  bool open();
  void close();
};
#endif // DBCONNECTION_H

DBConnection Class

#include "dbconnection.h"

QString DBConnection::getDBName()
{
    return this->dbname;
}
TypeDBConnection DBConnection::getTypeConnection()
{
    return this->type;
}
DBConnection *DBConnection::setDBName(QString value)
{
    this->dbname = value;
    return this;
}
DBConnection *DBConnection::setTypeConnetion(TypeDBConnection value)
{
    this->type = value;
    return this;
}
bool DBConnection::open()
{
    switch (this->type) {
    case QDB2:
        this->db = QSqlDatabase::addDatabase("QDB2");
        break;
    case QIBASE:
        this->db = QSqlDatabase::addDatabase("QIBASE");
        break;
    case QMYSQL:
        this->db = QSqlDatabase::addDatabase("QMYSQL");
        break;
    case QOCI:
        this->db = QSqlDatabase::addDatabase("QOCI");
        break;
    case QODBC:
        this->db = QSqlDatabase::addDatabase("QODBC");
        break;
    case QPSQL:
        this->db = QSqlDatabase::addDatabase("QPSQL");
        break;
    case QSQLITE:
        this->db = QSqlDatabase::addDatabase("QSQLITE");
        break;
    case QSQLITE2:
        this->db = QSqlDatabase::addDatabase("QSQLITE2");
        break;
    case QTDS:
        this->db = QSqlDatabase::addDatabase("QTDS");
        break;
    default:
        this->db = QSqlDatabase::addDatabase("QSQLITE");
        break;
    }

    this->db.setDatabaseName(this->dbname);
    //não é necessário validar o caminho do BD em android, pois o mesmo o cria!
    //if (QFile::exists(this->dbname))
    try
    {
        return db.open();
    }catch(...)
    {
        qDebug() << db.lastError().text();
    }
    return false;
}
void DBConnection::close()
{
    db.close();
}

Using the class

#include <QGuiApplication>
#include <QDebug>
#include "dbconnection.h"

int main(int argc, char *argv[])
{    
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine(QUrl("qrc:/main.qml"));

    DBConnection db* = new DBConnection();

    qDebug() << "Comunicou com bd: " << db->setDBName("Data.db")->setTypeConnetion(QSQLITE)->open();

    delete(db);

    return app.exec();
}
    
23.09.2016 / 19:13