由于插入查询中的单引号引起的错误 [英] error due to single quotes in insert query

查看:39
本文介绍了由于插入查询中的单引号引起的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 -

String query = "INSERT INTO genes (sent, title) VALUES ('"+sent+"','"+title+"')";
Statement stmt = con.createStatement();
int rs = stmt.executeUpdate(query);

其中 senttitle 是应用某种算法后提取的变量字符串.但是当 senttitle 包含单个 qoutes 时,这会导致 sql 错误.

where sent and title are variable strings extracted after applying some algorithm. But this gives sql error when sent or title contains single qoutes.

推荐答案

考虑使用带参数的准备好的语句:

Consider using a prepared statement with parameters:

PreparedStatement pstmt = con.prepareStatement(
    "INSERT INTO genes (sent, title) VALUES (?, ?)");
pstmt.setString(1, sent);
pstmt.setString(2, title);
pstmt.executeUpdate();

这篇关于由于插入查询中的单引号引起的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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