pip install pymysql#安装
db = pymysql.connect("主机", "名称", "密码","库") cursor = db.cursor()# 使用 cursor() 方法创建一个游标对象 cursor cursor.execute(sql)#运行,sql是SQL语句 db.commit()# 提交到数据库执行 cursor.fetchone()#获取单条数据. cursor.fetchall()#返回全部的结果行.
例子:
import pymysql#导入 db = pymysql.connect("localhost", "gu", "123456","gu") cursor = db.cursor() csql = "SELECT * FROM `tan` WHERE `title` LIKE '春'" cursor.execute(csql) results = cursor.fetchall() print(results) db.close()
Comments NOTHING