【问题标题】:How can I stop rounding int values in MySQL如何停止在 MySQL 中舍入 int 值
【发布时间】:2020-11-24 23:34:34
【问题描述】:

我是 Discord 机器人开发人员。我正在尝试将 MySQL 用于我的不和谐机器人。我正在尝试将公会 ID 保存在我的表中。当我发布到表格时,公会 ID 会自动四舍五入。 例如,我的公会 ID 是 837465019283745861。它的发布类似于 837465019283745900。 我正在使用 mysql 模块。

我的桌子:

CREATE TABLE guilds (
id INT PRIMARY KEY AUTO_INCREMENT,
guild_id BIGINT(18) NOT NULL);

我的邮政编码:

const guild = 837465019283745861;
connection.query('INSERT INTO guilds (guild_id) VALUES (?)', guild, function(error, results, fields) {
    if (error) throw error;
    console.log('Data sent!');
});

【问题讨论】:

    标签: javascript mysql node.js discord.js


    【解决方案1】:

    这些值不会在 MySQL 中四舍五入。那是 JavaScript 搞砸了你,因为你没有使用 BigInt:

    const guild = 837465019283745861n;
    

    除非另有说明,否则 JavaScript 中的所有数字实际上都是浮点数。这会导致对较大数字进行四舍五入。

    【讨论】:

      【解决方案2】:

      要解决这个问题,请将“supportBigNumbers”添加到您的 MySQL 配置中

      例子:

      const database = {
        host: env.db_host,
        port: env.db_port,
        user: env.db_user,
        password: env.db_password,
        database: env.db_name,
        supportBigNumbers: true // BigInt not rounded now
      };
      

      查看更多信息:https://www.npmjs.com/package/mysql#getting-the-id-of-an-inserted-row

      【讨论】:

        猜你喜欢
        • 2017-05-20
        • 1970-01-01
        • 1970-01-01
        • 2021-12-29
        • 1970-01-01
        • 2019-07-22
        • 1970-01-01
        • 1970-01-01
        • 2010-12-27
        相关资源
        最近更新 更多