已解决sqlalchemy.exc.OperationalError: (pymssql.OperationalError) (1946, b“Operation failed. The index
已解决(pandas+sqlalchemy向数据库插入数据失败)sqlalchemy.exc.OperationalError: (pymssql.OperationalError) (1946, b"Operation failed. The index entry of length 930 bytes for the index ‘index_words’ exceeds the maximum length of 900 bytes.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
报错代码
粉丝群一个小伙伴想用pandas+sqlalchemy向数据库插入数据,但是还是发生了报错(当时他心里瞬间凉了一大截,跑来找我求助,然后顺利帮助他解决了,顺便记录一下希望可以帮助到更多遇到这个bug不会解决的小伙伴),报错代码如下::
df = pd.DataFrame(data_list)
engine = sqlalchemy.create_engine('mssql+pymssql://账号:密码@服务器地址:端口号/数据表')
if len(df) - 1 < 2000:
df.to_sql("数据表", con=engine, if_exists="append", index=False, chunksize=10000)
报错信息如下:
sqlalchemy.exc.OperationalError: (pymssql.OperationalError) (1946, b"Operation failed. The index entry of length 930 bytes for the index ‘index_words’ exceeds the maximum length of 900 bytes.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
报错翻译
报错信息翻译:
sqlalchemy.exc.OperationalError:(pymssql.Operational error)(1946,b“操作失败。索引’index_words’的长度为930字节的索引项超过了900字节的最大长度。DB-Lib错误消息20018,严重性16:\n常规SQL Server错误:检查来自SQL Server的消息\n”)
报错原因
组合索引值允许的最大大小为 900 字节。也就是说,组成组合索引的固定大小列的总长度不得超过 900 字节。
索引键允许的最大大小为 900 字节,不过 SQL Server 2000 允许在可能包含大量可变类型列的列上创建索引,而这些列的最大大小超过 900 字节。
在创建索引时,SQL Server 检查下列条件:
所有参与索引定义的固定数据列的总长度必须小于或等于 900 字节。当所要创建的索引只由固定数据列构成时,固定数据列的总计大小必须小于或等于 900 字节。否则将不能创建索引,且 SQL Server 将返回错误。
如果索引定义由固定类型列和可变类型列组成,且固定数据列满足前面的条件(小于或等于 900 字节),则 SQL Server 仍要检查可变类型列的总大小。如果可变类型列的最大大小与固定数据列大小的和大于 900 字节,则 SQL Server 将创建索引,不过将给用户返回警告消息以提醒用户:如果随后在可变类型列上的插入或更新操作导致总大小超过 900 字节,则操作将失败且用户将收到运行时错误。
解决方法
解决方法1:删除较大字段
解决方法2:索引定义只由可变类型列组成,且这些列的最大总大小大于 900 字节,则 SQL Server 将创建索引,不过将返回警告消息
以上是此问题报错原因的解决方法,欢迎评论区留言讨论是否能解决,如果有用欢迎点赞收藏文章谢谢支持,博主才有动力持续记录遇到的问题!!!
千人全栈VIP答疑群联系博主帮忙解决报错
由于博主时间精力有限,每天私信人数太多,没办法每个粉丝都及时回复,所以优先回复VIP粉丝,可以通过订阅限时9.9付费专栏《100天精通Python从入门到就业》进入千人全栈VIP答疑群,获得优先解答机会(代码指导、远程服务),白嫖80G学习资料大礼包,专栏订阅地址:https://blog.csdn.net/yuan2019035055/category_11466020.html
-
优点:作者优先解答机会(代码指导、远程服务),群里大佬众多可以抱团取暖(大厂内推机会),此专栏文章是专门针对零基础和需要进阶提升的同学所准备的一套完整教学,从0到100的不断进阶深入,后续还有实战项目,轻松应对面试!
-
专栏福利:简历指导、招聘内推、每周送实体书、80G全栈学习视频、300本IT电子书:Python、Java、前端、大数据、数据库、算法、爬虫、数据分析、机器学习、面试题库等等
-
注意:如果希望得到及时回复,订阅专栏后私信博主进千人VIP答疑群
相关文章
- Windows下解决github push failed (remote: Permission to userA/XXXX.git denied to userB.) 上传gitHub失败报错
- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)
- ERROR [org.apache.hadoop.util.Shell] - Failed to locate the winutils binary in the hadoop binary path
- VS2010安装顽疾解决方法:error 25541 failed to open xml file
- 解决eclipse打开报错:failed to create the java virtual ma
- frida环境问题解决Failed to enumerate processes: this feature requires an iOS Developer Disk Image to be mounted;
- 解决Nginx的connect() to 127.0.0.1:8080 failed (13: Permission denied) while connect
- Mac 在启动eclipse时 Failed to load JavaHL Library解决方法
- error: %preun(keepalived-1.2.7-3.el6.x86_64) scriptlet failed, exit status 1 解决
- 解决argo workflow报错:MountVolume.SetUp failed for volume “docker-sock“ : hostPath type check failed
- jenkins pipeline发送邮件报错解决办法: failed to connect, no password specified?
- How to resolve ASSERTION_FAILED error when you register the odata service expose
- Commit request failed Commit failed. Ref must be HEAD and is HEAD
- 成功解决 raise RuntimeError(“The JPMML-SkLearn conversion application has failed. The Java executable
- 成功解决tensorflow.python.framework.errors_impl.NotFoundError: FindFirstFile failed for: ../checkpoints
- 成功解决 raise RuntimeError(“The JPMML-SkLearn conversion application has failed. The Java executable
- 成功解决OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cv::cvtColor
- 已解决 Failed to build opencv-python-headless
- Mac 下python3 [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed 解决方法
- 完美解决Failed to determine a suitable driver class的问题
- Ubuntu 挂载NFS文件系统报错:mount: 198.168.1.x:/mnt/nfs/ failed, reason given by server: Permission denied
- 计算机安装ipguard客户端visual studio 2019报错build stopped:subcommand failed
- 解决办法:git错误 error: failed to push some refs to 'https://github.com/...
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法!
- Android的报错提示:Failed to resolve: io.reactivex.rxjava2:rxandroid:2.1.0
- Win10解决Android Studio:Gradle sync failed: Failed to open zip file.