0

I Try to insert to MySQL database in C#, but what i got is this error

"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 "insert (NoNota, Nama, Tanggal, Tipe, Keterangan) VALUES ('1111', 'Kickass', '201" at line 1

i think the problem is the DateTime, in my database datatype i set it to DATETIME, Here is my code

        string sqlQuery;

        sqlQuery = "INSERT INTO insert (id, Name, Date, Type, Notes) VALUES ('1111', 'Kickass', '2013-09-09', 'Cash', 'Nothing')";


        if (this.OpenConnection() == true)
        {
            cmd = new MySqlCommand(sqlQuery, connect);
            cmd.ExecuteNonQuery();
            CloseConnection();
            MessageBox.Show("Operation INSERT is SUCCESS!!");
        }

What's wrong with it? i try excute my SQL Queries it work very FINE in MySQL Workbench, it automatically convert the DateTime and insert it into the table. Any clue?

2 Answers 2

3

insert is a reserved word (INSERT INTO insert). Rename the table or escape with backticks. I'd highly recommend renaming.

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

1 Comment

Ooh! god! ty! Where i can find restricted word for mysql Syntax?
0

You need to escape reserved words in MySQL like INSERT with backticks

INSERT INTO `insert` (id, Name, ...
            ^------^---------------------here

But it would be way better to rename your table. insert does not say anything about your data. Try to think of its content. When you have hundreds of tables in a database you need to name every one very carefully to keep track what it contains.

When you name your column, name it after the single word that finishes this sentance:

The data of my table holds ...

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.