zoukankan      html  css  js  c++  java
  • 10-pymysql的应用

    import pymysql
    
    # user = input('请输入用户名:')
    # pwd = input('请输入密码:')
    
    # 1.创建连接
    conn = pymysql.connect(host='127.0.0.1', user='root', password="",
                           database='db8', port=3306, charset='utf8')
    # 2.创建游标
    cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
    
    # sql = "select * from userinfo where name='%s' and pwd='%s'" %(user,pwd)
    # sql = "select * from userinfo where name=%s and pwd=%s"
    # sql = "select * from userinfo where name=%(user)s and pwd=%(pwd)s"
    
    # 插入数据
    # sql = 'insert into userinfo(name,pwd) values(%s,%s)'
    # print(sql)
    
    # 3执行sql语句
    # r = cur.execute(sql,[user,pwd])
    # r = cur.execute(sql,(user,pwd))
    # r = cur.execute(sql,{"user":user,"pwd":pwd})
    
    #插入多条数据
    # r = cur.executemany(sql, [('李四', '110'), ('王五', '119')])
    # print(r)
    
    sql = 'select * from userinfo'
    cur.execute(sql)
    # row = cur.fetchone()
    # print(row)
    # row = cur.fetchone()
    # print(row)
    # row = cur.fetchone()
    # print(row)
    # row = cur.fetchone()
    # print(row)
    # row = cur.fetchone()
    # print(row)
    
    # rows = cur.fetchmany()
    # # print(rows)
    
    rows = cur.fetchall()
    print(rows)
    
    
    
    # 增 删 改 一定要提交
    conn.commit()
    
    # 关闭游标 关闭连接对象
    cur.close()
    conn.close()
    
    # if r:
    #     print('查询成功')
    # else:
    #     print('查询失败')
  • 相关阅读:
    matlab后处理保存avi动画
    Python3在Windows安装配置及简单试用
    Matlab,C++存取二进制
    批量修改文件名
    这是我的第一篇博客园博客
    android的平台架构及特性
    Android开发学习
    跟着9张思维导图学习Javascript
    如何使用css和jquery控制文章标题字数?
    分离构造器(2-2)
  • 原文地址:https://www.cnblogs.com/mayugang/p/10222867.html
Copyright © 2011-2022 走看看