Python基础 之 Python MySQL - mysql-connector 驱动 13
Python MySQL - mysql-connector 驱动
Limit
如果我们要设置查询的数据量,可以通过 "LIMIT" 语句来指定
demo_mysql_test.py
读取前 3 条记录:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="123456",
database="baidu_db"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM sites LIMIT 3")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
执行代码,输出结果为:
(1, 'baidu', 'https://www.baidu.com')
(2, 'Google', 'https://www.google.com')
(3, 'Github', 'https://www.github.com')