2
votes

I'm using the mymysql package and I'm trying to create a function which gets an SQL query and some parameters (as variadic empty interface):

func FindByQuery(statement string, params ...interface{}) (diver *DiverT, err error) {
    values := make([]interface{}, len(params))
    for i := range params {
            values[i] = params[i]
    }

    // Both statements result in the same error...
    row, _, execError := Db.QueryFirst(statement,values...)

    row, _, execError := Db.QueryFirst(statement,params...)

    // Additional code...
}

When I call this method using some kind of SQL, I always get an SQL error. I do something like:

FindByQuery("SELECT * FROM Diver WHERE Name=?", "Markus")

Which results in the following error:

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 '?%!(EXTRA string=Markus)' at line 1"

What should I do that the parameter is converted correctly to a string (or whatever it is, if I have different parameter(s))?

1
Looking at github.com/ziutek/mymysql, it seems it uses printf style format characters for interpolating values into queries rather than question marks. Might that be the cause of your problem? - James Henstridge
Perfect, thanks. Quite interesting the the mymysql documentation uses question marks (and the question mark worked with integer parameters). - ComSubVie
The question marks are for prepared statements. See: github.com/ziutek/mymysql/blob/master/examples/prepared_stmt.go for an example. - Rich Churcher
Thanks for the claraification. - ComSubVie
@comsubvie, suggest putting an answer on your question as I am sure someone else will need it - miltonb

1 Answers

0
votes

Try using printf format syntax:

FindByQuery("SELECT * FROM Diver WHERE Name=%s", "Markus")