zoukankan      html  css  js  c++  java
  • 【Python】学习笔记5-模块pymysql操作mysql数据库

    import pymysql
    #其他数据库,比如oracle 模块是pyoracle

    #1、链接数据库mysq ip 端口号 密码 账户 数据库
    #2、建立游标
    #3、执行sql
    #4、获取结果
    #5、关闭连接,关闭游标


    # 1、show table 例子
     1 # conn = pymysql.connect(host = 'x.x.x.x',
     2 #                 user = 'jxz',passwd = '123456',
     3 #                 port = 3306, db = 'jxz', charset='utf8')#charset 必须是utf8不能是utf-8
     4 # cur = conn.cursor() #建立游标,游标你就认为是仓库管理员
     5 # # cur.execute('show tables;') #执行sql语句
     6 # cur.execute('select * from bt_stu limit 5;') #执行sql语句
     7 # # print(cur.execute('show tables;')) #执行sql语句 返回 10
     8 # print(cur.fetchall()) #获取sql语句执行的结果
     9 # res = cur.fetchall() #获取sql语句执行的结果 (('bt_stu',), ('hkk',), ('hkk2',), ('jxz_stu',), ('products_nyy',), ('stu',), ('user',), ('user_nyy',), ('user_passwd',), ('zmx',))
    10 # print(res[0][3])
    11 # cur.close() #关闭游标
    12 # conn.close() #关闭连接

    # # 2、查询例子
    # conn = pymysql.connect(host = 'X.X.X.X',
    #                 user = 'jxz',passwd = '123456',
    #                 port = 3306, db = 'jxz', charset='utf8')#charset 必须是utf8不能是utf-8
    # cur = conn.cursor() #建立游标,游标你就认为是仓库管理员
    # cur.execute('select * from bt_stu limit 2;') #执行sql语句
    # # res = cur.fetchall() #获取sql语句执行的结果,把结果放到元组(数组)里面,根据数组取结果,如 res[0][3]
    #     #((1, '贾梦缘', 1, '18612341231', '靠山屯', 1), (2, '处长', 0, '19212345678', '靠山屯', 1), (4, '处长', 0, '19312345678', '靠山屯', 1), (5, '处长', 0, '19312345671', '靠山屯', 1), (6, 'zdq', 0, '12312345678', '靠山屯', 1))
    # # res = cur.fetchall() #获取所有结果
    # res = cur.fetchone() #只获取一条结
    # res = cur.fetchone() #再获取剩下的 第一条结果 #(2, '处长', 0, '19212345678', '靠山屯', 1)
    # res = cur.fetchall() #取所有剩下的
    # cur.scroll(0,mode='absolute')  #移动游标,到最前面,#一般很少去移动游标
    # cur.scroll(-10,mode='relative')  #移动游标,相对于当前位置,-X是向前移动X个,X是向后移动X个
    # res = cur.fetchone()
    # print(res)
    # cur.close() #关闭游标
    # conn.close() #关闭连接


    # 3、insert
     1 # insert update delete 执行这些sql,必须要提交,才能生效 ,conn.commit
     2 # conn = pymysql.connect(host = 'X.X.X.X',
     3 #                 user = 'jxz',passwd = '123456',
     4 #                 port = 3306, db = 'jxz', charset='utf8')#charset 必须是utf8不能是utf-8
     5 # cur = conn.cursor() #建立游标,游标你就认为是仓库管理员
     6 # sql = "INSERT INTO `bt_stu` ( `real_name`, `sex`, `phone`, `class`, `type`) VALUES ( 'cm1', '1', '15712341231', '靠山屯', '1');"
     7 # cur.execute(sql) #执行sql语句
     8 # conn.commit()
     9 # cur.close() #关闭游标
    10 # conn.close() #关闭连接

    # 4、cursor 不输出元组,输出字典
     1 conn = pymysql.connect(host = 'X.X.X.X',
     2                 user = 'jxz',passwd = '123456',
     3                 port = 3306, db = 'jxz', charset='utf8')#charset 必须是utf8不能是utf-8
     4 cur = conn.cursor(cursor=pymysql.cursors.DictCursor) #建立游标,游标你就认为是仓库管理员
     5 sql = "select * from bt_stu limit 2"
     6 cur.execute(sql) #执行sql语句
     7 print(cur.fetchone()) #一条语句,返回就是一条字典
     8 # print(cur.fetchall()) #数组里面包含字典[{'phone': '18612341231', 'id': 502, 'sex': 1,。。。。
     9 cur.close() #关闭游标
    10 conn.close() #关闭连接

    5、利用executemany二维数组插入多行数据

    sql = 'insert into seq(blue,red,date) VALUES (%s,%s,%s)'
    print(sql)
    all_res = [
        ['00','01,02,03,04,05,06','2018-01-04'],
        ['00','01,02,03,04,05,06','2018-01-04']
    ]
    cur.executemany(sql,all_res)
  • 相关阅读:
    source : not found 原因及解决办法
    hdfs 数据坏块导致datanode不能正常上报数据块
    hadoop 基准测试
    Linux yum 安装mysql的时候指定安装版本
    如何从头构建一个只有bash的镜像
    创建自己的基础镜像
    go学习(2)变量
    Go学习(1)go安装
    spark on yarn 错误
    mysqld: File './mysql-bin.index' not found (Errcode: 13
  • 原文地址:https://www.cnblogs.com/amengmeng/p/8361338.html
Copyright © 2011-2022 走看看