Search code examples
mysqlnode-mysql2

mysql does not accept "?" as bind operator


Trying to run a query to update a database, and for some reason, MySQL returns:

Error Code: 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

Tried the line just in SQL to see what happened

INSERT INTO eu_profile (id, profileName) VALUES ? 

It also fails with the same message in the code:

import * as mysql2  from 'mysql2';
import * from 'mysql2/promise'

/** Lets open the database for mysql2 */
const mysql2connection = mysql2.createConnection({
    "host": "localhost",
    "port": 3306,
    "user": "node",
    "password": "NotTellingYou",
    "database": "stats"
});
mysql2connection.connect(function(error){
    if(!!error){
        console.log(error);
    }else{
        console.log('Connected!:)');
    }
});


    let dbTables: string="";
    let sqlInsert: string="";
    dbTables = "id, profileName";
    sqlInsert = "INSERT INTO eu_profile ("+dbTables+") VALUES ?"

    const profileLoad = await mysql2connection.query(sqlInsert, [profileData], function(err) {
        if (err) throw err{
        mysql2connection.end();
        }else
    }).then(console.log("We are done here"));

Version: enter image description here


Solution

  • If you have two columns you need tiw values

    INSERT INTO eu_profile (id, profileName) VALUES (?,?) 
    

    Also [profileData] needs 2 values like [req.body.topic, req.body.note]