下面是調(diào)用方式:
Example script - pymssql module (DB API 2.0)
Example script - _mssql module (lower level DB access)
不過(guò),在我使用過(guò)程中,發(fā)現(xiàn),如果表中包含了ntext字段,就會(huì)出錯(cuò),提示
不能用 DB-Library(如 ISQL)或 ODBC 3.7 或更早版本將 ntext 數(shù)據(jù)或僅使用
Unicode排序規(guī)則的 Unicode 數(shù)據(jù)發(fā)送到客戶端。
查了一下,發(fā)現(xiàn)官方網(wǎng)站有解釋:
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.
大概意思是,這是因?yàn)槲覀兊膒ymssql使用早期的ODBC函數(shù)集來(lái)獲取數(shù)據(jù)。后來(lái)微軟才引入了ntext和nvarchar類型,但Microsoft并沒(méi)有更新他們的 C-library,所以就沒(méi)辦法支持了。建議:將ntext修改為nvarchar或text.
顯然,這不是個(gè)好的解決方法,那么是否就沒(méi)有其他辦法了呢?
還好,不用絕望,既然不支持ntext但支持text,那么我們只需要在輸出時(shí)將ntext轉(zhuǎn)換為text就好了,方法很簡(jiǎn)單:
SELECT cast ( field_name AS TEXT ) AS field_name
唯一的問(wèn)題,可能是ntext和text字段所支持的長(zhǎng)度不一樣,所以也許你還需要設(shè)置一下TEXTSIZE
SET TEXTSIZE 65536
當(dāng)然,你還可以將字段設(shè)置的大一點(diǎn),這個(gè)就看你的需要了。
新聞熱點(diǎn)
疑難解答
圖片精選