mysqljs / mysql

A pure node.js JavaScript Client implementing the MySQL protocol.
MIT License
18.3k stars 2.53k forks source link

parser error #1983

Closed cristina21r closed 6 years ago

cristina21r commented 6 years ago

I have a table: valori, with id autoincrement, componenta text, valoare float; i try to insert from nodejs:

var b=5.6;
var sql='insert into valori values(id,pot,'+b+')';

and i got this..but this is the value!! not a field:|:|:| i tried every possible option i know..

Error: ER_BAD_FIELD_ERROR: Unknown column 'pot' in 'field list'
    at Query.Sequence._packetToError (/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
    at Query.ErrorPacket (/node_modules/mysql/lib/protocol/sequences/Query.js:77:18)
    at Protocol._parsePacket (/node_modules/mysql/lib/protocol/Protocol.js:279:23)
    at Parser.write (/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/node_modules/mysql/lib/protocol/Protocol.js:39:16)
    at Socket.<anonymous> (/node_modules/mysql/lib/Connection.js:103:28)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:153:18)
    at Socket.Readable.push (_stream_readable.js:111:10)
    --------------------
    at Protocol._enqueue (/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Connection.query (/node_modules/mysql/lib/Connection.js:208:25)
    at null._repeat (/lm35-iot/main2.js:40:14)
    at wrapper [as _onTimeout] (timers.js:275:11)
    at Timer.listOnTimeout (timers.js:92:15)
kai-koch commented 6 years ago

This is bad SQL-Syntax, not a mysqljs error.

You have no field named "pot". A correct (long) INSERT in SQL would be:

INSERT INTO valori SET id = NULL, componenta  = 'My Homework', valora = 5.6;

Check https://github.com/mysqljs/mysql#escaping-query-values on how to escape query values in mysqljs safely.

var post  = {id: null, componenta: 'My Homework', valora: 5.6};
var query = connection.query(
    'INSERT INTO valori SET ?',
    post,
    function callback (error, results, fields) {
        if (error) throw error;
        // Neat!
    }
);

Check https://dev.mysql.com/doc/refman/5.7/en/insert.html to learn how to write INSERT-queries in (My)SQL.

Closed: no mysqljs-error