数据库无法连接或更新

佩德罗·特兰|

我是Blackberry的新手,但是SQL连接有问题:当我尝试在db上执行查询时,我遇到了C ++错误,但是,当我使用QML中的方法时,它就像一种魅力。

so the thing is here:
/* Copyright (c) 2012 Research In Motion Limited.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "customsqldatasource.h"

int const CustomSqlDataSource::LOAD_EXECUTION = 0;

CustomSqlDataSource::CustomSqlDataSource(QObject *parent) :
        QObject(parent)
{

}

CustomSqlDataSource::~CustomSqlDataSource()
{
    delete mSqlConnector;
}

void CustomSqlDataSource::copyFileToDataFolder(const QString fileName)
{
    // Since we need read and write access to the file, it has
    // to be moved to a folder where we have access to it. First,
    // we check if the file already exists (previously copied).
    QString dataFolder = QDir::homePath();
    QString newFileName = dataFolder + "/" + fileName;
    QFile newFile(newFileName);

    if (!newFile.exists()) {
        // If the file is not already in the data folder, we copy it from the
        // assets folder (read only) to the data folder (read and write).
        QString appFolder(QDir::homePath());
        appFolder.chop(4);
        QString originalFileName = appFolder + "app/native/assets/" + fileName;
        QFile originalFile(originalFileName);

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

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

    mSourceInDataFolder = newFileName;
}


void CustomSqlDataSource::setSource(const QString source)
{
    if (mSource.compare(source) != 0) {
        // Copy the file to the data folder to get read and write access.
        copyFileToDataFolder(source);
        mSource = source;
        emit sourceChanged(mSource);
    }
}

QString CustomSqlDataSource::source()
{
    return mSource;
}

void CustomSqlDataSource::setQuery(const QString query)
{
    if (mQuery.compare(query) != 0) {
        mQuery = query;
        emit queryChanged(mQuery);
    }
}

QString CustomSqlDataSource::query()
{
    return mQuery;
}

bool CustomSqlDataSource::checkConnection()
{
    if (mSqlConnector) {
        return true;
    } else {
        QFile newFile(mSourceInDataFolder);

        if (newFile.exists()) {

            // Remove the old connection if it exists
            if(mSqlConnector){
                disconnect(mSqlConnector, SIGNAL(reply(const bb::data::DataAccessReply&)), this,
                        SLOT(onLoadAsyncResultData(const bb::data::DataAccessReply&)));
                delete mSqlConnector;
            }

            // Set up a connection to the data base
            mSqlConnector = new SqlConnection(mSourceInDataFolder, "connect");

            // Connect to the reply function
            connect(mSqlConnector, SIGNAL(reply(const bb::data::DataAccessReply&)), this,
                    SLOT(onLoadAsyncResultData(const bb::data::DataAccessReply&)));

            return true;

        } else {
            qDebug() << "CustomSqlDataSource::checkConnection Failed to load data base, file does not exist.";
        }
    }
    return false;
}

void CustomSqlDataSource::execute (const QString& query, const QVariantMap &valuesByName, int id)
{
    if (checkConnection()) {
        mSqlConnector->execute(query, valuesByName, id);
    }
}


void CustomSqlDataSource::load()
{

    if (mQuery.isEmpty() == false) {
        if (checkConnection()) {
            mSqlConnector->execute(mQuery, LOAD_EXECUTION);
        }
    }
}

void CustomSqlDataSource::onLoadAsyncResultData(const bb::data::DataAccessReply& replyData)
{
    if (replyData.hasError()) {
        qWarning() << "onLoadAsyncResultData: " << replyData.id() << ", SQL error: " << replyData;
    } else {

        if(replyData.id() == LOAD_EXECUTION) {
            // The reply belongs to the execution of the query property of the data source
            // Emit the the data loaded signal so that the model can be populated.
            QVariantList resultList = replyData.result().value<QVariantList>();
            emit dataLoaded(resultList);
        } else {
            // Forward the reply signal.
            emit reply(replyData);
        }
    }
}

这是我用作连接到sql的接口的cpp文件。

这是我从c ++调用SQL的地方

....
    string sqlVersion = "update version set VERSION = " + ver;
    CustomSqlDataSource dataToLoad;

    dataToLoad.setQuery(sqlVersion.c_str());
        dataToLoad.load();
....

这会产生一个错误

[email protected]_ZN2bb4data15AsyncDataAccess7executeERK8QVarianti+0x5)mapaddr = 0001d2ba。参考= 00000035

但是奇怪的是,当我从sql中使用它时,它工作得非常好,例如,我在varius qmls上使用它:

import bb.cascades 1.0
import com.lbc.data 1.0
import "customField"

Page {
    property string dropDownValue: "2"
    property string webViewText
    attachedObjects: [
        GroupDataModel {
            id: dataValueModel
            grouping: ItemGrouping.None
        },

        CustomSqlDataSource {
            id: asynkDataSource
            source: "sql/LBCData.db"
            query: "SELECT * FROM INFORMACION ORDER BY Id"
            property int loadCounter: 0

            onDataLoaded: {
                if (data.length > 0) {
                    dataValueModel.insertList(data);

                    var fetchColumData = dataValueModel.data([ dropDownValue ])
                    webViewText = fetchColumData.contenido
                    console.log(webViewText);
                }
            }
        }
    ]

    onCreationCompleted: {
        asynkDataSource.load();
    }

    Container {
        CustomHeader {
            text: "Tipo de Seguro:"
        }
        DropDown {
            id: dropdownVal
            horizontalAlignment: HorizontalAlignment.Center
            preferredWidth: 550
            Option {
                value: "1"
                text: "SEGUROS GENERALES"
            }
            Option {
                value: "2"
                text: "SEGUROS AUTOMOTORES"
                selected: true
            }
            Option {
                value: "5"
                text: "SEGUROS PERSONALES"
            }
            onSelectedValueChanged: {
                console.log("Value..." + dropdownVal.selectedOption.value);
                dropDownValue = dropdownVal.selectedOption.value;
                asynkDataSource.load();
            }
        }
        ScrollView {
            id: scrollView
            scrollViewProperties {
                scrollMode: ScrollMode.Vertical
            }
            WebView {
                id : webView
                html: webViewText
                settings.defaultFontSize: 42
                settings.minimumFontSize: 16


            }
        }
    }
}

如果有人对它的想法有所了解,请让我知道,这是我第一次使用bb10,也是我第一次使用矩论。

编辑1:我添加行来设置查询,但生成copyfile错误,似乎错误是文件

dataToLoad.setSource("lbc/LBCData.db");


Failed to copy file data base file does not exists. 
Process 43913393 (LaBolivianaCiacruz) terminated SIGSEGV code=1 fltno=11 ip=fffffb34

编辑2:现在我正在使用以下代码,该代码基于官方纪录片,不同之处在于我不喜欢file.open,因为它被标记为类型文件的无效函数。控制台返回hasError上的no Error消息,但是在崩溃之后,我对其进行了检查,并且sql语句已执行且处于良好状态,但是无论如何该应用程序都会崩溃。它返回以下错误:

进程115667153(LaBolivianaCiacruz)终止了SIGSEGV代码= 1 fltno = 11 ip = 0805f27a(/accounts/1000/appdata/com.lbc.movi​​lexpres.testDev_movilexpreseb26522c/app/native/[email protected]_ZNSs6appendERKSsjj+09)

QDir home = QDir::home();
    copyfiletoDir("sql/LBCData.db");
    bb::data::SqlDataAccess sda(home.absoluteFilePath("sql/LBCData.db"));
//  QFile file(home.absoluteFilePath("sql/LBCData.db"));
//  if(file.open());
        sda.execute(sqlVersion.c_str());
if(sda.hasError()){
            DataAccessError theError = sda.error();
            if (theError.errorType() == DataAccessErrorType::SourceNotFound)
                qDebug() << "Source not found: " + theError.errorMessage();
            else if (theError.errorType() == DataAccessErrorType::ConnectionFailure)
                qDebug() <<  "Connection failure: " + theError.errorMessage();
            else if (theError.errorType() == DataAccessErrorType::OperationFailure)
                qDebug() <<  "Operation failure: " + theError.errorMessage();
        } else {
            qDebug() << "No error.";
        }
LS_ᴅᴇᴠ

在这一行:

string sqlVersion = "update version set VERSION = " + ver;

ver整数吗 否则(在整数情况下也是如此!),必须使用参数化的SQL命令:

QVariantList values;
values<<ver;
sda.execute("update version set VERSION = :ver", values);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章