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

标签 qt sqlite qwebview

有没有办法在 qwebview 中使用预填充的 sqlite 数据库?我有一个使用该数据库的 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。

关于qt - 如何在 qwebview 中使用预填充的 sqlite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28695772/

相关文章:

c++ - 如何创建语音聊天应用程序(SIP 协议(protocol))

c++ - 构造函数 MyClass(QWidget *parent = 0) 中 (QWidget *parent = 0) 的含义;

c++ - QTabBar图标位置

android - 获取导致 SQLiteConstraintException 的约束名称

python - QWebKit linkClicked 信号从不触发

html - Qt:让QWebview返回一个完整的URL

c# - QNetworkAccessManager->在 DLL 中调用时卡住

ubuntu - 为什么 apt-get install libsqlite3-dev 给我一个 404 错误?

android - 如何将 GCM 推送通知消息存储到 sqlite 数据库中?

c++ - Qt QWebEngineView 不允许加载本地资源