- mysql drop table
- mysql limit
更新表
您可以使用 "update" 语句来更新表中的现有记录:
实例把地址列中的 "valley 345" 覆盖为 "canyoun 123":
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "update customers set address = 'canyon 123' where address = 'valley 345'" mycursor.execute(sql) mydb.commit() print(mycursor.rowcount, "record(s) affected")
运行实例
重要:请注意语句 mydb.commit()。需要进行更改,否则不会表不会有任何改变。
请注意 update 语法中的 where 子句:where 子句指定应更新的记录。如果省略 where 子句,则所有记录都将更新!
防止 sql 注入
在 update 语句中,转义任何查询的值都是个好习惯。
此举是为了防止 sql 注入,这是一种常见的网络黑客技术,可以破坏或滥用您的数据库。
mysql.connector 模块使用占位符 %s 来转义 delete 语句中的值:
实例使用占位符 %s 方法来转义值:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "update customers set address = %s where address = %s" val = ("valley 345", "canyon 123") mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record(s) affected")
运行实例
- mysql drop table
- mysql limit
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!