python操作数据库
python操作数据库
插入数据
-
import pymysql conn = pymysql.Connection(host = '127.0.0.1',user = 'root',password = '123',database = 'day39') cur = conn.cursor() username = "太亮" sql = 'insert into employee(emp_name,sex,age,hire_date) values (%s,"female",88,20170808)' cur.execute(sql,(username,)) conn.commit()#插入需要提交,不然只是在内存中做出了操作,数据库中数据没改变 cur.close() conn.close()
查询数据
-
#查询 import pymysql conn = pymysql.Connection(host = '127.0.0.1',user = 'root',password = '123',database = 'day39') cur = conn.cursor()#游标数据库操作符 sql = 'select emp_name,salary from employee where age < %s' cur.execute(sql,(80,)) #获取结果 ret1 = cur.fetchone()#接受一个数据 ret2 = cur.fetchmany(2)#接受多个数据,可设定参数 ret3 = cur.fetchall()#全部接受 print(ret1) print(ret2) print(ret3) cur.close() conn.close()
其余暂无>>>>