Giter Site home page Giter Site logo

Comments (7)

xaprb avatar xaprb commented on May 9, 2024

MySQL itself isn't going to allow you to prepare the SHOW TABLES query. It
only permits parameters to be used for values, not identifiers. So I'm not
sure if this is helpful or just noise in this conversation, but I just
wanted to point out that your last sentence assumes something MySQL is not
going to allow.

On the other hand, the following is legal to prepare:

SHOW TABLES LIKE ?

from mysql.

arnehormann avatar arnehormann commented on May 9, 2024

My last paragraph was misleading, by legitimate I only meant the query itself, not using it in a prepared statement (though I tried it this way at first). But that still leaves me wondering... If it is disallowed for identifiers, why is SHOW CREATE {PROCEDURE | FUNCTION | EVENT | TABLE | VIEW} ok for prepared statements? What's used as a parameter if not an identifier? Something along the lines of WHERE name LIKE ...?

Concerning this issue, I only think the error message could and should be clearer, as it doesn't cause an error on prepare, only on execution. It's not even that bad when it is called as

stmt, err := db.Prepare("SHOW TABLES FROM ?")
rows, err := stmt.Query("mysql")

but it is very strange getting sql: statement expects 0 inputs; got 1 after

rows, err := db.Query("SHOW TABLES FROM ?", "mysql")

SHOW TABLES LIKE ? didn't work for me, but SHOW TABLES did. Still calls for USE ..., though. It's not a big issue, but I hoped I could get along without tracking the current schema.

Still, thanks!

from mysql.

julienschmidt avatar julienschmidt commented on May 9, 2024

I get the following error in both cases, which seems completely fine to me:

Error 1064: 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 '?' at line 1

Did you forget to check err somewhere?

from mysql.

julienschmidt avatar julienschmidt commented on May 9, 2024

The error sql: statement expects 0 inputs; got 1 comes from database/sql/sql.go#1203. There is nothing we could do on driver side to change the error text.
I'm still wondering how you even got there without ignoring an err, which should already be returned on Prepare(..)

from mysql.

arnehormann avatar arnehormann commented on May 9, 2024

I was able to demonstrate to Julien I could get the error without black magic or ignoring errors, but we can't prevent it.

from mysql.

alidawud avatar alidawud commented on May 9, 2024

Same issue here, solved by doing this:

stmt, err := mysql.Prepare("SELECT * FROM tags WHERE tag_name like ?")
rows, err := stmt.Query("%" + tagName + "%")

from mysql.

itsjavi avatar itsjavi commented on May 9, 2024

I got the same problem and it was because I had the ? between quotes

stmt, err := db.Prepare("SELECT id, name FROM foobar WHERE id='?' OR name='?'")
checkErrFail(err)
err = q.QueryRow(slug, slug).Scan(&m.id, &m.name)

error: sql: statement expects 0 inputs; got 2.

this was fine:

SELECT id, identifier FROM foobar WHERE id=? OR identifier=?

from mysql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.