ziutek / mymysql

MySQL Client API written entirely in Go
Other
735 stars 161 forks source link

dbConn.Prepare(sql) error on valid sql statement #62

Open emaxerrno opened 11 years ago

emaxerrno commented 11 years ago

Working sql. Tested pasting this text on the shell.

INSERT INTO users (email, password, status, signup_date, zipcode, fname, lname) VALUES ( 'a@a.com', 'asdf', 'unverified', now(), '111', 'asdf', 'asdf' ); SELECT LAST_INSERT_ID();

Error by using ? where the quotes are like this

`INSERT
      INTO users (email,
    password,
    status,
    signup_date,
    zipcode,
    fname,
    lname)
      VALUES (
      ?,
      ?,
      'unverified',
      now(),
      ?,
      ?,
      ?
      );
      SELECT LAST_INSERT_ID();`

That will cause a:

Cannot create new user sql prepared statement:Received #1064 error from MySQL server: "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 'select last_insert_id()' at line 1"

ziutek commented 11 years ago
  1. Did you used last version of mymysql (1.4.2)?
  2. Did you used db.Exec or db.Query?
ziutek commented 11 years ago

First. database/sql doesn't handle properly multiple results. Your query contains INSERT and SELECT. Each one returns some result and you can't obtains them using database/sql, because it assumes only one result per query. If you really need such functionality use raw mymysql (not database/sql).

Second. You probably use db.Query or db.Prepare. Prepared statements can't contain multi-statements, see: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html