pymssql ntext字段调用问题解决方法

yipeiwu_com5年前Python基础

下面是调用方式:

Example script - pymssql module (DB API 2.0)

Example script - _mssql module (lower level DB access)

不过,在我使用过程中,发现,如果表中包含了ntext字段,就会出错,提示

不能用 DB-Library(如 ISQL)或 ODBC 3.7 或更早版本将 ntext 数据或仅使用

Unicode排序规则的 Unicode 数据发送到客户端。

查了一下,发现官方网站有解释:

Q: What means "Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library"?

A: If you connect to a SQL Server 2000 SP4 or SQL Server 2005, and if you make a SELECT query on a table that contains a column of type NTEXT, you may encounter the following error:
_mssql.error: SQL Server message 4004, severity 16, state 1, line 1:
Unicode data in a Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier.

It's the SQL Server complaining that it doesn't support pure Unicode via TDS or older versions of ODBC. There's no fix for this error. Microsoft has deprecated DB-Library a long ago, in favor of ODBC, OLE DB, or SQL Native Client. Many new features of SQL 2005 aren't accessible via DB-Library so if you need them, you have to switch away from pymssql or other tools based on TDS and DB-Library.

A workaround is to change the column type to NVARCHAR (it doesn't exhibit this behaviour), or TEXT.

大概意思是,这是因为我们的pymssql使用早期的ODBC函数集来获取数据。后来微软才引入了ntext和nvarchar类型,但Microsoft并没有更新他们的 C-library,所以就没办法支持了。建议:将ntext修改为nvarchar或text.

显然,这不是个好的解决方法,那么是否就没有其他办法了呢?

还好,不用绝望,既然不支持ntext但支持text,那么我们只需要在输出时将ntext转换为text就好了,方法很简单:

SELECT cast ( field_name AS TEXT ) AS field_name

唯一的问题,可能是ntext和text字段所支持的长度不一样,所以也许你还需要设置一下TEXTSIZE

SET TEXTSIZE 65536

当然,你还可以将字段设置的大一点,这个就看你的需要了。

相关文章

Python Grid使用和布局详解

Python Grid使用和布局详解

本文实例为大家分享了Python Grid使用和布局的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python import vtk # 这个示例主要用...

Tensorflow之构建自己的图片数据集TFrecords的方法

学习谷歌的深度学习终于有点眉目了,给大家分享我的Tensorflow学习历程。 tensorflow的官方中文文档比较生涩,数据集一直采用的MNIST二进制数据集。并没有过多讲述怎么构建...

安装python3的时候就是输入python3死活没有反应的解决方法

我用brew安装python3 装完了发现 输入python3毫无反应,检查了 $PATH 也没有任何问题 这个时候回去看安装过程,发现安装时有一个错误: ERROR:The `b...

django中使用Celery 布式任务队列过程详解

django中使用Celery 布式任务队列过程详解

本文记录django中如何使用celery完成异步任务。 Celery 是一个简单、灵活且可靠的,处理大量消息的分布式系统,并且提供维护这样一个系统的必需工具。 它是一个专注于实时处理的...

python cv2读取rtsp实时码流按时生成连续视频文件方式

python cv2读取rtsp实时码流按时生成连续视频文件方式

我就废话不多说了,直接上代码吧! # coding: utf-8 import datetime import cv2 import os ip = '192.168.3.160...