资讯专栏INFORMATION COLUMN

Python如何操作MySQL

zhjx922 / 1954人阅读

摘要:安装和的安装图解破解可以看下这个文章链接的增删改查通过提供的模块实现对数据库的操作,这个地方注意使用的是,的话使用模块安装模块创建连接创建游标修改执行,并返回受影响行数添加执行,并返回受影响行数查询删除执行,并

安装Mysql和Navicat for MySQL

mysql的安装图解https://jingyan.baidu.com/art...
navicat for mysql破解可以看下这个文章https://www.cnblogs.com/da199...

Python链接Mysql的增删改查
通过Python提供的pymysql模块实现对mysql数据库的操作,这个地方注意python3.x使用的是pymysql,python2.x的话使用mysqldb模块
安装pymysql模块:pip install PyMySQL
import pymysql
  
# 创建连接
conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123456", db="student")
# 创建游标
cursor = conn.cursor()

# 修改----执行SQL,并返回受影响行数
# effect_row = cursor.execute("update user set name=%s,pwd=%s where id=%s", ("aaa","bb", 1))
  
# 添加----执行SQL,并返回受影响行数
# cursor.execute("insert into user (name, pwd) values (%s,%s)", ("lidao","aaa"))
# 查询----
cursor.execute("select * from user")
stus = cursor.fetchall()   
for stu in stus:
        print("id:%d; name: %s; pwd: %s; " %(stu[0], stu[1], stu[2]))

# 删除---执行SQL,并返回受影响行数
cursor.execute("delete from user where id=%s", (2))

# 提交,不然无法保存新建或者修改的数据
conn.commit()
#如果不加这个就手动添加autocommit=True 自动提交
#db=pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="123456",db="school",charset="utf8",autocommit=True)


  
# 关闭游标
cursor.close()
# 关闭连接
conn.close()
自己封装helper类
import pymysql  

class dbhelper():
    def __init__(self,host,port,user,passwd,db,charset="utf8"):
        self.host=host
        self.port=port
        self.user=user
        self.passwd=passwd
        self.db=db
        self.charset=charset
    #创建一个链接
    def connection(self):
        #1. 创建连接
        self.conn = pymysql.connect(host=self.host, port=self.port, user=self.user, passwd=self.passwd, db=self.db,charset=self.charset)
        #2. 创建游标
        self.cur = self.conn.cursor()
    #关闭链接
    def closeconnection(self):
        self.cur.close()
        self.conn.close()
    #查询一条数据
    def getonedata(self,sql):
        try:
            self.connection()
            self.cur.execute(sql)
            result=self.cur.fetchone()
            self.closeconnection()
        except Exception:
            print(Exception)    
        return result
    #查询多条数据    
    def getalldata(self,sql):
        try:
            self.connection()
            self.cur.execute(sql)
            result=self.cur.fetchall()
            self.closeconnection()
        except Exception:
            print(Exception)             
        return result
    #添加/修改/删除 
    def executedata(self,sql):
        try:
            self.connection()
            self.cur.execute(sql)
            self.conn.commit()
            self.closeconnection()
        except Exception:
            print(Exception)       
              

封装好了以后,后续用到mysql的操作的地方都可以直接使用,栗子如下:

from mysqlhelper import *

db=dbhelper(host="127.0.0.1", port=3306, user="root", passwd="123456", db="school",charset="utf8")
result=db.getalldata("select * from class")
print(result)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/41117.html

相关文章

  • Slog3_如何使用PythonMysql进行数据交互

    摘要:现在,对接文件已经到位接下来测试数据库,如果还没安装或者安装了还没配置,移步数据库配置,完成数据库的安装和配置或者在官方下载,安装官方手册安装。 ArthurSlog SLog-3 Year·1 Guangzhou·China July 9th 2018 showImg(https://segmentfault.com/img/remote/1460000016093266?w=2...

    ccj659 评论0 收藏0
  • Python之使用Pandas库实现MySQL数据库的读写

    摘要:本次分享将介绍如何在中使用库实现数据库的读写。提供了工具包及对象关系映射工具,使用许可证发行。模块实现了与不同数据库的连接,而模块则使得能够操作数据库。   本次分享将介绍如何在Python中使用Pandas库实现MySQL数据库的读写。首先我们需要了解点ORM方面的知识。 ORM技术   对象关系映射技术,即ORM(Object-Relational Mapping)技术,指的是把关...

    darcrand 评论0 收藏0
  • [零基础学python]用Python操作数据库(3)

    摘要:用选择要操作的数据库,然后通过指针就可以操作这个数据库了。这样就在这个数据库中创建了一个名为的表这是查看表的方式。树欲静而风不止,小偷在行动。所以,要特别提醒诸位注意。 通过python操作数据库的行为,除了能够完成前面两讲中的操作之外(当然,那是比较常用的),其实任何对数据库进行的操作,都能够通过python-mysqldb来实现。 建立数据库 在《用python操作数据库(1)...

    BDEEFE 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<