Python3与MySQL

【python3与MySQL】

  • 交互类型(模块名):
  1. python3:
    pymysql $ sudo pip3 install pymysql
  2. python2:
    MySQLdb $ sudo pip install mysql-python
  • connection对象:
  1. 创建与数据库连接对象(调用connect()方法)
    conn = pymysql.connect(参数列表)
    参数列表:

    1. host:主机地址,本机:‘localhost’
    2. user:用户
    3. port:mysql端口,默认3306
    4. database:数据库名
      #db
    5. password:连接密码
      #passwd
    6. charset:编码方式,推荐使用utf8

    示例:

    conn = pymysql.connect(
    	host='localhost',user='root',password='123456',
    	database='db4',charset='utf8')
    
  2. 连接对象(如:conn)的方法:

    1. close() 关闭连接
    2. commit() 提交到数据库执行
    3. rollback() 事务回滚操作
    4. cursor() 创建游标对象,用于执行SQL语句获得结果
  3. 游标对象(cursor对象):

    1. 作用:
      执行sql语句
    2. 创建游标对象:
      调用连接对象的cursor()方法
      示例:cursor1 = conn.cursor()
    3. 游标对象的方法:
      1. execute(operation[,参数]) 执行sql语句
      2. close() 关闭游标对象
      3. fetchone() 获取结果集的第一条记录,返回一个元组
      4. fetchmany(n) 获取结果集的n条记录,返回一个大元组
      5. fetchall() 获取结果集的所有记录,返回一个大元组
  4. 总结(pymysql使用流程):

    1. 建立数据库连接

    2. 创建游标对象 cursor1=conn.cursor()

    3. 利用游标对象的方法操作数据库
      cursor1.execute(“sql语句”)

    4. 提交 conn.commit()

    5. 关闭游标 cursor1.close()

    6. 关闭数据库连接 conn.close()

  • 示例:
  1. 对库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()
    
    
  2. 对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()
    
  3. 对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()
    
  4. 自己创建一个类来写数据库
    【自己创建一个类用来写数据库】

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.矩形框代表实体,菱形框代表关系,椭圆形代表属性
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值