2013-04-26 121 views
0

我想首先創建數據文件夾中的臨時quizz.db,然後創建它裏面的表,然後創建的資產文件夾中的文件quizz.db它複製到文件夾資產。通過調試代碼,它顯示該文件夾已創建。但我無法在資產文件夾中找到它。下面是代碼,無法建立資產文件夾中的.db文件

#include <bb/cascades/Application> 
#include <bb/cascades/QmlDocument> 
#include <bb/cascades/AbstractPane> 
#include <bb/data/SqlDataAccess> 

using namespace bb::cascades; 
using namespace bb::data; 

SQLTest::SQLTest(bb::cascades::Application *app): QObject(app) 
{ 
const QString fileName = QString("quizz.db"); 
QString dataFolder = QDir::homePath(); 
QString newFileName = dataFolder + "/" + fileName; 
QTemporaryFile file(newFileName); 

// Open the file that was created 
if (file.open()) 
{ 
    // Create an SqlDataAccess object 
    SqlDataAccess sda(newFileName); 

    // Create a table called Employee in the database file 
    sda.execute("CREATE TABLE Employee(firstName VARCHAR(50),lastName VARCHAR(50), salary INT);"); 

    // Insert employee records into the table 
    sda.execute("INSERT INTO Employee (firstName, lastName, salary) VALUES (\"Mike\", \"Chepesky\", 42000);"); 
    sda.execute("INSERT INTO Employee (firstName, lastName, salary) VALUES (\"Westlee\", \"Barichak\", 55000);"); 
    sda.execute("INSERT INTO Employee (firstName, lastName, salary) VALUES (\"Ian\", \"Dundas\", 47000);"); 
    if(sda.hasError()) 
    { 

    } 
    else 
     copyFileToAssetsFolder("quizz.db"); 
} 
} 

void SQLTest::copyFileToAssetsFolder(const QString fileName) 
{ 
QString appFolder(QDir::homePath()); 
appFolder.chop(4); 
QString originalFileName = appFolder + "app/native/assets/" + fileName; 
QFile newFile(originalFileName); 
// If I enable this `if` condition the code satisfies it and removes the quizz.db file and then it satisfies the next `if` condition and successfully copies the quizz.db file from `data` folder to `assets` folder. 
/*if(newFile.exists()) 
    QDir().remove(originalFileName);*/ 
// this `if` condition is not satisfied. Which should mean the quizz.db file has been created on assets folder. 
if (!newFile.exists()) 
{ 
    // If the file is not already in the assets folder, we copy it from the 
    // data folder (read and write) to the assets folder (read only). 
    QString dataFolder = QDir::homePath(); 
    QString newFileName = dataFolder + "/" + fileName; 
    QFile originalFile(newFileName); 

    if (originalFile.exists()) 
    { 
     // Create sub folders if any creates the SQL folder for a file path like e.g. sql/quotesdb 
     QFileInfo fileInfo(originalFileName); 
     QDir().mkpath (fileInfo.dir().path()); 

     if(!originalFile.copy(originalFileName)) { 
      qDebug() << "Failed to copy file to path: " << originalFileName; 
     } 
    } else { 
     qDebug() << "Failed to copy file data base file does not exists."; 
    } 
} 

// mSourceInDataFolder = newFileName; 
} 

如果我能copyFileToAssetsFolder功能評論if條件它刪除已經創建的資產文件夾(IM無法找到它)quizz.db文件,並進入下一if條件和副本里面在data文件夾中創建quizz.db以資產fodler。但無論如何,我無法在資產文件夾中找到quizz.db。我很快需要幫助。謝謝。

回答

0

看來它不可能修改here提到現在我創建的數據文件夾我的數據庫文件,因爲它是可寫的資產文件夾。在資產

+1

.BAR的文件通常包含文件夾「資產」,我希望它從應用程序的家目錄鏈接到存檔,因爲你不能修改。 BAR(這意味着,您可以更改已簽名的應用程序...),您將無法更改資產目錄或其內容。那麼,你自己找到了你的解決方案,我只是想解釋這個問題的可能原因。 – Schlangi 2013-04-27 14:35:27

相關問題