I am currently working on a discord bot, using discord.js. I recently started teaching myself SQLite a couple of days ago (so I may just be an idiot... probably). Anyways, in code below, I am attempting to select values from a SQlite DB and push them into an array to send as a message. The issue is that, the IDs seem to be rounding themselves, I've tried to convert into strings (this was after they were already integer values, I've tried reentering the values but that did not work), however, it did not help. Within the database the values are correct, but when returning the values (in the form of a discord message) the values are wrong. The issue seems to be solely residing within the SQLite query as the value returned is not modified through the script and console logging the rows, returning values that are already rounded. Also: The Code's very messy, please show me where I went wrong. The Values are Rounded to: From -> To
- 231324359870906368 -> 231324359870906370
- 342592681790144513 -> 342592681790144500
- 310682989380108290 -> 310682989380108300
- 345801479329677313 -> 345801479329677300
- 549366490919469096 -> 549366490919469100
- 199416992615235600 -> 199416992615235586
- 158100000000000123 -> 158100000000000130
My test value (158100000000000123) showed there must be a rounding error, as the value was correct until it was the same length as the other values. I also attempted to create another column, which was a string, yet the rounding still occurred when the value was the same length. Any help is greatly appreciated!
var partners = [];
var price = [];
var partnerembed;
var i;
function getPartners(id){
var query = "SELECT * FROM partners WHERE userid = " + id;
db.all(query, function (err, rows) {
if(err){
console.log(err);
}
else
{
console.log(rows[0].partner)
for (i = 0; i <= rows.length-1; i++)
{
partners.push(rows[i].partner)
price.push(rows[i].price)
}
}
});
}
getPartners(message.author.id);
function partnerMessage()
{
var partnerembed = new RichEmbed()
.setTitle('You are married to:')
.setAuthor(message.author.username, id.displayAvatarURL)
.setColor(0x8AC784);
for (i=0; i<= partners.length-1; i++)
{
partnerembed.addField(partners[i], price[i])
}
message.channel.send(partnerembed)
}
setTimeout(partnerMessage, 1000)
DB Values:
userid partnerid price
- 332734758515769354 231324359870906368 420000
- 332734758515769354 342592681790144513 1
- 332734758515769354 310682989380108290 4
- 332734758515769354 345801479329677313 1000
- 332734758515769354 276968137293824021 2000
- 332734758515769354 549366490919469096 5000
- 332734758515769354 199416992615235586 5
- 332734758515769354 158100000000000123 5
Also, don't question why my bot believes marriage is essentially just slavery - it's just a little bit slow...
sqlite is returning the correct value, unfortunately it's more integer than js can handle.
js supports 53 bit integers. A simple js in the console
produces the same result.
Try selecting the column(s) from the database as a string using CAST (doc found on this page) and then treat all as strings in the js.