数据库不连接或更新
db doesn't connect or update
我是blackberry的新手,SQL连接有问题:当我试图在数据库上执行查询时,我遇到了来自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)映射地址=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,也是第一次使用momentics。
编辑1:我添加了设置查询的行,但生成了复制文件错误,似乎错误是文件
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(LaBolianaCiacruz)终止SIGSEGV代码=1 fltno=11 ip=0805f27a(/accounts/1000/appdata/com.lbc.moviexpress.testDev_movilexpresse26522c/app/native/LaBolivianaCiacruz@_ZNSs6appendERKSsjj+0x4e5)参考=8b00020a9
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.";
}
在行中:
string sqlVersion = "update version set VERSION = " + ver;
您的ver
是整数吗?否则(在整数情况下也是!),您必须使用参数化的SQL命令:
QVariantList values;
values<<ver;
sda.execute("update version set VERSION = :ver", values);
我可以在评论中添加这个,但我没有足够的代表
我认为罪魁祸首是
sda.execute(sqlVersion.c_str());
"sqlVersion"是std::string,有时它不适用于级联API。应该是QString
修改为:
sda.execute(QString(sqlVersion));
或者只是做
QString sqlVersion = "update version set VERSION = " + ver;
sda.execute(sqlVersion);
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 更新的矢量元素不打印
- CMake 错误"源似乎不包含 CMakeLists.txt",路径/库连接问题
- 在不知道套接字的情况下关闭网络连接
- 传递 std::vector of std::shared_ptr,而不是更新对象
- 为什么 c++ 映射值不针对同一键更新?
- 在 3ds Max 中更新进度条后,环境和效果 UI 不刷新
- QT信号在虚拟机中不连接
- QThread信号发射不连接
- 是STL中的联合获取(或不连接集)数据结构
- 从具有本地 std::thread 对象而不连接的函数退出
- QT连接在构造函数中不连接
- 当int为char时,C++字符串不连接char
- 数据库不连接或更新
- 使用stlplus库在有线网络中连接,在无线网络中不连接
- 如何在“连接的零部件”标签中使用“不连接集”
- c++ 11线程不连接
- 为什么类中的静态数据成员在发送到函数时更新不正确
- 为什么在C++当我连接带有"+"的字符串时,就像它不连接而是删除字符串的第一个字符
- QListView更新-不触发更新