简体   繁体   English

使用PYODBC在Vertica中创建表时出现语法错误

[英]Syntax error when creating table in Vertica with PYODBC

I am trying to load a big list of sql queries into a table in Vertica using PYODBC. 我正在尝试使用PYODBC将大量SQL查询列表加载到Vertica中的表中。 Here's my code: 这是我的代码:

tablename = DVXTEMP.my_table_name
sql = my_sql_query.strip().strip(';')
samplesize = 1000

createstring =   'CREATE TABLE %s AS %s \n limit %s;' %(tablename, sql, samplesize)

cursor.execute(createstring)

when I print createstring and run it in Toad, it works fine. 当我打印createstring并在Toad中运行它时,它工作正常。 when I try to execute it in pyodbc, it gives me the following error: 当我尝试在pyodbc中执行它时,出现以下错误:

'Syntax error at or near "DVXTEMP" at character 1\n (4856) (SQLExecDirectW)'

We are using Vertica Analytic Database v7.1.2-6 我们正在使用Vertica Analytic Database v7.1.2-6

Any ideas what might be causing this? 任何想法可能是什么原因造成的?

Thanks 谢谢

1) did you import pyodbc? 1)您是否导入了pyodbc?

2) did you define "cursor" from "pyodbc.connect"? 2)您是否从“ pyodbc.connect”定义了“光标”?

import pyodbc

DB = '[string for dbfile]'
DRV = '[string of which driver you are going to use]'
con = pyodbc.connect('DRIVER={};DBQ={}'.format(DRV,DB))
cursor = con.cursor()
##build SQL code and execute as you have done

Try SQL commands after you can connect without an error. 可以正确连接后尝试SQL命令。

3) I use pyodbc for mdb files (MS Access) and some of my queries will not run unless I put single quotes outside double quotes on table/field names. 3)我将pyodbc用于mdb文件(MS Access),除非将单引号放在表/字段名称的双引号之外,否则我的某些查询将无法运行。

mytbl_1 = "mytbl"
SQL = 'SELECT * FROM ' + mytbl_1
print SQL

print result -> SELECT * FROM mytbl 打印结果-> SELECT * FROM mytbl

(this fails) (失败)

mytbl_2 = '"mytbl"' #single quotes outside of double quote
SQL = 'SELECT * FROM ' + mytbl_2
print SQL

print result -> SELECT * FROM "mytbl" 打印结果-> SELECT * FROM“ mytbl”

(this string gets passed w/o error works for me with MDB files) (此字符串不带错误而传递给我,适用于MDB文件)

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

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