zoukankan      html  css  js  c++  java
  • python 操作mysql

    python-mysqldb : http://www.cnblogs.com/wupeiqi/articles/5095821.html      python3不支持mysqldb

    pymysql : http://www.cnblogs.com/wupeiqi/articles/5713330.html       https://www.cnblogs.com/wt11/p/6141225.html

    本篇对于Python操作MySQL主要使用两种方式:

    • 原生模块 pymsql

    pymsql

    pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同。

    下载安装

    pip3 install pymysql  

    二、使用操作

    1、执行SQL

    import pymysql
      
    # 创建连接
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1', charset='utf8')#host主机名,用ifconfig查询出,db后是数据库名字  
    # 创建游标
    cursor = conn.cursor()
      
    # 执行SQL,并返回收影响行数
    effect_row = cursor.execute("select * from tb7")
      
    # 执行SQL,并返回受影响行数
    #effect_row = cursor.execute("update tb7 set pass = '123' where nid = %s", (11,))
      
    # 执行SQL,并返回受影响行数,执行多次
    #effect_row = cursor.executemany("insert into tb7(user,pass,licnese)values(%s,%s,%s)", [("u1","u1pass","11111"),("u2","u2pass","22222")])
      
      
    # 提交,不然无法保存新建或者修改的数据
    conn.commit()
      
    # 关闭游标
    cursor.close()
    # 关闭连接
    conn.close()
    
    注意:存在中文的时候,连接需要添加charset='utf8',否则中文显示乱码。
    

    2、获取查询数据

    import pymysql
     
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
    cursor = conn.cursor()
    cursor.execute("select * from tb7")
     
    # 获取剩余结果的第一行数据
    row_1 = cursor.fetchone()
    print row_1
    # 获取剩余结果前n行数据
    # row_2 = cursor.fetchmany(3)
     
    # 获取剩余结果所有数据
    # row_3 = cursor.fetchall()
     
    conn.commit()
    cursor.close()
    conn.close()

    3、获取新创建数据自增ID

    可以获取到最新自增的ID,也就是最后插入的一条数据ID

    import pymysql
     
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
    cursor = conn.cursor()
    effect_row = cursor.executemany("insert into tb7(user,pass,licnese)values(%s,%s,%s)", [("u3","u3pass","11113"),("u4","u4pass","22224")])
    conn.commit()
    cursor.close()
    conn.close()
    #获取自增id
    new_id = cursor.lastrowid      
    print new_id

    4、移动游标

    操作都是靠游标,那对游标的控制也是必须的

    注:在fetch数据时按照顺序进行,可以使用cursor.scroll(num,mode)来移动游标位置,如:
     
    cursor.scroll(3,mode='relative') # 相对当前位置移动,指的是由当前的记录位置开始的第N个位置,这里指指针指向当前数据位置的后数3位的记录。
    cursor.scroll(2,mode='absolute') # 相对绝对位置移动,指的是由第一条记录开始的第N个位置,这里指针指向数据的第2位这条记录。
    #查询当前数据库中指针位置:lastrowid
    print(cursor.lastrowid)
    

    5、fetch数据类型

    关于默认获取的数据是元祖类型,如果想要或者字典类型的数据,即:

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # __author__ = "TKQ"
    import pymysql
     
    conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='tkq1')
    #游标设置为字典类型
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor.execute("select * from tb7")
     
    row_1 = cursor.fetchone()
    print row_1  #{u'licnese': 213, u'user': '123', u'nid': 10, u'pass': '213'}
     
    conn.commit()
    cursor.close()
    conn.close()
    

      

     

     

     

     
  • 相关阅读:
    go函数
    Linux 查看磁盘容量、查找大文件、查找大目录
    五分钟理解一致性哈希算法(consistent hashing)
    使用Java实现三个线程交替打印0-74
    Python实现IOC控制反转
    Wannafly挑战赛5 A珂朵莉与宇宙 前缀和+枚举平方数
    Yandex Big Data Essentials Week1 Scaling Distributed File System
    Yandex Big Data Essentials Week1 Unix Command Line Interface Processes managing
    Yandex Big Data Essentials Week1 Unix Command Line Interface File Content exploration
    Yandex Big Data Essentials Week1 Unix Command Line Interface File System exploration
  • 原文地址:https://www.cnblogs.com/tianqizhi/p/9283071.html
Copyright © 2011-2022 走看看