How to use Sqlite in Qt 5.7 with Android

Asked

Viewed 120 times

0

I would like to know how the communication with the database is done Sqlite on android using the Qsqldatabase? I performed some tests and for desktop I can perform the communication with BD, but with android I’m not able to perform the same communication.

  • Hello, welcome to SOPT. This site is not a forum. If you haven’t done it yet, do the [tour] and read [Ask]. When you say that "you are not able to carry out the communication", what happens? Any errors? Post the relevant code snippet (if possible a [mcve] that reproduces the problem) and explain in detail what its difficulty is.

  • I managed to solve this problem and published how I managed to solve!

1 answer

2


The author found the solution: below is what he posted:

After much break the head manage to find the problem! The problem occurred because I performed a check if the bank existed and as the bank on 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();
}

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.