Results 1 to 6 of 6

Thread: Connect Sqlite database from Android device through the network

  1. #1
    Join Date
    Jan 2015
    Location
    Tremp, Spain
    Posts
    14
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Connect Sqlite database from Android device through the network

    I have a Qt Desktop application on Ubuntu that accesses a SQlite database and it works fine. Now I have started an app for Android that will connect via Wifi to the same database. I have installed Samba for file sharing, granted permissions to the database and tested file access from my Android device via ES File Explorer, all OK.
    The app deploys right in debug mode on Android device (Samsung Galaxy Tab 3 - SMT110), but when I try to connect:
    Qt Code:
    1. #include <QtSql/QSqlDatabase>
    2. #include <QMessageBox>
    3. #include <QSqlError>
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QString name = "\\\\192.168.1.33\\dades\\proves.db"; // Path to the database; folder 'dades' is shared
    2. db.addDatabase("QSQLITE");
    3. db.setDatabaseName(name);
    4. if (!db.open()) {
    5. QMessageBox::critical(0,tr("Connection"), tr("Unable to connect to %1").arg(name));
    6. QMessageBox::information(0, tr("Error"), db.lastError().text());
    7. QMessageBox::information(0, tr("Drivers availables"), QSqlDatabase::drivers().join("\n"));
    8. } else {
    9. QMessageBox::information(0,tr("Connection"), tr("Connected to %1 !!!").arg(name));
    10. }
    To copy to clipboard, switch view to plain text mode 
    Database is never opened, and I get "Driver not loaded" at message box "Error" and then "QSQLITE" at message box "Drivers availables".
    My questions are:
    If I have not forgotten anything, is it enough to share the database file on the network?
    If not, do I have to link QtNetwork to connect otherwise, e.g. via Samba protocol ("smb://192.168.1.33/dades/proves.db) ?

    Thank you all in advance.
    Last edited by Laureta; 10th February 2015 at 17:07. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Sqlite database from Android device through the network

    Driver not loaded sounds like you have not deployed the driver plugin as part of your application or the application can't find it.

    You also need to mount the Samba share and access the database through its local filename.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2015
    Location
    Tremp, Spain
    Posts
    14
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Connect Sqlite database from Android device through the network

    Quote Originally Posted by anda_skoa View Post
    You also need to mount the Samba share and access the database through its local filename.
    Does this mean install SAMBA on the android device?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Sqlite database from Android device through the network

    Quote Originally Posted by Laureta View Post
    Does this mean install SAMBA on the android device?
    No idea, maybe Android can mount Samba shares with a different mechanism.

    Your target is having the sqlite file as a local file, since SQLite is a local file database engine.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2015
    Location
    Tremp, Spain
    Posts
    14
    Thanks
    6
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Connect Sqlite database from Android device through the network

    Well, finally I managed to connect to the shared database. The cause was that the variable 'db'' must be a public member of the class, which is not properly documented nor in Qt documentation nor in the examples.
    When I changed to:

    MainWindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtSql/QSqlDatabase>
    6. #include <QSqlTableModel>
    7. #include <QMessageBox>
    8. #include <QSqlError>
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21. QSqlDatabase db; // declared as public
    22. void connectar();
    23.  
    24. private:
    25. Ui::MainWindow *ui;
    26. };
    27.  
    28. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    MainWindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QDebug>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10. connectar();
    11. model = new QSqlTableModel(this);
    12. model->setTable("families");
    13. QMessageBox::critical(0,"Error", model->lastError().text()); // throws "Unable to find table families" error
    14. ui->llista->setModel(model); // llista is QListView
    15. ui->llista->setModelColumn(1);
    16. ui->llista->show();
    17. connect(ui->btnClose, SIGNAL(clicked()), this, SLOT(close()));
    18. }
    19.  
    20. MainWindow::~MainWindow()
    21. {
    22. delete ui;
    23. }
    24.  
    25. void MainWindow::connectar() {
    26. db = QSqlDatabase::addDatabase("QSQLITE");
    27. QString nom = "\\\\192.168.1.33\\dades\\proves.db";
    28. db.setDatabaseName(name);
    29. if (!db.open()) {
    30. QMessageBox::critical(0,tr("Connexió"), tr("Unable to connect to %1").arg(name));
    31. } else {
    32. QMessageBox::information(0,tr("Connexió"), tr("Connected to %1 !!!").arg(name));
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 
    the connection is successfully established, but then the error "Unable to find table families" appears. Of course, I've made sure that the table exists in the database. Tests with other tables in the same database throw the same error.
    I am aware that Sqlite is not the proper database for sharing; in fact, I've always preferred MySQL in all my projects. But I would rather not to change the desktop application mentioned in my first post.
    So, if a fellow has networked SQLite databases, I appreciate his/her advice to address this latter problem (Unable to find table ...)
    Thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connect Sqlite database from Android device through the network

    Quote Originally Posted by Laureta View Post
    Well, finally I managed to connect to the shared database. The cause was that the variable 'db'' must be a public member of the class, which is not properly documented nor in Qt documentation nor in the examples.
    It is not documented because that is not a requirement.
    The connection doesn't have to be a member and it especially doesn't need to be public.

    Quote Originally Posted by Laureta View Post
    the connection is successfully established
    Are you sure it is accessing the database file on the Samba share?
    Your code above still looks like you are passing a Windows-style network drive path to the database handler.

    Cheers,
    _

Similar Threads

  1. Replies: 3
    Last Post: 8th March 2011, 07:57
  2. How to connect Qt to SQlite
    By visualo in forum Qt Programming
    Replies: 1
    Last Post: 21st October 2009, 22:24
  3. steps to connect to SQLite from Q
    By Qt Coder in forum Qt Programming
    Replies: 3
    Last Post: 8th July 2009, 12:12
  4. qt how to connect to sqlite db file
    By TomASS in forum Qt Programming
    Replies: 2
    Last Post: 22nd April 2009, 09:47
  5. No network when Modem Emulator connect to a real device
    By tommy_tang in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 10th December 2007, 08:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.