【python3与MySQL】
- 交互类型(模块名):
- python3:
pymysql $ sudo pip3 install pymysql - python2:
MySQLdb $ sudo pip install mysql-python
- connection对象:
-
创建与数据库连接对象(调用connect()方法)
conn = pymysql.connect(参数列表)
参数列表:- host:主机地址,本机:‘localhost’
- user:用户
- port:mysql端口,默认3306
- database:数据库名
#db - password:连接密码
#passwd - charset:编码方式,推荐使用utf8
示例:
conn = pymysql.connect( host='localhost',user='root',password='123456', database='db4',charset='utf8')
-
连接对象(如:conn)的方法:
- close() 关闭连接
- commit() 提交到数据库执行
- rollback() 事务回滚操作
- cursor() 创建游标对象,用于执行SQL语句获得结果
-
游标对象(cursor对象):
- 作用:
执行sql语句 - 创建游标对象:
调用连接对象的cursor()方法
示例:cursor1 = conn.cursor() - 游标对象的方法:
- execute(operation[,参数]) 执行sql语句
- close() 关闭游标对象
- fetchone() 获取结果集的第一条记录,返回一个元组
- fetchmany(n) 获取结果集的n条记录,返回一个大元组
- fetchall() 获取结果集的所有记录,返回一个大元组
- 作用:
-
总结(pymysql使用流程):
-
建立数据库连接
-
创建游标对象 cursor1=conn.cursor()
-
利用游标对象的方法操作数据库
cursor1.execute(“sql语句”) -
提交 conn.commit()
-
关闭游标 cursor1.close()
-
关闭数据库连接 conn.close()
-
- 示例:
-
对库duobiao中的sheng表进行增删改
import pymysql # 1.创建数据库连接对象 conn = pymysql.connect(host = 'localhost', user = 'root',passwd = '123456', db = 'duobiao',charset = 'utf8') # 2.创建游标对象 cursor1 = conn.cursor() # 3.利用execute方法执行sql命令 try: sql_insert = "insert into sheng(s_name)\ values ('台湾省');" cursor1.execute(sql_insert) # 增加数据 sql_delete = 'delete from sheng where id = 2;' cursor1.execute(sql_delete) # 删除数据 sql_update = "update sheng set s_name='修改'\ where id=1;" cursor1.execute(sql_update) # 修改记录 print('ok') conn.commit() except Exception as e: conn.rollback() print('出现错误,以回滚',e) cursor1.close() conn.close()
-
对duobiao库中的sheng表进行查看
import pymysql conn = pymysql.connect(host='localhost', user='root',passwd='123456', db='duobiao',charset='utf8') cursor1 = conn.cursor() try: sql_select = 'select * from sheng;' cursor1.execute(sql_select) data = cursor1.fetchone() print('第一行:',data) print('*********************') data2 = cursor1.fetchmany(3) for i in data2: print(i) print('*********************') data3 = cursor1.fetchall() for i in data3: print(i) print('ok') conn.commit() except Exception as e: print(e) cursor1.close() conn.close()
-
对duobiao库中的sheng表进行添加数据
# sql语句的参数化 import pymysql conn = pymysql.connect(host='localhost', user='root',passwd='123456', db='duobiao',charset='utf8') cursor1 = conn.cursor() try: name = input('请输入省:') s_id = input('请输入对应编号:') sql_insert = 'insert into sheng(s_name,s_id)\ values(%s,%s);' cursor1.execute(sql_insert,[name,s_id]) print('ok') conn.commit() except Exception as e: conn.rollback() print('出现错误,以回滚',e) cursor1.close() conn.close()
-
自己创建一个类来写数据库
【自己创建一个类用来写数据库】
from pymysql import *
class mysqlpython:
def __init__(self,host,port,db,user,
passwd,charset='utf8'):
self.host = host
self.port = port
self.db = db
self.user = user
self.passwd = passwd
self.charset = charset
def open(self):
self.conn = connect(host=self.host,
port=self.port,db=self.db,
user=self.user,passwd=self.passwd,
charset=self.charset)
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def zhixing(self,sql):
self.open()
try:
self.cursor.execute(sql)
self.conn.commit()
print('ok')
except Exception as e:
self.conn.rollback()
print('Failed:',e)
self.close()
【调用测试】
from mysqlpython import mysqlpython
sqlh = mysqlpython('localhost',3306,'duobiao',
'root','123456')
sql_update = 'update sheng set id=150 where id=5;'
sqlh.zhixing(sql_update)
【5.workbench】
可用图形化界面操作数据库
【6.ER模型】
Entry - Relation
1.关系
1.分类:
1.一对一关系(1:1): 班级和班长
2.一对多关系 (1:n) : 公司和职工
3.多对多关系(m:n): 学生和课程
2.ER图的绘制
1.矩形框代表实体,菱形框代表关系,椭圆形代表属性