安裝pymysql模塊:
https://www.cnblogs.com/Eva-J/articles/9772614.html
file--settings for New Projects---Project Interpreter----+---pymysql安裝就好。
若忘記函數用法,鼠標放在內建函數上,Ctrl+B,看源碼
pymysql常見報錯:
https://www.cnblogs.com/HByang/p/9640668.html
在cmd中mysql中建homework庫中表student:
創建表:
create table student
(
stuid int not null,
stuname varchar(4) not null,
stusex bit default 1,
stuaddr varchar(50),
colid int not null comment '學院編號',
primary key (stuid)
);
插入數據:
insert into tb_student values
(1001,'小強',1,'四川成都',30),
(1002,'花月',1,'四川成都',10),
(1003,'小紅',1,'四川成都',20),
(1004,'小白',1,'四川成都',10),
(1005,'小青',1,'四川成都',30),
(1006,'小黑',1,'四川成都',10),
(1007,'白龍',1,'四川成都',20),
(1008,'小花',1,'四川成都',20),
(1009,'白馬',1,'四川成都',30),
(1010,'冷面',1,'四川成都',30),
(1011,'白潔',1,'四川成都',20),
(1012,'紫薇',1,'四川成都',20),
(1013,'楊洋',1,'四川成都',20);
pymysql模塊.py文件操作MySQL:
import pymysql
conn = pymysql.connect(host="127.0.0.1", user="root", password="181818",database="homework")
cur = conn.cursor()
try:
cur.execute('select * from student')
ret = cur.fetchall()
print(ret)
except pymysql.err.ProgrammingError as e:
print(e)
cur.close() #歸還資源
conn.close()
pymysql的增刪改:
conn = pymysql.connect(host='127.0.0.1',
user='root',
password="123",
database='homework')
cur = conn.cursor() # cursor游標
try:
# cur.execute('insert into student values(18,"男",3,"大壯")')
# cur.execute('update student set gender = "女" where sid = 17')
cur.execute('delete from student where sid = 17')
conn.commit() #提交數據
except Exception as e:
print(e)
conn.rollback() #回滾 可以試一下 myisam
cur.close()
conn.close()