简体   繁体   English

使用预准备语句的变量列名

[英]Variable column names using prepared statements

I was wondering if there was anyway to specify returned column names using prepared statements.我想知道是否可以使用准备好的语句指定返回的列名。

I am using MySQL and Java.我正在使用 MySQL 和 Java。

When I try it:当我尝试时:

String columnNames="d,e,f"; //Actually from the user...
String name = "some_table"; //From user...
String query = "SELECT a,b,c,? FROM " + name + " WHERE d=?";//...
stmt = conn.prepareStatement(query);
stmt.setString(1, columnNames);
stmt.setString(2, "x");

I get this type of statement (printing right before execution).我得到这种类型的语句(在执行前打印)。

SELECT a,b,c,'d,e,f' FROM some_table WHERE d='x'

I would like to see however:然而,我想看到:

SELECT a,b,c,d,e,f FROM some_table WHERE d='x'

I know that I cannot do this for table names, as discussed here , but was wondering if there was some way to do it for column names.我知道我不能像这里讨论的那样对表名执行此操作,但想知道是否有某种方法可以对列名执行此操作。

If there is not, then I will just have to try and make sure that I sanitize the input so it doesn't lead to SQL injection vulnerabilities.如果没有,那么我只需要尝试确保对输入进行消毒,以免导致 SQL 注入漏洞。

This indicates a bad DB design.这表明数据库设计不佳。 The user shouldn't need to know about the column names.用户不需要知道列名。 Create a real DB column which holds those "column names" and store the data along it instead.创建一个真正的 DB 列来保存这些“列名”并沿它存储数据。

And any way, no, you cannot set column names as PreparedStatement values.无论如何,不​​,您不能将列名设置为PreparedStatement值。 You can only set column values as PreparedStatement values您只能将列设置为PreparedStatement

If you'd like to continue in this direction, you need to sanitize the column names (to avoid SQL Injection ) and concatenate/build the SQL string yourself.如果您想继续朝这个方向发展,您需要清理列名(以避免SQL 注入)并自己连接/构建 SQL 字符串。 Quote the separate column names and use String#replace() to escape the same quote inside the column name.引用单独的列名并使用String#replace()转义列名内的相同引号。

Prepare a whitelist of allowed column names.准备一个允许列名的白名单。 Use the 'query' to look up in the whitelist to see if the column name is there.使用“查询”在白名单中查找以查看列名是否存在。 If not, reject the query.如果不是,则拒绝查询。

The accepted answer is not actually correct.接受的答案实际上并不正确。 While the OP approach indicated a bad DB design, it might be required by the business logic (for instance a MySQL IDE)虽然 OP 方法表明数据库设计不好,但业务逻辑可能需要它(例如 MySQL IDE)

Anyway, for MySQL prepared statements, what you need to know is that ?无论如何,对于 MySQL 准备好的语句,您需要知道的是? is for values, but if you need to escape column names, table names etc, use ??用于值,但如果您需要转义列名、表名等,请使用?? instead.反而。

Something like this will work:像这样的事情会起作用:

SELECT ??, ??, ?? FROM ?? WHERE ?? < ? 

Set values to ['id', 'name', 'address', 'user', 'id', 100]将值设置为['id', 'name', 'address', 'user', 'id', 100]

I think this case can't work because the whole point of the prepared statement is to prevent the user from putting in unescaped query bits - so you're always going to have the text quoted or escaped.我认为这种情况是行不通的,因为准备好的语句的重点是防止用户输入未转义的查询位 - 所以你总是要引用或转义文本。

You'll need to sanitize this input in Java if you want to affect the query structure safely.如果您想安全地影响查询结构,您需要在 Java 中清理此输入。

Use sql injection disadvantage of Statement Interface as advantage.使用语句接口的 sql 注入劣势作为优势。 Ex:前任:

st=conn.createStatement();
String columnName="name";
rs=st.executeQuery("select "+ columnName+" from ad_org ");
public void MethodName(String strFieldName1, String strFieldName2, String strTableName)
{
//Code to connect with database
String strSQLQuery=String.format("select %s, %s from %s", strFieldName, strFieldName2, strTableName);
st=conn.createStatement();
rs=st.executeQuery(strSQLQuery);
//rest code
}

下面是java中的解决方案。

String strSelectString = String.format("select %s, %s from %s", strFieldName, strFieldName2, strTableName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM