Python connection SQL Server-- error InterfaceError: Connection to the database failed for an unknown reason Solution

Module Description:

    pymssql: used to connect SQL Server database, the default installation Anaconda module is not installed, pip install pymssql mounting may be used;

  pyodbc: can also be used to connect to SQL Server database, but other than that, but also for Oracle, Excel, MySql, etc., is installed by default when you install Anaconda.

Pymssql connection using the local database:

    Database using the connection connect, which can be found to give the required parameters in the database connection properties, wherein the password is a computer power-on password.

    Using acquired data, the subsequent data mining can work it!

import pymssql

# 数据库服务器信息
server = 'DESKTOP-L*'  # 从数据库连接属性中查看
user = 'DESKTOP-L*\DELL'  # 从数据库连接属性中查看
password = '*'  # 电脑开机密码
database = 'testdb'  # 需要连接的表名

# 使用 connect 创建连接对象
conn = pymssql.connect(server, user, password, database)
# 使用 connect.cursor创建游标对象
cur = conn.cursor()
sql = 'select * from checks'  # 查询语句
# cursor.execute方法执行SQL语句
cur.execute(sql)
# cursor.fetchall 获取查询结果
rows = cur.fetchall()  # list
# 使用close方法关闭cursor 和 数据库连接
conn.close()
for row in rows:
    print(row)  # tuple

# 调用结果如下
(1, 'MaBell', 150.0, 'Havesonsnexttime')
(2, 'ReadingR.R.', 245.34, 'TraintoChicago')
(3, 'MaBell', 200.0, 'CellularPhone')
(4, 'LocalUtilities', 98.0, 'Gas')
(5, 'JoesStale$Dent', 150.0, 'Groceries')
(6, 'Cash', 25.0, 'WildNightOut')
(7, 'JoansGas', 25.1, 'Gas')

Pymssql connect to the server using the database:

    In connection to the remote server, the program error: pymssql.InterfaceError: Connection to the database failed for an unknown reason.

Traceback (most recent call last):
  File "src\pymssql.pyx", line 636, in pymssql.connect
  File "src\_mssql.pyx", line 1957, in _mssql.connect
  File "src\_mssql.pyx", line 677, in _mssql.MSSQLConnection.__init__
_mssql.MSSQLDriverException: Connection to the database failed for an unknown reason.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/PycharmProjects/Python_SQL_Server/1_pymssql.py", line 21, in <module>
    conn = pymssql.connect(server, user, password, database)
  File "src\pymssql.pyx", line 645, in pymssql.connect
pymssql.InterfaceError: Connection to the database failed for an unknown reason.

    Several online search solutions, as follows, but to no avail, still error.

Pyodbc connect to the server using the database:

    After trying various methods valid, to switch to connect to a remote database pyodbc, as follows:

    The last successful operation to get to the server data.

import pyodbc

# 数据库服务器信息
driver = 'SQL Server Native Client 11.0'  # 因版本不同而异
server = '180.106.30.04,1003'  
user = 'sa'
password = '*****'
database = '***'

conn = pyodbc.connect(driver=driver, server=server, user=user, password=password, database=database)

cur = conn.cursor()
sql = 'select * from [checks]'  # 查询语句
cur.execute(sql)
rows = cur.fetchall()  # list
conn.close()

for row in rows:
    print(row)  # tuple

At last:

    Connecting the local database: using pymssql module;

    Connect to a remote database server: use pyodbc module.

    As to why the error, so far unknown.

 

Guess you like

Origin blog.csdn.net/weixin_41713230/article/details/82698287