• 肖sir___高级讲师___第二个月课堂019讲解__pymysql(001)



    python操作mysql数据库

    pymysql 库名,关于数据库操作的库

    一、:pymysql下载安装

    方法一:pip3 install pymysql
    方法二:在pycharm 中搜素pymysql 下载

    ===================================================

    二、

    import pymysql #导入pymysql库====》用来连接数据库
    #通过pymysql库里面的Connection这个类创建一个数据库连接对象db

    方式一:
    db = pymysql.Connection(host='192.168.153.130',
    user='root',
    password='123456',
    database='hz',
    port=3306,
    charset='utf8')
    方式二:

    db = pymysql.connect(host="192.168.153.130",user="root",passwd="123456",db="hz", port=3306,charset='utf8')

    #connect和Connection 两个单词都可以

    解释:
    host:mysql服务器ip地址
    user:用户名
    password:密码  (password或passwd二取一)
    db:数据库名称  (db或database二取一)
    port:端口号
    charset= "utf-8":字符集编码

    ===========================================================

    操作方法:

    第一步:创建游标对象

    第二步:执行对象脚本(sql语句)

    #通过对象db用Connection这个类里面的cursor方法创建一个游标对象
    #游标的作用:1、执行sql语句 2、获取执行后的结果返回的结果会存在游标对象中

    # r = db.cursor()#创建一个游标对象,通过游标象执行sql语句
    # sql = 'select * from dcs'
    # r.execute(sql)#执行sql语句

    注解:1、execute()方法是代表执行

    --------------------------------------------------------------------------

    #通过游标获取结果的第一行数据(fetchone)

    import pymysql #或者引用pymysql
    db=pymysql.Connection(host="192.168.153.130",user="root",passwd="123456",db="hz", port=3306,charset='utf8')
    r1=db.cursor()  #游标对象
    sql1= "select  *  from student2"#sql语句
    r1.execute(sql1)              #执行语句
    one=r1.fetchone()            #游标获取并占用一条数据 ,就是表示显示第一行数据
    print (one)
    print(type(one))
    

    ----------------------------------------------------------------------------  

    #获取剩余的结果中所有数据(fetchall)

    import pymysql   #或者引用pymysql
    db = pymysql.Connection(host="192.168.153.130",user="root",passwd="123456",db="hz", port=3306,charset='utf8')
    r=db.cursor()  #游标对象
    sql1= "select  *  from student2"#sql语句
    r.execute(sql1)              #执行语句
    all=r.fetchall()            #游标获取未被占用的所有数据.
    for i in all:                #for循环遍历出来,
        print (i)

    备注:也可以使用直接打印出来
    print(all)

    ======================================================== 

    指定获取剩余结果中的数据fetchmany(size=3)

    import pymysql   #或者引用pymysql
    db = pymysql.Connection(host="192.168.153.130",user="root",passwd="123456",db="hz", port=3306,charset='utf8')
    r3=db.cursor()  #游标对象
    sql1= "select  *  from student2"#sql语句
    r3.execute(sql1)              #执行语句
    u=r3.fetchmany(size=3)        #游标获取剩余结果数据中的条数,根据size设置数量.
    for i in u:
        print (i)
    # print(u)

    【备注:size=3表示指定获取剩余3条数据】

    ========================================================

    #执行删除符合条件的数据,并且查询

    import pymysql   #或者引用pymysql
    db=pymysql.connect("192.168.153.130","root","123456","hz",3306,charset='utf8')   #连接数据库
    r=db.cursor()  #游标对象
    # sql4="delete from student2 where id=6"
    # r.execute(sql4)             #删除符合条件的数据
    # #备注:查看数据库中删除的数据
    #--------------------------------
    #如需要使用语句查看,如下
    select_sql = 'select * from student2' #定义一个查询的sql语句(查看删除后的数据)
    r.execute(select_sql)
    u= r.fetchall()
    for i in u:
        print (i)

    ========================================================

     #查询更新后的数据

    import pymysql   #或者引用pymysql
    db=pymysql.connect("192.168.153.130","root","123456","hz",3306,charset='utf8')   #连接数据库
    r=db.cursor()  #游标对象
    sql1="select count(*) from student2"    #sql语句
    r.execute(sql1)
    all=r.fetchall()      #游标获取未被占用的所有数据.
    print (all)

    =======================================================

    import pymysql
    db = pymysql.connect('192.168.153.130','root','123456','hz',3306,charset='utf8')
    cursor = db.cursor()
    sql = "SELECT * FROM student2;"
    print(cursor.execute(sql))
    print(cursor.fetchall())
    try:
        cursor.execute(sql)
        db.commit()
        print("插入成功")
    except:
        print("插入失败")
        db.rollback()
    cursor.close()
    db.close()

     ==============================================================================

    以下案例方法一一样:

    import pymysql
    class Db_Utils:

    def __init__(self,host,user,passwd,db,port,):
    self.host = host
    self.user = user
    self.passwd = passwd
    self.db =db
    self.port = port
    def get_connection(self): # 实例方法
    '''创建数据库的连接对象'''
    db = pymysql.connect(self.host,self.user,self.passwd,self.db,self.port,charset='utf8')
    return db
    #讲解:开始封装查询一条数据实例的方法
    def find_one(self,sql):
    '''封装查询一条数据的实例方法'''
    db = self.get_connection() #拿到数据库连接对象
    cursor = db.cursor() #创建游标对象
    cursor.execute(sql)
    one = cursor.fetchone() #拿到第一行的数据
    return one
    def find_all(self,sql):
    '''封装查询所有数据的实例方法'''
    db = self.get_connection() # 拿到数据库连接对象
    cursor = db.cursor() # 创建游标对象
    cursor.execute(sql)
    all = cursor.fetchall() # 拿到所有的数据
    return all

    if __name__ == '__main__':
    d = Db_Utils(host = "192.168.153.130",user="root",passwd="123456",db="hz", port=3306)
    #print (d.find_one('select * from student2 '))
    print(d.find_all('select * from student2 '))


    =============================================================================================
    import  pymysql
    class Db():
    def __init__(self,host,user,passwd,db,port,):
    self.host=host
    self.user=user
    self.passwd=passwd
    self.db=db
    self.port=port
    def lianjie(self):

    db=pymysql.Connection(self.host,self.user,self.passwd,
    self.db,self.port,charset="utf8")
    return db
    def one(self,sql):
    db=self.lianjie()
    r=db.cursor()
    r.execute(sql)
    one1=r.fetchone()
    print(one1)
    def many(self,sql):
    db = self.lianjie()
    r = db.cursor()
    r.execute(sql)
    one2 = r.fetchmany(size=2)
    print(one2)
    def all(self,sql):
    db = self.lianjie()
    r = db.cursor()
    r.execute(sql)
    one3 = r.fetchall()
    print(one3)
    if __name__ == '__main__':
    d=Db(host="192.168.153.130",
    user="root",passwd="123456",port=3306,
    db="hz")
    # d.one("select * from student2")
    #d.many("select * from student2")
    d.all("select * from student2")

      

  • 相关阅读:
    C/S和B/S结构区别整理
    JavaScript特点、优缺点及常用框架
    ExtJs特点、优缺点及注意事项
    Oracle SQL 脚本跟踪
    解决 C#中 SQL脚本执行超时 问题
    oracle 时间转化函数及常见函数 .
    Spring.NET 1.3.2 集成 NHibernate 3.2 1 下载软件
    SQL Server 跨服务器 不同数据库之间复制表的数据的方法:
    spring.net nhibernate 分布布式事务(上)
    set xact_abort ON 你懂的, 在分布式数据库事务中,用到. 在事务中,若出现错误,系统即默认回滚事务
  • 原文地址:https://www.cnblogs.com/xiaolehua/p/14176334.html
Copyright © 2020-2023  润新知