%s 占位符代表什么? [英] What does the %s placeholder stand for?

查看:61
本文介绍了%s 占位符代表什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用 MySQL 连接器/Python.在大多数教程中,我看到他们在 VALUES 子句中使用 %s 占位符在表中插入数据.然后,在下一行,值存储在一个元组中.然后,他们执行命令.

I am learning to use MySQL Connector/Python. In most of the tutorials, I see they use the %s placeholder inside VALUES clause for inserting data in a table. Then, on the next line, the values are stored in a tuple. And then, they execute the command.

sql = "INSERT INTO table (col1, col2) VALUES (%s, %s)"
val = ("Foo", "Bar")
cursor.execute(sql, val)

那么这个 %s 是什么意思?和C中使用的一样吗?我看到有人在 C 中使用它.我只是好奇.请提供任何参考链接,如果有的话.谢谢.

So what does this %s mean? Is it the same as the one used in C? I saw people using this in C. I am just curious. Please do give any reference link, if any. Thanks.

推荐答案

它告诉字符串该值将被转换为字符串并替换该位置的占位符.

It tells the string that the value will be converted to a string and replace the placeholder at that position.

但是连接器也支持准备好的语句.

But the connector also supports prepared statements.

cursor = connection.cursor(prepared=True)
sql_insert_query = """ INSERT INTO customers (name, address) VALUES (%s, %s)"""

insert_tuple_1 = ("Json", "BAker street")
insert_tuple_2 = ("Emma", "Avenue des Champs-Élysées")

cursor.execute(sql_insert_query, insert_tuple_1)
cursor.execute(sql_insert_query, insert_tuple_2)
connection.commit()

这篇关于%s 占位符代表什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆