qt – 如何在qwebview中使用预先填充的sqlite数据库?

前端之家收集整理的这篇文章主要介绍了qt – 如何在qwebview中使用预先填充的sqlite数据库?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法使用预先填充的sqlite数据库和qwebview?我有一个使用该数据库JavaScript应用程序.

我启用了离线存储,

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

为它设置一个舒适的大小

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

并设置位置:

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

数据库文件从qrc资源文件复制到该位置并不起作用;

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

如何进行?

谢谢.

解决方法

在复制之前验证目标路径中的文件是否不存在

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)
   }

更新的答案:
创建了一个简单的项目

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

MAINQ.db是我的sqlite文件.
和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();
 }

我们已经连接到我们的数据库并执行一些查询.我们验证文件是否存在,如果不存在,我们将其复制到某个位置.我们可以将sql的结果赋给变量,然后在WebView等中将它们用作String.

猜你在找的Sqlite相关文章