windows 7
python 2.7
Django 1.11
我已经使用Django开发了一个网站。在后端,我有带2个表的sqlite数据库。一个表接受用户提交的表单,另一个用于比较。 用户提交表单A后,它将保存在表catalog_fw下,并将catalog_fw.ODM和catalog_fw.project_name与表catalog_fw_instance中的表进行比较。如果一行的catalog_fw.ODM和catalog_fw.project的内容完全相同,则catalog_fw_instance.level将与A组合以传递到.exe以生成txtx文件。
但是,此行发生错误:c.execute("catalog_fw_instance.level,......
`
当我运行此python文件时:
sqlite3.OperationalError: near "catalog_fw_instance": syntax error
获取sqlite数据,进行比较并传递给.exe的代码在这里:
def when_call_exe():
with sqlite3.connect('db.sqlite3') as con:
c = con.cursor()
#c.execute("catalog_fw_instance.level, SELECT catalog_fw.ODM_name, catalog_fw.project_name, catalog_fw.UAP, catalog_fw.NAP, catalog_fw.LAP, catalog_fw.num_address FROM catalog_fw INNER JOIN catalog_fw_instance ON catalog_fw.ODM_name=catalog_fw_instance.ODM_name AND catalog_fw.project_name=catalog_fw_instance.project_name")
sql = ("SELECT catalog_fw.ODM_name, catalog_fw.project_name, catalog_fw.UAP, catalog_fw.NAP, catalog_fw.LAP, " +
"catalog_fw.num_address, catalog_fw_instance.level " +
"FROM catalog_fw catalog_fw" +
"INNER JOIN catalog_fw_instance catalog_fw_instanc" +
" ON catalog_fw.ODM_name = catalog_fwi.ODM_name AND catalog_fw.project_name = catalog_fw_instance.project_name")
c.execute(sql)
print '1:', c.fetchone()
parameter = c.fetchone()
print '2', parameter
#pass to exe
args = ['.//exe//Test.exe', parameter[0], parameter[1]+parameter[2], parameter[3], parameter[4], parameter[5], parameter[6]]
output = my_check_output(args)
if 'SUCCESS' in output:
filename = output[28:-1]
else:
filename = output[8:-1]
downloadlink = os.path.join('/exe', '%s' % filename)
#save link to sqlite db
c.execute('''UPDATE catalog_fw SET download = %s WHERE
ODM_Name=parameter[1] AND project_Name=parameter[2] ''' % downloadlink)
此处显示sqlite数据库中的2个表
答案 0 :(得分:0)
据我所知,在Python中调用cursor#execute()
时,我们应该传递包含要运行查询的单个字符串。看起来您正在传递选择参数之一,然后是查询,所有这些都作为单个字符串一起传递。考虑以下版本:
c = con.cursor()
sql = ("SELECT cf.ODM_name, cf.project_name, cf.UAP, cf.NAP, cf.LAP, " +
"cf.num_address, cfi.level " +
"FROM catalog_fw cf " +
"INNER JOIN catalog_fw_instance cfi " +
" ON cf.ODM_name = cfi.ODM_name AND cf.project_name = cfi.project_name")
c.execute(sql)
print(c.fetchone())
parameter = c.fetchone()