如何将变量放入 `pyodbc` 查询中?
How to put variable in `pyodbc` query?
我正在使用 pyodbc
在 Microsoft SQL 服务器中保存数据。
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})
''')
我收到以下错误
Invalid column name \'Shams\'.
Invalid column name \'Nahid\'.
The multi-part identifier "directory.PNG" could not be bound.
但是如果我放置硬编码字符串而不是变量
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'>
如何将变量放入查询中?
通过在变量两边加上引号解决。
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}')
''')
或者你可以试试这个
cursor.execute('''
INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
VALUES
("Shams", "Nahid", "my_profile_photo_path")
''')
我再试一次,成功了,例如
import pymysql
def test():
db = pymysql.connect('localhost', 'root', '****', 'python')
cur = db.cursor()
id_ = "123456"
query = ''.join(["select *", " from customer where id = ", id_])
cur.execute(query)
result = cur.fetchone()
print("result: ", result)
if __name__ == '__main__':
test()
使用字符串格式将列值插入 SQL 语句是一种危险的做法,因为它会使您的代码暴露于 SQL 注入 漏洞。例如, 中的代码将适用于
user_last_name = "Nahid"
但是
会失败
user_last_name = "O'Connor"
SQL 注入也可能具有严重的安全隐患。在网络上搜索 "Little Bobby Tables" 以查看相关示例。
相反,您应该使用 参数化查询,如下所示:
user_first_name = 'Shams'
user_last_name = 'Nahid'
file_name = 'my_profile_photo_path'
sql = '''\
INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
VALUES (?, ?, ?)
'''
params = (user_first_name, user_last_name, file_name, )
cursor.execute(sql, params)
我正在使用 pyodbc
在 Microsoft SQL 服务器中保存数据。
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})
''')
我收到以下错误
Invalid column name \'Shams\'.
Invalid column name \'Nahid\'.
The multi-part identifier "directory.PNG" could not be bound.
但是如果我放置硬编码字符串而不是变量
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'>
如何将变量放入查询中?
通过在变量两边加上引号解决。
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}')
''')
或者你可以试试这个
cursor.execute('''
INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
VALUES
("Shams", "Nahid", "my_profile_photo_path")
''')
我再试一次,成功了,例如
import pymysql
def test():
db = pymysql.connect('localhost', 'root', '****', 'python')
cur = db.cursor()
id_ = "123456"
query = ''.join(["select *", " from customer where id = ", id_])
cur.execute(query)
result = cur.fetchone()
print("result: ", result)
if __name__ == '__main__':
test()
使用字符串格式将列值插入 SQL 语句是一种危险的做法,因为它会使您的代码暴露于 SQL 注入 漏洞。例如,
user_last_name = "Nahid"
但是
会失败user_last_name = "O'Connor"
SQL 注入也可能具有严重的安全隐患。在网络上搜索 "Little Bobby Tables" 以查看相关示例。
相反,您应该使用 参数化查询,如下所示:
user_first_name = 'Shams'
user_last_name = 'Nahid'
file_name = 'my_profile_photo_path'
sql = '''\
INSERT INTO pyUser.dbo.user_information (first_name, last_name, profile_photo_path)
VALUES (?, ?, ?)
'''
params = (user_first_name, user_last_name, file_name, )
cursor.execute(sql, params)