热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何向sql数据库添加包含双引号的字符串?-Howtoaddastringcontaingadoublequotetoasqldatabase?

IwanttoaddastringtomysqldatabaseusingJDBC.Buttheproblemisthatwheneverthestringco

I want to add a string to my sql database using JDBC. But the problem is that whenever the string contains a double quote, then the sql command is interpreted completely differently and a "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax" gets thrown."

我想使用JDBC向sql数据库添加一个字符串。但问题是,每当字符串包含双引号时,sql命令的解释就会完全不同,“sql语法中有错误”;检查与MySQL服务器版本对应的手册,找到正确的语法“被抛出”。

For example, String ssql = "INSERT INTO tableName VALUES (\""+ string + "\")";

例如,String ssql = "插入到tableName值(\" \" + String + "\" \")";

if string = "abc", then sql = INSERT INTO tableName VALUES ("abc")
But if string = "ab\"cd", then sql = INSERT INTO tableName VALUES ("ab"c")


And hence for a string that contains a double quote, the sql command is interpreted completely differently.

如果字符串= "abc",则sql =插入到tableName值("abc")中,但如果字符串= "ab\"cd",则sql =插入到tableName值("ab"c")中,因此对于包含双引号的字符串,sql命令的解释完全不同。

How can I add such a string to the database.

如何向数据库中添加这样的字符串。

PS. I cannot afford to change the double quote to a single quote. And there can be other hacks to add such a string but I want to know if there really is no such way of adding such a string directly.

我不能把双重报价改为单一报价。还有其他方法可以添加这样的字符串但我想知道是否真的没有这种直接添加这样的字符串的方法。

5 个解决方案

#1


2  

You need to escape the string value to make a string literal in the query.

您需要转义字符串值,以便在查询中创建字符串文字。

When you are using quotation marks to delimiter the string:

当您使用引号分隔字符串时:

  • A backslash (\) in the string should be replaced by two backslashes.

    字符串中的反斜杠(\)应该被两个反斜杠替换。

  • A quotation mark (") in the string should be replaced by a backslash and a quotation mark.

    字符串中的引号(")应该用反斜杠和引号替换。

If you would use apostrophes (') to delimiter the string (which is more common in SQL), you would escape the apostrophes instead of the quotation marks.

如果您使用撇号(')来分隔字符串(在SQL中更常见),您将转义撇号,而不是引号。

If possible, you should use parameterised queries, instead of concatenating the values into the queries.

如果可能,您应该使用参数化查询,而不是将值连接到查询中。

#2


1  

You're experiencing benign SQL injection. Be lucky string was not Little Bobby Tables.

您正在经历良性SQL注入。幸运之弦不是小波比表。

Instead you should use parameterized queries like INSERT INTO tableName VALUES (?) and send the value of string via a parameter.

相反,您应该使用参数化查询,如插入到tableName值(?)中,并通过参数发送字符串值。

For more details, see the JDBC documentation at http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

有关详细信息,请参阅http://docs.oracle.com/javase/tutorial/jdbc/basics/preparedhtml的JDBC文档


Now, to answer the question: Assuming the string does not come from a user source and instead you are trying to write the query yourself, you can replace " with \" in the string before concatenating.

现在,要回答这个问题:假设字符串不是来自用户源,而是您自己尝试编写查询,那么您可以在连接之前在字符串中替换“\”。

#3


0  

Per MySQL Spec it recignizes \" as escaping double quote. you can as well use two double quote "" to escape double quote " character

根据MySQL规范,它接收到“作为转义双引号”。您也可以使用两个双引号“”来转义双引号“字符”

\" A double quote (“"”) character

一个双引号(" " ")字符

#4


0  

change this

改变这一切

String ssql = "INSERT INTO tableName VALUES (\""+ string + "\")";

字符串ssql = "插入到tableName值(\" + String + "\" \")";

to

String ssql = "INSERT INTO tableName VALUES ('"+ string + "')";

Or simplify it with a PreparedStatement like this

或者用这样的PreparedStatement化简它

String ssql = "INSERT INTO tableName VALUES (?)";
preparedStatement.setString(1,"string");

When you want to insert double-quotes you need to escape them something like this

当你想插入双引号时,你需要避开它们。

String ssql = "INSERT INTO tableName VALUES (?)";
preparedStatement.setString(1,"\"string\"");

This shall insert "string" in the table

这将在表中插入“字符串”。

#5


0  

I tried adding the string by escaping the double quote but that didn't work. The only way I could add the string is by using PreparedStatement instead of a Statement class.

我试图通过转义双引号来添加字符串,但这行不通。添加字符串的唯一方法是使用PreparedStatement而不是Statement类。

String string = "abc\"abc";
String sql = "INSERT INTO tableName VALUES(?)";

cOnnection= DriverManager.getConnection(DB_URL, USER, PASS);
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, string);
preparedStatement.executeUpdate();

This code segment successfully adds the string containing the double quote into the database.

这个代码段成功地将包含双引号的字符串添加到数据库中。


推荐阅读
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • WebSocket与Socket.io的理解
    WebSocketprotocol是HTML5一种新的协议。它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
author-avatar
失心人2702939300
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有