• 操作mysql数据库


    import pymysql
    host='127.0.0.1'
    user = 'jxz'
    password = '123456'#字符串
    db = 'jxz'
    port = 3306#int类型
    connect = pymysql.connect(host=host,port=port,db=db,user=user,password=password,autocommit=True)#autocommit=True增删改会自动提交
    # cur = connect.cursor()#建立游标,仓库管理员
    cur = connect.cursor(pymysql.cursors.DictCursor)#建立字典游标类型,仓库管理员  {'id': 1, 'name': 'niuhy', 'sex': 'nv', 'age': 20, 'class': '111', 'addr': '河北'}
    
    # sql = 'insert into students values (3434,"test","girl",18,"tmz","重庆");'
    # sql = 'update students set age=19 where id = 3434'
    # sql = 'delete from students where id = 3434'
    
    # cur.execute(sql)
    # connect.rollback()#回滚
    # connect.commit()#增删改需要提交
    cur.execute('select * from students limit 5;')
    print(cur.description)#返回表的所有字段 (('id', 3, None, 10, 10, 0, False), ('name', 253, None, 80, 80, 0, False), ('sex', 253, None, 16, 16, 0, True), ('age', 3, None, 10, 10, 0, True), ('class', 253, None, 80, 80, 0, False), ('addr', 253, None, 200, 200, 0, True))
    # cur.execute(sql)
    for data in cur:#直接循环游标
        print(data)
    
    # result = cur.fetchall()#拿到所有结果
    # result = cur.fetchone()#1条结果
    # result = cur.fetchmany(2)#拿到2条结果
    # print(result)
    cur.close()
    connect.close()
  • 相关阅读:
    如何使用android模拟器截图
    android SD卡文件的读写
    res/raw下的资源文件读写
    window 运行指令(1)
    javax.swing.JOptionPane.showMessageDialog() 方法
    Eclipse快捷键
    EditPlus怎样自动换行
    java的HashCode方法
    eclipse汉化全程
    (转载)jdbc事务处理
  • 原文地址:https://www.cnblogs.com/Mezhou/p/13658386.html
Copyright © 2020-2023  润新知