2013-12-17 52 views
2

我是黑莓手机新手,并且遇到了SQL连接问题:当我尝试在db上执行查询时,我有和C++中的错误,但是当我使用来自QML的方法就像魅力一样。db没有连接或更新

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++

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

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

这将产生错误

[email protected]_ZN2bb4data15AsyncDataAccess7executeERK8QVarianti+0x5)mapaddr = 0001d2ba调用SQL。 REF = 00000035

,但奇怪的是,当我从SQL消耗它,它完美的作品伟大的,我消耗它杂色山雀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: I'va增加了行来设置查询,但产生的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错误消息,但在崩溃之后,我检查了它,并且执行了sql语句,并且状态良好,但是无论如何应用程序崩溃了。它给回以下错误:

过程115667153(LaBolivianaCiacruz)终止SIGSEGV代码= 1 fltno = 11 IP = 0805f27a(/accounts/1000/appdata/com.lbc.movi​​lexpres.testDev_movilexpreseb26522c/app/native/[email protected]_ZNSs6appendERKSsjj + 0x4e5)REF = 8b0020a9

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."; 
     } 
+0

我还没看着它那么久,但不是你querry:),你必须使用一个parameterized SQL命令“更新版本集版本=” +版本 – Richard

+0

不要”我认为是这样,我在Android和iOS应用上使用了相同的查询,现在我正在为bb10开发并且完美地工作,同时我也对查询进行了评论,之后那些来自web服务并且同样崩溃的查询,以及他们正在工作,因为他们来自Android和iOS平台从 –

+0

我不能真正帮助你深入,但如果你知道它的工作方式''但奇怪的是,当我从SQL使用它,它非常好,我很好例如,在var qmls上使用它:'您可以更详细地研究它,并将其与麻烦的代码进行比较。 – Andrew

回答

2

在这一行:

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

是您ver整数?否则(并在整数情况下!从C调用时++设置为

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

我可以添加这个上发表评论,但我没有足够的代表

我认为罪魁祸首是

sda.execute(sqlVersion.c_str()); 

“sqlVersion”是std :: string有时它不支持级联API。应当即QString

修改它想:

sda.execute(QString(sqlVersion)); 

或只是做

QString sqlVersion = "update version set VERSION = " + ver; 
sda.execute(sqlVersion);