Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
so im making a dscord bot and i used sqlite, but becous emy host (heroku) i could no use that. thay said i needed to use postgres so i did im updating my code to use but i cant get it to work for creating my table i use this code
client.query('CREATE TABLE users(name text, money bigint);', (err, res) => {
  if (err) throw err;
  for (let row of res.rows) {
    console.log(JSON.stringify(row));
  }
  client.end();
});


that works fine the table is created.
then for insertinf data i use this:
client.query(`INSERT INTO users(name,money) SELECT ${message.author.id},0 WHERE NOT EXISTS(SELECT 1 FROM users WHERE name = '${message.author.id}'); `, (err, res) => {
        if (err) throw err;
        message.reply(`inserted data`);

    });

that also works fin i think.
but then when i wanted to get data with:
client.query(`SELECT * FROM users WHERE name = ?`, id,(err, row) => {
			if(err) throw err;
			console.log(id);
  			if (!row) return message.reply(`${name} has no money`);
  			
 	 		message.reply(`${name} has ${row.money} money`);
  		
		});


i get this error:
G:\dolphin casino\cmds\money.js:17
                        if(err) throw err;
                                ^

Error: Query values must be an array
    at Query.submit (G:\dolphin casino\node_modules\pg\lib\query.js:156:17)
    at Client._pulseQueryQueue (G:\dolphin casino\node_modules\pg\lib\client.js:356:24)
    at Client.query (G:\dolphin casino\node_modules\pg\lib\client.js:393:8)
    at Object.module.exports.run (G:\dolphin casino\cmds\money.js:16:10)
    at Client.bot.on.message (G:\dolphin casino\bot.js:80:14)
    at emitOne (events.js:96:13)
    at Client.emit (events.js:188:7)
    at MessageCreateHandler.handle (G:\dolphin casino\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (G:\dolphin casino\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:103:65)
    at WebSocketConnection.onPacket (G:\dolphin casino\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)


wich is weird becouse the name column is text not an array.

does someone know whats going on ?

What I have tried:

searching the internet. but i did not found an solution.
Posted
Updated 5-Apr-18 2:17am

1 solution

When using placeholders as in your SELECT command you have to pass the values as array (enclosed by square brackets):
javascrip
client.query('SELECT * FROM users WHERE name = ?', [id], (err, row) => {

[EDIT]
Quote:
now i get an syntax error
The result object returned in row is an array of row objects. Assuming you do not have multiple recordsets with the same name, you can access the first result:
JavaScript
if (row.rowCount == 1) {
    message.reply(`${id} has ${row.rows[0]['money']} money`);
}
else if (row.rowCount == 0) {
    message.reply(`${id} not found`);
}
[/EDIT]
 
Share this answer
 
v2
Comments
dolfijn3000 5-Apr-18 8:19am    
now i get an syntax error

G:\dolphin casino\cmds\money.js:17
if(err) throw err;
^

error: syntax error at end of input
at Connection.parseE (G:\dolphin casino\node_modules\pg\lib\connection.js:545:11)
at Connection.parseMessage (G:\dolphin casino\node_modules\pg\lib\connection.js:370:19)
at TLSSocket.<anonymous> (G:\dolphin casino\node_modules\pg\lib\connection.js:113:22)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:551:20)
Jochen Arndt 5-Apr-18 8:41am    
I can only guess that ${name} should be id or ${row.name}.

If that fails too check the documentation of the used MySQL client on how to access query result data.

dolfijn3000 5-Apr-18 8:45am    
the weird thing is that it did work with sqlite
Jochen Arndt 5-Apr-18 9:19am    
See my updated answer
dolfijn3000 5-Apr-18 10:12am    
but i dont need the first row i need the row where name is equal to id

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900