pymssql ntext字段調用問題解決方法
下面是調用方式:
Example script - pymssql module (DB API 2.0)
Example script - _mssql module (lower level DB access)
不過,在我使用過程中,發(fā)現(xiàn),如果表中包含了ntext字段,就會出錯,提示
不能用 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.
大概意思是,這是因為我們的pymssql使用早期的ODBC函數(shù)集來獲取數(shù)據(jù)。后來微軟才引入了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和Tkinter實現(xiàn)高考倒計時功能
隨著高考的臨近,每個考生都在緊鑼密鼓地復習,這時候,一款實用的倒計時軟件能有效幫助你規(guī)劃剩余時間,提醒你不要浪費每一分每一秒,今天,我們來聊聊一款基于Python和Tkinter開發(fā)的高考倒計時軟件,功能簡單卻極具實用性,讓你在緊張的備考過程中不再迷失2025-03-03
Python獲取Redis所有Key以及內(nèi)容的方法
今天小編就為大家分享一篇Python獲取Redis所有Key以及內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
Python的Pandas庫中使用DataFrame篩選和刪除含特定值的行與列
Pandas是一個強大的數(shù)據(jù)處理庫,提供了各種功能來操作和處理數(shù)據(jù),這篇文章主要給大家介紹了關于Python的Pandas庫中使用DataFrame篩選和刪除含特定值的行與列的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-05-05

