用于创建表的 SQL 准备语句
我想知道一些根据用户输入动态创建表的方法(SQL 准备语句)
CREATE TABLE ? (
First_Name char(50),
Last_Name char(50)
)
我应该用什么来代替问号
I wanted to know of some way to create table on the fly based on user input(SQL Prepared Statement)
CREATE TABLE ? (
First_Name char(50),
Last_Name char(50)
)
What should i put in place of question mark
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PreparedStatement 占位符不适用于表名或列名,它们仅适用于实际的列值。
因此,您必须动态创建(准备好的)语句字符串,这意味着您的应用程序将容易受到 SQL 注入的攻击。根据应用程序的使用方式以及由谁使用,这可能是一个大问题。
相关问题
PreparedStatement placeholders are not intended for table names nor column names, they are only intended for actual column values.
So you would have to create the (prepared) statement string dynamically, which means your application will be vulnerable to SQL injection. Depending on how the application is supposed to be used - and by who - this could be a BIG problem.
Related question
正如其他答案所指出的,在这种情况下不能使用准备好的语句,因为大多数 DBMS 不支持替换表名。
但是,我想指出,根据用户输入选择表名似乎是一个坏主意。如何防止重复或无效名称?
为什么表名很重要?
As the other answers point out, you cannot use prepared statement in this case, as replacement of table names is not supported in most DBMS.
However, I'd like to point out that choosing a table name based on user input seems like a bad idea. How do you prevent duplicates, or invalid names?
Why does the table name matter?