将 Ubuntu 上的 python3 连接到 SQL Server 2014
Connecting python3 on Ubuntu to SQL Server 2014
我正在尝试在 Python 3.5 脚本中使用 Linux 服务器 (Ubuntu 16.04.2) 检索 SQL 数据 [= =34=]服务器。 运行 SQL 服务器上的脚本和 windows 中的 Python 运行正常。 运行 Linux Python 中的脚本抛出 SQL 服务器语法错误:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][SQL 服务器的 ODBC 驱动程序 13][SQL 服务器] '0x107c' 附近的语法不正确。 (102) (SQLExecDirectW)")
当我添加或删除列时,它会将“0x107c”更改为不同的字符,表明它不是单个非法字符,而是多个。对于数量有限的列,脚本甚至可以运行(不包括 [Order Type text] 和 [Order Nr])。这让我怀疑字符集转换出现问题。我做错了什么,我该如何解决?
Python3.5:
import pandas as pd
import pyodbc
#Set parameters
sql_file = 'file.sql'
#Define methods
def SQLDataToDataframe(filename):
fd = open('file.sql','r')
content = fd.read()
fd.close()
df = pd.read_sql(content, connection)
return df
#Import Data from SQL
connection = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};'
'Server=Server;'
'Database=DB;'
'uid=User;pwd=Password')
dataframe = SQLDataToDataframe(sql_file)
file.sql:
SELECT [ID]
,[Company Code]
,[Description]
,[Order Category]
,[Order Category Text]
,[Order Type]
,[Order Type text]
,[Order Nr]
FROM [TABLE]
解决方案供以后参考。不要在 Linux 上使用 pyodbc 连接到 MS SQL 服务器。请改用 pymssql。此处说明:https://docs.microsoft.com/en-us/sql/connect/python/pymssql/step-1-configure-development-environment-for-pymssql-python-development。
我正在尝试在 Python 3.5 脚本中使用 Linux 服务器 (Ubuntu 16.04.2) 检索 SQL 数据 [= =34=]服务器。 运行 SQL 服务器上的脚本和 windows 中的 Python 运行正常。 运行 Linux Python 中的脚本抛出 SQL 服务器语法错误:
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][SQL 服务器的 ODBC 驱动程序 13][SQL 服务器] '0x107c' 附近的语法不正确。 (102) (SQLExecDirectW)")
当我添加或删除列时,它会将“0x107c”更改为不同的字符,表明它不是单个非法字符,而是多个。对于数量有限的列,脚本甚至可以运行(不包括 [Order Type text] 和 [Order Nr])。这让我怀疑字符集转换出现问题。我做错了什么,我该如何解决?
Python3.5:
import pandas as pd
import pyodbc
#Set parameters
sql_file = 'file.sql'
#Define methods
def SQLDataToDataframe(filename):
fd = open('file.sql','r')
content = fd.read()
fd.close()
df = pd.read_sql(content, connection)
return df
#Import Data from SQL
connection = pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};'
'Server=Server;'
'Database=DB;'
'uid=User;pwd=Password')
dataframe = SQLDataToDataframe(sql_file)
file.sql:
SELECT [ID]
,[Company Code]
,[Description]
,[Order Category]
,[Order Category Text]
,[Order Type]
,[Order Type text]
,[Order Nr]
FROM [TABLE]
解决方案供以后参考。不要在 Linux 上使用 pyodbc 连接到 MS SQL 服务器。请改用 pymssql。此处说明:https://docs.microsoft.com/en-us/sql/connect/python/pymssql/step-1-configure-development-environment-for-pymssql-python-development。