QT QxOrm update_by_query更新数据不成功
QxOrm update_by_query更新数据不成功,是什么原因呢?
daoError = qx::dao::update_by_query
功能:通过查找bookname等于yuwen2的信息,修改book_price的信息
book bookInfo2; // qx::QxSqlQuery query_mo("WHERE book_name = :book_name"); //默认前面是 select * from user qx::QxSqlQuery query_mo("WHERE book_name = 'yuwen2'"); //query_mo.bind(":book_name","yuwen2"); qx_query qx2; qx2.where("book_name").isEqualTo("yuwen2"); daoError = qx::dao::fetch_by_query(query_mo,list_of_book); //bookInfo2.id =18; //bookInfo2.book_name = "yuwen2b"; bookInfo2.book_price="6.661"; QStringList aa={"book_price"}; daoError = qx::dao::update_by_query(query_mo,bookInfo2,NULL,aa,true);
/*! * \ingroup QxDao * \brief Update an element or a list of elements into database (adding a user SQL query to the default SQL query builded by QxOrm library) * \param query Define a user SQL query added to default SQL query builded by QxOrm library * \param t Element (or list of elements) to be updated into database * \param pDatabase Connection to database (you can manage your own connection pool for example, you can also define a transaction, etc.); if NULL, a valid connection for the current thread is provided by qx::QxSqlDatabase singleton class (optional parameter) * \param columns List of database table columns (mapped to properties of C++ class T) to be updated (optional parameter) * \param bUseExecBatch If true then use the QSqlQuery::execBatch() method to improve performance updating a list of instances in database * \return Empty QSqlError object (from Qt library) if no error occurred; otherwise QSqlError contains a description of database error executing SQL query * * qx::dao::update_by_query<T>() execute following SQL query :<br> * <i>UPDATE my_table SET my_column_1 = ?, my_column_2 = ?, etc.</i> + <i>WHERE my_query...</i> */ template <class T> inline QSqlError update_by_query(const qx::QxSqlQuery & query, T & t, QSqlDatabase * pDatabase = NULL, const QStringList & columns = QStringList(), bool bUseExecBatch = false) { return qx::dao::detail::QxDao_Update<T>::update(query, t, pDatabase, columns, bUseExecBatch); }
打印输出:
[QxOrm] sql query (total: 0.551 ms, db_exec: 0.183 ms, db_prepare: 0.134 ms, db_next(0): 0 ms, db_open: 0.0055 ms, db_transaction: 0.162 ms, build_relations: 0 ms, build_sql: 0.0037 ms, build_cpp: 0 ms, read_cpp: 0.0234 ms) :
UPDATE book_info
SET id = :id, book_price = :book_price
WHERE id = :id_bis
AND book_name = 'yuwen2'
采用普通语句代替咯:
//普通查询语句 QString price_value ="7.78"; QString com_sql =QString("UPDATE book_info SET book_ibsn='ibsn1',book_price='%1' WHERE book_name='yuwen2'").arg(price_value); qx_query querySql_normal(com_sql); daoError = qx::dao::execute_query(querySql_normal,bookInfo2); if(daoError.type()==QSqlError::NoError) { qDebug()<<"数据更新成功by bookname"; } else { qDebug()<<"数据更新失败by book name"<<daoError.text(); return; }
欢迎讨论,相互学习。
cdtxw@foxmail.com