1. 使用占位符 `%`:
sql = "SELECT FLAG from TTRD_AUTH_USER where account = %s and account1 = %s" % (data, data1)
2. 使用 `.format()` 方法:
sql = "SELECT FLAG from TTRD_AUTH_USER where account = {} and account1 = {}".format(data, data1)
3. 使用 f-string(Python 3.6+):
sql = f"SELECT FLAG from TTRD_AUTH_USER where account = '{data}' and account1 = '{data1}'"
4. 对于非常长的SQL语句,可以使用三引号 `'''` 或 `"""` 进行多行书写:
sql = """
UPDATE student t
SET t.name = '',
t.sex = '',
t.age = '',
t.height = '',
t.weight = ''
"""
5. 连接数据库并执行SQL语句(以MySQL为例):
import pymysql
创建与数据库连接对象
conn = pymysql.connect(host='localhost', user='root', password='', database='db4', charset='utf8')
建立游标对象
cur = conn.cursor()
执行SQL命令
cur.execute("insert into t_weather_new(timestamp, content, type) values (%s, %s, %f) ON DUPLICATE KEY UPDATE t_weather_new.content = (%s), t_weather_new.timestamp = (%s)", (repr(timestamp), repr(res_str), 0, repr(res_str), repr(timestamp)))
提交到数据库
conn.commit()
关闭游标对象和数据库连接
cur.close()
conn.close()
请根据你的具体需求选择合适的方法。