Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 如何配置pyodbc以使用freeTDS和unixODBC从SQL Server正确接受字符串?_Sql Server 2008_Unicode_Pyodbc_Freetds_Unixodbc - Fatal编程技术网

Sql server 2008 如何配置pyodbc以使用freeTDS和unixODBC从SQL Server正确接受字符串?

Sql server 2008 如何配置pyodbc以使用freeTDS和unixODBC从SQL Server正确接受字符串?,sql-server-2008,unicode,pyodbc,freetds,unixodbc,Sql Server 2008,Unicode,Pyodbc,Freetds,Unixodbc,我无法将MSSQL服务器中的有效字符串转换为python。我相信某个地方存在编码不匹配。我相信它介于ODBC层和python之间,因为我能够在tsql和isql中获得可读的结果 pyodbc期望什么字符编码?我需要在链中更改什么才能使其正常工作 具体示例 以下是一个简化的python脚本示例: #!/usr/bin/env python import pyodbc dsn = 'yourdb' user = 'import' password = 'get0lddata' database =

我无法将MSSQL服务器中的有效字符串转换为python。我相信某个地方存在编码不匹配。我相信它介于ODBC层和python之间,因为我能够在tsql和isql中获得可读的结果

pyodbc期望什么字符编码?我需要在链中更改什么才能使其正常工作

具体示例

以下是一个简化的python脚本示例:

#!/usr/bin/env python
import pyodbc

dsn = 'yourdb'
user = 'import'
password = 'get0lddata'
database = 'YourDb'

def get_cursor():
    con_string = 'DSN=%s;UID=%s;PWD=%s;DATABASE=%s;' % (dsn, user, password, database)
    conn = pyodbc.connect(con_string)
    return conn.cursor()

if __name__ == '__main__':
    c = get_cursor()
    c.execute("select id, name from recipe where id = 4140567")

    row = c.fetchone()
    if row:
        print row
此脚本的输出为:

(Decimal('4140567'), u'\U0072006f\U006e0061\U00650067')
或者,如果脚本的最后一行更改为:

print "{0}, '{1}'".format(row.id, row.name)
结果是:

Traceback (most recent call last):
  File "/home/mdenson/projects/test.py", line 20, in <module>
    print "{0}, '{1}'".format(row.id, row.name)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
在isql中也可以看到:

root@luke:~# isql -v yourdb import get0lddata
SQL>  select id, name from recipe where id = 4140567
+----------------------+--------------------------+
| id                   | name                     |
+----------------------+--------------------------+
| 4140567              | orange2                  |
+----------------------+--------------------------+
SQLRowCount returns 1
1 rows fetched
所以我今天早上一直在做这件事,看起来有点高有点低,还没有弄清楚是什么地方出了问题

详细信息

以下是版本详情:

  • 客户端是Ubuntu 12.04
  • freetds v0.91
  • unixodbc 2.2.14
  • python 2.7.3
  • pyodbc 2.1.7-1(来自ubuntu包)和3.0.7-beta06(从源代码编译)

  • 服务器是XP和SQL Server Express 2008 R2

下面是客户端上一些配置文件的内容

/etc/freetds/freetds.conf

[global]
    tds version = 8.0
    text size = 64512
[cmw]
    host = 192.168.90.104
    port = 1433
    tds version = 8.0
    client charset = UTF-8
/etc/odbcinst.ini

[FreeTDS]
Description = TDS driver (Sybase/MS SQL)
Driver = /usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
CPTimeout =
CPReuse =
FileUsage = 1
/etc/odbc.ini

[yourdb]
Driver = FreeTDS
Description = ODBC connection via FreeTDS
Trace = No
Servername = cmw
Database = YourDB
Charset = UTF-8

有没有可能是BOM(字节顺序标记)有问题?如果是这样的话,这段代码可能会有所帮助:

import codecs
if s.beginswith( codecs.BOM_UTF8 ):
    # The byte string s begins with the BOM: Do something.
    # For example, decode the string as UTF-8

if u[0] == unicode( codecs.BOM_UTF8, "utf8" ):
    # The unicode string begins with the BOM: Do something.
    # For example, remove the character.

# Strip the BOM from the beginning of the Unicode string, if it exists
u.lstrip( unicode( codecs.BOM_UTF8, "utf8" ) )

我在上找到了这个片段。

所以在继续工作之后,我现在将unicode字符导入python。不幸的是,我偶然发现的解决办法就像亲吻你的表弟一样令人满意

我通过安装python3和python3开发包,然后用python3重建pyodbc,解决了这个问题

现在我已经完成了这项工作,我的脚本现在可以工作了,尽管我仍然在用Python2.7运行它们


因此,我不知道这样做解决了什么问题,但它现在起作用了,我可以继续我开始的项目。

如果您将pyodbc升级到版本3,问题将得到解决。

在我继续这方面的工作中,我现在编译了pyodbc的最新版本,3.0.7-beta06,但行为没有变化。Hmm。我已经阅读了你的答案和链接,我不确定这是问题所在,或者至少我不知道如何处理它。从字符串u'\U0072006f\U006e0061\U00650067'可以看出,每对字母都被交换了,但是缺少了第7个字符,并且我看不到任何BOM表的迹象。我只是想尝试一下这个问题,因为错误是无法“…对位置0-2的字符进行编码,因为它们不在范围内”。抱歉,Matthew,这不是python版本,是ubuntu的软件包。Debian也有同样的问题。我删除了debian的软件包,并通过
pip
安装了pyodbc,一切正常,不需要python 3。
import codecs
if s.beginswith( codecs.BOM_UTF8 ):
    # The byte string s begins with the BOM: Do something.
    # For example, decode the string as UTF-8

if u[0] == unicode( codecs.BOM_UTF8, "utf8" ):
    # The unicode string begins with the BOM: Do something.
    # For example, remove the character.

# Strip the BOM from the beginning of the Unicode string, if it exists
u.lstrip( unicode( codecs.BOM_UTF8, "utf8" ) )