【发布时间】:2019-01-13 05:18:54
【问题描述】:
我正在使用pyodbc 在 Microsoft SQL Server 中保存数据。
I 3 个字符串变量,user_first_name、user_last_name 和 profile_photo_path
user_first_name = 'Shams'
user_last_name = 'Nahid'
file_name = 'my_profile_photo_path'
现在当我尝试使用保存数据时
cursor.execute(f'''
INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
VALUES
({user_first_name}, {user_last_name}, {file_name})
''')
我收到以下错误
无效的列名\'Shams\'。
无效的列名\'Nahid\'。
无法绑定多部分标识符“directory.PNG”。
但不是变量,如果我把硬编码的字符串放进去
cursor.execute('''
INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
VALUES
('my_user_first_name', 'my_user_last_name', 'my_file_name')
''')
然后查询显示没有错误。
我检查了变量类型,user_first_name、user_last_name和file_name,都是<class 'str'>
如何将变量放入查询中?
【问题讨论】:
标签: python python-3.x pyodbc