-1

My Question is how can i create a table with a table name what the user typed in? So i have already that: string createtable = "CREATE TABLE '" + email.Text + "' (benutzung VARCHAR (20), passwort VARCHAR (30), id INTEGER);"; but everytime i start the program and try it again this error pops up:

MySql.Data.MySqlClient.MySqlException: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test' (benutzung VARCHAR (20), passwort VARCHAR (30), id INTEGER)' at line 1"

  • table name can not be in single quotes. – Amit Verma Sep 01 '21 at 12:08
  • 2
    `create a table with a table name what the user typed in?` - are you _sure_ this is not a terrible idea, especially in terms of security? _why_ do you want this, what are you trying to achieve? also: you are using [the wrong quotes](https://stackoverflow.com/a/11004848/5309228) – Franz Gleichmann Sep 01 '21 at 12:08
  • @AmitVerma can you possibly write how i can do it? – Sarper Eroglu Sep 01 '21 at 14:06
  • Creating tables dynamically, and worse from user input is a **terrible** design choice. Regardless on how you do it both security and scalability concerns come to mind. Why do you need to do this way specifically? Normally I would expect the schema to be fixed and user actions only modify data on the DB. – Alejandro Sep 01 '21 at 14:20

1 Answers1

0

I found the Solution from myself, what you must do is easily make it like that:

string createtable = "CREATE TABLE " + email.Text + " (benutzung VARCHAR (20), 
passwort VARCHAR (30), id INTEGER);";

Now all works :D

  • Try using string interpolation like `string createTable = $"CREATE TABLE {email.Text} (benutzung VARCHAR (20), passwort VARCHAR(30), id INTEGER);"`. It will make string construction easy. – Navjot Singh Sep 01 '21 at 15:32