如何在python中用单引号格式化sqlwhere子句参数?

2024-03-29 10:07:39 发布

您现在位置:Python中文网/ 问答频道 /正文

然后,我会在py-user-to-connect子句中为My-user-to-input语句中的My-user-to-type使用引号。你知道吗

每次我试图将输入格式化为包含单引号时,它似乎都不起作用,并且不返回任何行。你知道吗

这就是我想要达到的目标

"select FirstName, LastName from person.person where FirstName = 'Kim'" 

其中“Kim”应该是用户输入。你知道吗

在我的代码区。你知道吗

user_first_name = print(input("Enter the first name you are looking 
for: "))

cursor.execute("select FirstName, LastName from person.person where 
FirstName = ?", (user_first_name))

如何格式化输入(user\u first\u name),使参数在查询字段中有单引号。你知道吗

import pyodbc

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=DESKTOP-3NLISNE\SQLNEW;DATABASE=TestDB;UID=python;PWD=password')

cursor = cnxn.cursor()

user_first_name = print(input("Enter the first name you are looking for:"))

query = "select FirstName, LastName from person.person where FirstName = ?"
result = cursor.execute(query, (user_first_name,))

rows = result.fetchall()

print (rows)

Tags: tonamefrominputmyconnectfirstnamewhere