Reputation: 17
I have a mysql unknown column error 1054. i can't insert in my database table my insert function
public void Insert(string tablename ,string[] values, string[] columns)
{
string col = "(";
for (int x = 0; x < columns.Length; x++)
{
if (columns[x] == columns.Last())
col += columns[x];
else
if (columns.Length > 1)
col += columns[x] + ",";
}
col += ")";
string val = "VALUES"+"(";
for (int x = 0; x < values.Length; x++)
{
if (values[x] == values.Last())
val += values[x];
else
if (values.Length > 1)
val += values[x] + ",";
}
val += ")";
string query = "INSERT INTO "+ tablename + col + val ;
my query: return from the function.
"INSERT INTO rezervationinformations(Fullname,Phone,Description)VALUES(dsa,cq,q)"
called function:
db.Insert("rezervationinformations", new string[] { textBox1.Text, textBox2.Text,
textBox3.Text }, new string[] { "Fullname", "Phone", "Description" });
Upvotes: 1
Views: 3992
Reputation: 45500
What you are trying to achieve is really bad, and not only it will cause errors, it is also exposing your database to SQL injection.
Just forget this useless function and use vanilla ADO.NET
with parametrized query.
Or you can always use an ORM such as Entity Framework
Upvotes: 2
Reputation: 28629
You should properly quote all your query elements.
INSERT INTO `databasename.rezervationinformations` ( `Fullname`, `Phone`, `Description` ) VALUES ( "dsa" ,"cq", "q" )
Upvotes: 0
Reputation:
You need to have quotations around your values, like this:
INSERT INTO rezervationinformations (Fullname,Phone,Description) VALUES ('dsa','cq','q')
Also, depending on your setup, you may need to specify the db:
INSERT INTO mydatabasename.rezervationinformations (Fullname,Phone,Description) VALUES ('dsa','cq','q')
Spacing between keywords is important too, make sure you are implementing whitespace where it is expected or the interpreter won't be able to understand what you're telling it to do. For the same reason when we, as humans, speak and write, we put a pause in between each word we say.
Upvotes: 1