5

Is there a way to use a pre-populated sqlite database with a qwebview? I have a javascript application that makes use of that database.

I've enabled offline storage,

QWebSettings::globalSettings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);

set a confortable size to it

QWebSettings::setOfflineStorageDefaultQuota(20*1024*1024);

and set location:

QWebSettings::globalSettings()->setOfflineStoragePath(QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/data/myapp");

Copying the database file from qrc resource file to that location doesn't do the trick;

QFile::copy(":/mydatabase.db" , QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/data/myapp/mydatabase.db");

how to proceed?

Thank you.

pr.nizar
  • 641
  • 6
  • 20
  • Are you sure mydatabase.db does not already exist at /data/myapp/ ? Because QFile will not overwrite file if it exist at destination path, and should return false. Try to print out (QStandardPaths::GenericDataLocation)+"/data/myapp/mydatabase.db" and see if you get doubled /data/myapp/ in path. – ares777 Mar 05 '15 at 12:49
  • See updated answer for an example using sql from local file and copy src to dest. – ares777 Mar 06 '15 at 10:44

1 Answers1

2

Verify if file in destination path does not exist before copy

     const QString filedest = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/data/app/") + "mydatabase.db";
if (!QFile::exists(filedest)) {
   // you can use  QVERIFY(QFile::copy(src, filedest)); 
   QFile::copy(src, filedest)
   }

Updated answer: Created a simple project

   QT += core gui
   QT += sql
   QT += webkitwidgets
   greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
   TARGET = aawa
   TEMPLATE = app
   SOURCES += main.cpp
   DISTFILES += \
      ../Desktop/before/MAINQ.db

where MAINQ.db is my sqlite file. And main.cpp

   #include <QApplication>
   #include <qgraphicsscene.h>
   #include <QGraphicsView>
   #include <QVBoxLayout>
   #include <QPushButton>
   #include <QStandardPaths>
   #include <QFile>
   #include <QtSql>
   #include <QFileInfo>

   int main(int argc, char* argv[]){
   QApplication app(argc, argv);
   QGraphicsScene* scene = new QGraphicsScene;
   QGraphicsView* view = new QGraphicsView(scene);
   //scene->setBackgroundBrush((Qt::white);

   QWidget *widget = new QWidget;
    view->setBackgroundBrush(Qt::yellow);
    QVBoxLayout* vlayout = new QVBoxLayout(widget);

    vlayout->addWidget(view);
    vlayout->addWidget(new QPushButton("print"));
    printf("ok..");
    QSqlDatabase db;
    db =  QSqlDatabase::addDatabase("QSQLITE");
   //now full path of my db
    db.setDatabaseName("/Users/Ioan/Desktop/before/MAINQ.db");
    if (db.open()) {
   qDebug() << "tabele in db " << db.tables();
   QSqlQuery query(db);
       query.prepare( "SELECT id FROM user_cards");
      if( !query.exec() )
        qDebug() << query.lastError();
      else
      {
        qDebug( "Selected!" );
        QSqlRecord rec = query.record();
         int cols = rec.count();
          for( int c=0; c<cols; c++ )
          qDebug() << QString( "Column %1: %2" ).arg( c ).arg( rec.fieldName(c) );

        for( int r=0; query.next(); r++ )
          for( int c=0; c<cols; c++ )
            qDebug() << QString( "Row %1, %2: %3" ).arg( r ).arg( rec.fieldName(c) ).arg( query.value(c).toString() );
      }
      qDebug() << "nice.";
   }


    else {
         qDebug() << "DB not open.";
         }

          const QString src = "/Users/Ioan/Desktop/before/MAINQ.db";      
   db.close(); // for close connection
   const QString filedest = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/data/app/") + "mydatabase.db";
   QFile file( filedest );
    if( !file.exists() )
    {
      qDebug() << "The file" << file.fileName() << "does not exist.";
    //  return 0;
    } else {

          QFile::copy(file, filedest);
    }


 widget->show();
return app.exec();
 }

There we have connected to our db and execute some query. We verify if file exists, if not we copy it to some location. The result from sql we can assign to variables, then use them as String in WebView etc.

ares777
  • 3,590
  • 1
  • 22
  • 23
  • That's not quite what I wanted: I wanted to use the database as a HTML5 database (Web SQL Database). I believe I have to use the `QWebDatabase` Class and copy from that database I copied from resource file to the database used by webkit (I can have access to it like in [here](http://doc.qt.io/qt-5/qwebdatabase.html#fileName)).. I wanted a direct method. Can you think of one? – pr.nizar Mar 09 '15 at 07:10
  • 1
    As in this example http://cadswes2.colorado.edu/~philw/2009/QtDocs/QtHybridWebNativeDevelopment_Whitepaper.pdf we cand use openDatabase. Take a look also to http://qt-project.org/doc/qt-4.8/qtwebkit-guide-cache.html where you can use window.localStorage; to access a stored client db. I can not think to something more direct access method. – ares777 Mar 09 '15 at 09:40