Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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
Python 读取sqlalchemy错误返回无效名称,因为它包含空字符或无效unicode字符_Python_Sql_Sql Server_Sqlalchemy - Fatal编程技术网

Python 读取sqlalchemy错误返回无效名称,因为它包含空字符或无效unicode字符

Python 读取sqlalchemy错误返回无效名称,因为它包含空字符或无效unicode字符,python,sql,sql-server,sqlalchemy,Python,Sql,Sql Server,Sqlalchemy,我正在使用sqlalchemy读取一个查询。我的代码是下一个: def conection(file_name, server, database, uid, pwd): cnxn = create_engine("mssql+pyodbc:///?odbc_connect={}".format(urllib.parse.quote_plus("DRIVER=SQL SERVER;\ SERVER={0};PORT=1433;DAT

我正在使用sqlalchemy读取一个查询。我的代码是下一个:

def conection(file_name, server, database, uid, pwd):
    cnxn = create_engine("mssql+pyodbc:///?odbc_connect={}".format(urllib.parse.quote_plus("DRIVER=SQL SERVER;\
                            SERVER={0};PORT=1433;DATABASE={1};UID={2};PWD={3};TDS_Version=8.0;".format(server, database, uid, pwd))))
    pd.read_sql(query, cnxn, chunksize=100)

parser = argparse.ArgumentParser(description='Connect to the ODBC and extract the tables')
parser.add_argument('-table', type=str)
args = parser.parse_args()

sql_path = os.path.join('..', 'SQL')
filename_base = args.table
sql_filename = os.path.join(sql_path, filename_base)

with open(os.path.join('..', '..', '..', 'configuration', 'connnection.json')) as f:
    data = json.load(f)

query = obtain_query(sql_filename)
cnxn = conection(filename_base,
                        data[filename_base]['server'],
                        data[filename_base]['database'],
                        data[filename_base]['uid'],
                        data[filename_base]['pwd'])
但是,我收到以下错误:

sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'L'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]'.C.u.s.t.o.m.e.r.C.o.d.e.' is an invalid name because it contains a NULL character or an invalid unicode character. (1055)")
以下是我的表格示例:

正如您所看到的,我没有空值或无效字符,因为它只是作为文本的数字。我已经检查过了,没有空值或unicode字符

我使用的查询只是一个基本的选择:

SELECT
     [Customer]
    ,[Account]
    ,[Country]
    ,[Metric1]
    ,[Metric2]
    ,[Metric3]
    ,[Metric4]
FROM 
    [Table1]

在查询中,对于出现错误的列,请尝试使用

REPLACE(name_col,char(13)+char(10), ' ') as name_col
像这样的

SELECT
      REPLACE(Customer,char(13)+char(10), ' ') as Customer
     ,REPLACE(Account,char(13)+char(10), ' ') as Account
      .....
FROM 
    [Table1]

你能给我们看一下你使用的查询吗?@Owen我刚把它添加到描述中。好的,你能给我们看一下你用来执行查询的代码吗?我这样问是因为错误在查询本身。看起来列名(或者可能是表名)格式不正确。@Owen确定!我刚刚用它更新了描述错误显示了一个名为CustomerCode的东西(可能是一列)正在作为C.u.s.t.o.m.e.r.C.o.d.e发送打印您的查询并查看它。