已解决The conversion of the nvarchar value ‘10033121171441‘ overflowed an int column
已解决pandas向SqlServer数据库插入数据,抛出异常sqlalchemy.exc.OperationalError: (pymssql._pymssql.OperationalError) (248, b"The conversion of the nvarchar value ‘10033121171441’ overflowed an int column.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")的正确解决方法,亲测有效,并附上常用number类型的存储大小表
报错问题
一个小伙伴遇到问题跑来私信我,想用pandas向SqlServer数据库插入数据,但是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下所示:
def insert_sql(data_list):
df = pd.DataFrame(data_list)
# df.to_excel('郎酒_京东检索呈现页.xlsx')
engine = sqlalchemy.create_engine('')
if len(df) - 1 < 2000:
df.to_sql("GoodsSearch", con=engine, if_exists="append", index=False, chunksize=10000)
else:
start = 0
for i in range(0, len(df), 2000):
end = i
dfn = df.loc[start:end]
dfn.to_sql("GoodsSearch", con=engine, if_exists="append", index=False, chunksize=10000)
start = end + 1
dfn = df.loc[start:len(df) - 1]
dfn.to_sql("GoodsSearch", con=engine, if_exists="append", index=False, chunksize=10000)
报错信息截图如下所示:
报错翻译
报错信息翻译如下所示:
sqlalchemy.exc.操作错误:(pymssql_pymssql.操作错误)(248,b“nvarchar值10033121171441的转换溢出了一个int列。DB-Lib错误消息20018,严重性16:\n常规SQL Server错误:检查来自SQL Server的消息\n”)
报错原因
这个异常是由于数据库中的一个nvarchar类型的值,超过了int列的最大值范围,导致无法进行转换,下面附上常用number类型的存储大小表:
小伙伴们按下面的方法即可解决!!!
解决方法
解决这个问题的方法有:
-
将int列的数据类型改为bigint,以支持更大的值范围。
-
将nvarchar类型的值进行截断,以使其适合int列的大小范围。
-
如果将值截断不可行,可以考虑使用其他数据类型,比如float或decimal,以支持更大的值范围。
-
如果无法更改数据库结构或数据类型,可以考虑在查询时使用CAST或CONVERT函数,将nvarchar类型的值转换为适当的数据类型。
例如,如果要将nvarchar类型的值转换为bigint类型,可以使用以下查询:
SELECT CAST('10033121171441' AS BIGINT)
这将返回一个bigint类型的值,而不是抛出异常。
以上是此问题报错原因的解决方法,欢迎评论区留言讨论是否能解决,如果有用欢迎点赞收藏文章谢谢支持,博主才有动力持续记录遇到的问题!!!
千人全栈VIP答疑群联系博主帮忙解决报错
由于博主时间精力有限,每天私信人数太多,没办法每个粉丝都及时回复,所以优先回复VIP粉丝,可以通过订阅限时9.9付费专栏《100天精通Python从入门到就业》进入千人全栈VIP答疑群,获得优先解答机会(代码指导、远程服务),白嫖80G学习资料大礼包,专栏订阅地址:https://blog.csdn.net/yuan2019035055/category_11466020.html
-
优点:作者优先解答机会(代码指导、远程服务),群里大佬众多可以抱团取暖(大厂内推机会),此专栏文章是专门针对零基础和需要进阶提升的同学所准备的一套完整教学,从0到100的不断进阶深入,后续还有实战项目,轻松应对面试!
-
专栏福利:简历指导、招聘内推、每周送实体书、80G全栈学习视频、300本IT电子书:Python、Java、前端、大数据、数据库、算法、爬虫、数据分析、机器学习、面试题库等等
-
注意:如果希望得到及时回复,和大佬们交流学习,订阅专栏后私信博主进千人VIP答疑群
相关文章
- mycat 链接mysql提示ERROR 3009 (HY000): java.lang.IllegalArgumentExceptio解决方式
- 服务器端解决跨域问题的三种方法
- error the @annotation pointcut expression is only supported at Java 5 compliance level or above 异常解决
- python paramiko ssh.exec_command()启动tomcat服务器应用进程失败问题解决方法- Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this progr
- 解决java compiler level does not match the version of the installed java project facet
- python paramiko ssh.exec_command()启动tomcat服务器应用进程失败问题解决方法- Neither the JAVA_HOME nor the JRE_HOME environment variable is defined At least one of these environment variable is needed to run this progr
- 成功解决.sh: line 6: syntax error near unexpected token `do
- 问题解决了,可是为什么呢?could not find the main class.program will exitmain
- 成功解决Not possible to connect to the Web3 provider. Make sure the provider is running and a connection
- 成功解决gensimmatutils.py:737: FutureWarning: Conversion of the second argument of issubdtype from `int
- 成功解决sklearnpreprocessinglabel.py:151: DeprecationWarning: The truth value of an empty array is amb
- 已解决This behaviour is the source of the following dependency conflicts.
- 已解决pip升级失败报错WARNING: There was an error checking the latest version of pip.
- 已解决 DEPRECATION: configobj is being installed using the legacy ‘setup.py install‘ method, because i
- 已解决pymssql._pymssql.OperationalError: (156, b“Incorrect syntax near the keyword ‘distinct‘.DB-Lib er
- 已解决ERROR: Could not find a version that satisfies the requirement re ( from versi ons: none) ERROR:
- 关于appium踩坑 :Encountered internal error running command: Error: Cannot verify the signature of (已解决)
- 解决 Virtualbox 6.1.34 出现 End kernel panic - not syncing: attempted to kill the idle task
- Cleanup failed to process the following paths错误的解决
- Ubuntu下is not in the sudoers file 问题解决
- Another app is currently holding the yum lock解决方法
- 解决 fatal: Not a git repository (or any of the parent directories): .git 问题
- 完美解决 fatal: Not a git repository (or any of the parent directories): .git