我是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();
....
这会产生错误
libbbdata.so.1.0.0@_ZN2bb4data15AsyncDataAccess7executeERK8QVarianti+0x5)mapaddr = 0001d2ba。 REF = 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.movilexpres.testDev_movilexpreseb26522c/app/native/LaBolivianaCiacruz@_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 :(得分:2)
在这一行:
string sqlVersion = "update version set VERSION = " + ver;
你的ver
是一个整数吗?否则(在整数情况下也是!),您必须使用parameterized SQL命令:
QVariantList values;
values<<ver;
sda.execute("update version set VERSION = :ver", values);
答案 1 :(得分:1)
我可以在评论中加上这个,但我没有足够的代表
我认为罪魁祸首是
sda.execute(sqlVersion.c_str());
&#34; sqlVersion&#34;是std :: string,有时它不适用于级联API。它应该是QString
将其修改为:
sda.execute(QString(sqlVersion));
或只是做
QString sqlVersion = "update version set VERSION = " + ver;
sda.execute(sqlVersion);