将包含“`"或“'"的字符串插入数据库表 - Qt [英] Insert Strings that contain “ ` ” or “ ' ” to the database table - Qt

查看:59
本文介绍了将包含“`"或“'"的字符串插入数据库表 - Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须向 MySQL 数据库插入一些字符串.问题是每次我使用`"或'"时,它都会导致 QSqlquery 执行出错.我怎样才能防止这种情况?

I have to insert some strings to MySQL database. The problem is that every time I use " ` " or " ' " it causes errors in the QSqlquery execution. How can I prevent this?

推荐答案

在运行查询时始终使用绑定变量,SQL 查询中的特殊字符永远不会出现问题.以下是文档中的示例:

Always use bind variables when running your query and you will never have problems with special characters in SQL queries. Here is an example from the documentation:

QSqlQuery query;
query.prepare("INSERT INTO person (id, forename, surname) "
              "VALUES (:id, :forename, :surname)");
query.bindValue(":id", 1001);
query.bindValue(":forename", "Bart");
query.bindValue(":surname", "Simpson");
query.exec();

这篇关于将包含“`"或“'"的字符串插入数据库表 - Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆