简体   繁体   English

PYmssql python连接字符串

[英]PYmssql python connection string

I have downloaded Pymssql to connect to the sqlserver db but the connection string is throwing error-pymssql.connect(pymssql.c.:7990) 我已经下载了Pymssql以连接到sqlserver db但是连接字符串正在抛出error-pymssql.connect(pymssql.c.:7990)

import pymssql
pymssql.connect(host='username\SQLEXPRESS',user='username',password='pwd',database='master')

Anyone had luck connecting to the sqlserver? 谁有幸连接到sqlserver?

Error:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pymssql.pyx", line 556, in pymssql.connect (pymssql.c:7990)
    raise OperationalError(e[0])
pymssql.OperationalError: (20009, 'Net-Lib error during Unknown error')

I ran into the same Net-Lib error during Unknown error error message. Net-Lib error during Unknown error错误消息Net-Lib error during Unknown error遇到了相同的Net-Lib error during Unknown error For me, the issue was that I needed to enable TCP/IP and Named Pipes in my instance of SQL Server Express. 对我来说,问题是我需要在我的SQL Server Express实例中启用TCP / IP和命名管道

Try going into SQL Server Configuration Manager and turning on both TCP/IP and Named Pipes. 尝试进入SQL Server配置管理器并打开TCP / IP和命名管道。

My setup: 我的设置:

  • Microsoft Windows XP SP3. Microsoft Windows XP SP3。
  • Microsoft SQL Server 2008 (not R2). Microsoft SQL Server 2008(不是R2)。
  • CPython 2.7.4. CPython 2.7.4。
  • pymssql 2.0.0 (installed with easy_install , IIRC). pymssql 2.0.0(与easy_install安装,IIRC)。

I had forgot to create a sa login account, so I remember scavenging a few weeks ago for a way to create it; 我忘了创建一个sa登录帐户,所以我记得在几个星期前为了创建它的方式进行清理; previously, I was logging in (eg with SQL Server Management Studio) with Windows authentication. 以前,我使用Windows身份验证登录(例如使用SQL Server Management Studio)。 I followed https://stackoverflow.com/a/3781737 to create the sa user account. 我按照https://stackoverflow.com/a/3781737创建了sa用户帐户。

After that intermediary step, I started Python. 在那个中间步骤之后,我开始使用Python。

import pymssql
conn = pymssql.connect(host=r'MACHINE\SQLEXPRESS', user=r'sa', password=r'password', database=r'MYDB')
cur = conn.cursor()
cur.execute(r'SELECT COUNT(*) FROM mytable')
row = cur.fetchone()
print row[0]
cur.close()
conn.close()

My guess with your problem is that you should have used raw strings in the connection parameters -- particularly the host parameter, which takes in a backslash. 我对你的问题的猜测是你应该在连接参数中使用原始字符串 - 特别是host参数,它接收反斜杠。

I also tried it with a CentOS 5.8 64-bit machine (pymssql 1.0.2, freetds 0.91). 我也尝试使用CentOS 5.8 64位机器(pymssql 1.0.2,freetds 0.91)。 To that effect, I had also created a $HOME/.freetds.conf file with contents as 为此,我还创建了一个$HOME/.freetds.conf文件,内容为

[global]
tds version = 10.0

[MACHINE]
host = 192.168.1.2
port =1433
tds version = 10.0
encryption = request

I forgot where I picked that file's configuration example from. 我忘记了从中选择该文件的配置示例的位置。

Hope that helps. 希望有所帮助。

你必须声明charset样本:

pymssql.connect(host='username\SQLEXPRESS',user='username',password='pwd',database='master',chartset="your_charset")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM