在 SQL Python 中使用 INSERT INTO 时整数值发生变化
Integer value changes when using INSERT INTO in SQL Python
我正在使用 Python 中的 mysql.connector
创建一个 table。当我将整数值插入 table 时,SQL 会将其转换为其他整数值。这是我的代码:
cursor.execute("CREATE TABLE cleaned_data (myid VARCHAR(255), mynum INTEGER(255))")
cursor.execute("INSERT INTO cleaned_data(myid, mynum) VALUES(%s,%s)", ('mytext',25683838382092010098988))
rows = cursor.execute('select * from cleaned_data;')
for row in cursor:
print(row)
这是打印行的输出:
('mytext', 2147483647)
知道发生了什么事吗?我希望 table 存储我的整数,即使它很大。
INTEGER(255)
并没有按照你的想法去做。
整数的最大值为 2147483647(有符号)或 4294967295(无符号)。
有关详细信息,请参阅此答案What is the size of column of int(11) in mysql in bytes?
要存储最多 65 位数字,您可以使用小数类型。例如,对于 25 位的整数,使用 decimal(25,0)
我正在使用 Python 中的 mysql.connector
创建一个 table。当我将整数值插入 table 时,SQL 会将其转换为其他整数值。这是我的代码:
cursor.execute("CREATE TABLE cleaned_data (myid VARCHAR(255), mynum INTEGER(255))")
cursor.execute("INSERT INTO cleaned_data(myid, mynum) VALUES(%s,%s)", ('mytext',25683838382092010098988))
rows = cursor.execute('select * from cleaned_data;')
for row in cursor:
print(row)
这是打印行的输出:
('mytext', 2147483647)
知道发生了什么事吗?我希望 table 存储我的整数,即使它很大。
INTEGER(255)
并没有按照你的想法去做。
整数的最大值为 2147483647(有符号)或 4294967295(无符号)。
有关详细信息,请参阅此答案What is the size of column of int(11) in mysql in bytes?
要存储最多 65 位数字,您可以使用小数类型。例如,对于 25 位的整数,使用 decimal(25,0)