我试图通过首先quizz.db
在数据文件夹中创建一个临时文件夹然后在其中创建表然后将其复制到资产文件夹来在资产文件夹中创建一个 quizz.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;
}
如果我启用函数的注释if
条件,copyFileToAssetsFolder
它将删除资产文件夹中已创建的 quizz.db 文件(我无法找到)并进入下一个if
条件并将文件夹上创建的 quizz.dbdata
复制到资产文件夹。但无论如何我都无法在资产文件夹中找到 quizz.db。我真的很快需要帮助。谢谢。