c++ dev , mysql Bind_placeholders.bind with single quotes

Viewed 25

I'm learning c++ web develoment .

I what save my data by MySql database .

my env

  1. Apple M1
  2. Docker Debian 11 Arm64
  3. "Mysql8" and "MySQL Connector/C++ 8"

My question

I don't know how to use parameterized query .

my error code ①

SqlStatement sqlStatement = session.sql("SELECT * FROM ?.?")
                                      .bind(nameless_carpool, user);
SqlResult sqlResult = sqlStatement.execute();

/*

I guses the api will got sql is : 
    SELECT * FROM ?.? -> SELECT * FROM 'nameless_carpool'.'user' 

In fact error message :

terminate called after throwing an instance of 'mysqlx::abi2::r0::Error'
  what():  CDK 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 ''nameless_carpool'.'user'' at line 1
*/

My right code ②

SqlStatement sqlStatement = session.sql("SELECT * FROM nameless_carpool.user");
SqlResult sqlResult = sqlStatement.execute();

/*
use the code , i can get my table info . 
but i want use parameterized query
*/

Will anyone help me ?

1 Answers

slack friend Nik told me :

I don't think that will work. Using ? will escape the values as values, not for table names. Typically, you would use parameterized queries for values only. For example, as you have seen, this doesn't work:

SELECT * FROM 'nameless_carpool'.'user'

It should be this:

SELECT * FROM `nameless_carpool`.`user`

But this is not what parameterized queries are typically for. It would be used for values, eg:

SELECT * FROM `nameless_carpool`.`user` WHERE `driver_name` = ?

which would turn into something like:

SELECT * FROM `nameless_carpool`.`user` WHERE `driver_name` = 'Wukong'

I know this from working with MySQL and other databases. It is not common to need a variable to reference a table. If you do, the application should handle this without directly taking user input

Related