SQL Prepared Statement to Create Table

心不动则不痛 提交于 2019-12-23 08:01:43

问题


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


回答1:


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

  • How do I sanitize SQL without using prepared statements



回答2:


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?



来源:https://stackoverflow.com/questions/3655615/sql-prepared-statement-to-create-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!