每日日报2021.11.9
完成内容:
python的学习 mysql
1.limit
import mysql.connector
mydb = mysql.connector.connect( host="localhost", user="root", passwd="123456", database="runoob_db" )
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM sites LIMIT 3")
myresult = mycursor.fetchall() for x in myresult: print(x)
2.offset
import mysql.connector
mydb = mysql.connector.connect( host="localhost", user="root", passwd="123456", database="runoob_db" )
mycursor = mydb.cursor() mycursor.execute("SELECT * FROM sites LIMIT 3 OFFSET 1")# 0 为 第一条,1 为第二条,以此类推
myresult = mycursor.fetchall() for x in myresult: print(x)
3.delete
import mysql.connector
mydb = mysql.connector.connect( host="localhost", user="root", passwd="123456", database="runoob_db" )
mycursor = mydb.cursor()
sql = "DELETE FROM sites WHERE name = 'stackoverflow'" mycursor.execute(sql)
mydb.commit() print(mycursor.rowcount, " 条记录删除")
4.%s
import mysql.connector
mydb = mysql.connector.connect( host="localhost", user="root", passwd="123456", database="runoob_db" )
mycursor = mydb.cursor()
sql = "DELETE FROM sites WHERE name = %s" na = ("stackoverflow", )
mycursor.execute(sql, na) mydb.commit() print(mycursor.rowcount, " 条记录删除