2013-12-17 54 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);