使用Python执行SQL查询期间获取类型错误
当我执行下面的python代码时,我得到类型错误:使用Python执行SQL查询期间获取类型错误,python,sql,sql-server,Python,Sql,Sql Server,当我执行下面的python代码时,我得到类型错误: connsql.cursor().execute(query, name, path, path, name) TypeError: The first argument to execute must be a string or unicode query. Python代码: connsql,_DATABASE = self.sql_connection() query = ''' IF NOT EXISTS
connsql.cursor().execute(query, name, path, path, name)
TypeError: The first argument to execute must be a string or unicode query.
Python代码:
connsql,_DATABASE = self.sql_connection()
query = '''
IF NOT EXISTS (select 1 from [%s].[dbo].[config] where name = ? and val = ?)
BEGIN
insert into [%s].[dbo].[config] values (?,?)
END
''',(_DATABASE,_DATABASE)
connsql.cursor().execute(query, name, path, path, name)
connsql.cursor().commit()
connsql.close()
请任何人帮我解决这个错误。试试
connsql.cursor()。执行(查询,(名称,路径,路径,名称))
。顺便说一句,链接游标的创建和执行是非常难看的,我认为connsql.cursor().commit()
甚至不起作用,因为commit
通常是在连接上执行的,而不是在游标上,所以字符串格式设置是关闭的<代码>查询实际上只是一个元组,当我切换到Try connsql.cursor().execute(查询,(名称、路径、路径、名称))
是的,请参阅我的第二条评论。尝试打印(查询)
和打印(键入(查询))