0

I am trying to insert into a mysql table. Here is my code...

        Dim cmdText = "INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp') values (@cid,@bus,@notey,@datey)"
    Dim conn As New MySqlConnection("server=theipofserver;user id=id;password=mypass;database=thedatabase")
    Using cmd = New MySqlCommand(cmdText, conn)
        conn.Open()
        Dim testy As String
        testy = TextBox1.Text
        Dim dater = Date.Today + TimeOfDay
        cmd.Parameters.AddWithValue("@cid", c_id)
        cmd.Parameters.AddWithValue("@bus", c_business)
        cmd.Parameters.AddWithValue("@notey", testy)
        cmd.Parameters.AddWithValue("@datey", dater)
        cmd.ExecuteNonQuery()
    End Using

But, I get the following error:

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 ''cust_id', 'business', 'note', 'date_stamp') values ('ID','null','first one'' at line 1

the table I am trying to insert into has 4 columns that are all varchars with plenty of space (varchar(100))

Here are more details on the error... error occurs on line 31...

Line 29:             cmd.Parameters.AddWithValue("@notey", testy)
Line 30:             cmd.Parameters.AddWithValue("@datey", dater)
Line 31:             cmd.ExecuteNonQuery()
Line 32:         End Using
Line 33:         TextBox1.Font.Bold = True

So, what am I doing wrong here? Any tips on better ways to insert into mysql tables would be greatly appreciated!

1
  • 1
    Do not forget to close the connection (better enclose in using statement) Commented Jul 22, 2013 at 15:19

3 Answers 3

3

Don't use quotes around table or column names.

INSERT INTO complete_log (`cust_id`, `business`, `note`, `date_stamp`)
values (@cid,@bus,@notey,@datey)

If you want to escape table and column names then use backticks. But you only need to escape reserved words in MySQL.

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

1 Comment

Well, I feel silly now... Thanks a lot juergen! I'll mark this as the answer in a few minutes.
1

Do not specify column names in apostrophies

Instead of

Dim cmdText = "INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp') values (@cid,@bus,@notey,@datey)"

Do

Dim cmdText = "INSERT INTO complete_log (cust_id, business, note, date_stamp) values (@cid,@bus,@notey,@datey)"

Comments

1

Change

INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp')

to

INSERT INTO complete_log (cust_id, business, note, date_stamp)

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.