1

I created a sequelize raw query helper function that will insert data from my mysql database.

const addBudget = function(name, amount) {
  sequelize.query(
    'INSERT INTO budget (name, amount) VALUES(' + name + ',' + amount + ')', {
      type: Sequelize.QueryTypes.INSERT
    }
  ).then(function (data) {
    console.log('inserted STEAMER data---> ', data);     });
};

And then when I call the function to try to insert some data on mysql database to see if it's inserting the my data to the record:

addBudget('Book', 100);

I got this error:

Executing (default): SELECT 1+1 AS result
Executing (default): INSERT INTO budget (name, amount) VALUES(Book,100)
Unhandled rejection SequelizeDatabaseError: Unknown column 'Book' in 'field list'
    at Query.formatError (/Users/mac/Documents/dudes/moira/node_modules/sequelize/lib/dialects/mysql/query.js:247:16)
    at Query.handler [as onResult] (/Users/mac/Documents/dudes/moira/node_modules/sequelize/lib/dialects/mysql/query.js:68:23)
    at Query.Command.execute (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/commands/command.js:30:12)
    at Connection.handlePacket (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/connection.js:502:28)
    at PacketParser.onPacket (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/connection.js:81:16)
    at PacketParser.executeStart (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (/Users/mac/Documents/dudes/moira/node_modules/mysql2/lib/connection.js:89:29)
    at Socket.emit (events.js:180:13)
    at addChunk (_stream_readable.js:274:12)
    at readableAddChunk (_stream_readable.js:261:11)
    at Socket.Readable.push (_stream_readable.js:218:10)
    at TCP.onread (net.js:581:20)

Any idea what am I doing wrong here when it comes to inserting data to the database?

1
  • I think you are quoting the string 'Book' again inside your query, try removing it. It should work Commented May 3, 2018 at 7:37

3 Answers 3

1

SQL only allows single-quote strings for your values. So, this will not work:

'INSERT INTO budget (name, amount) VALUES("Book", 100)'

Nor will this:

'INSERT INTO budget (name, amount) VALUES(Book, 100)'

Assuming you instructed SQL to read the amount as an integer, this is the proper way to insert your values:

'INSERT INTO budget (name, amount) VALUES('Book', 100)'

If Book is just a variable that is already a string, you can also do this to insure the single-quote rule is kept:

`INSERT INTO budget (name, amount) VALUES('${Book}', 100)`
Sign up to request clarification or add additional context in comments.

Comments

0

Issue here is your values should be covered b/w single quote or double quote , but with your code it generates VALUES(Book,100); , that should be VALUES('Book','100'); , For that

Just change below line :

'INSERT INTO budget (name, amount) VALUES(' + name + ',' + amount + ')'

With this :

`INSERT INTO budget (name, amount) VALUES('${name}','${amount}')`

Comments

0

Have you tried this:

var sql = "insert into budget (name) values ()";
sequelize.execute(sql, function(error, result){
  console.log(result);
});

execute insert and update sql and get more informations

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.