1

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" });
3
  • 2
    does it look to me like you are writing a wrapper for MysqlClient? Don't you are just breaking perfectly good code to replace it with bad code Commented Dec 19, 2016 at 23:05
  • Also leave a space before values here string val = "VALUES"+"("; Commented Dec 19, 2016 at 23:05
  • Making SQL and db Ops in general just isnt that tedious. What happens when you need to insert/update a Date, Int or Decimal? Commented Dec 19, 2016 at 23:20

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

You should properly quote all your query elements.

INSERT INTO `databasename.rezervationinformations` ( `Fullname`, `Phone`, `Description` ) VALUES ( "dsa" ,"cq", "q" )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.