安装pymysql模块及使用

安装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()

实际操作mysql的时候会遇到的一个问题:

原文地址:https://www.cnblogs.com/dbslinux/p/11905758.html