zoukankan      html  css  js  c++  java
  • python 学习_第四模块 python 对mysql的操作

    python 学习_第四模块 python 对mysql的操作

    1 查询

    import  pymysql
    
    user="egon1"
    pwd="123"
    
    conn = pymysql.connect(
        host="10.0.0.87",
        user="test",
        password="123456",
        database="db10",
        charset="utf8"
    )
    
    cursor = conn.cursor()
    
    sql = "select * from test1 where name=%s and password=%s"
    
    res = cursor.execute(sql,[user,pwd])
    
    if res:
        print("ok")
    else:
        print("error")
    
    
    cursor.close()
    conn.close()

    2. 插入 修改 删除

    import  pymysql
    conn = pymysql.connect(
        host="10.0.0.87",
        user="test",
        password="123456",
        database="db10",
        charset="utf8")
    
    # 游标
    cursor = conn.cursor()
    
    sql = 'insert into test1(name,password) value("ysl","111");'
    res = cursor.execute(sql)
    print(res)
    conn.commit()
    cursor.close()
    conn.close()
    import  pymysql
    conn = pymysql.connect(
        host="10.0.0.87",
        user="test",
        password="123456",
        database="db10",
        charset="utf8")
    
    # 游标
    cursor = conn.cursor()
    sql = 'insert into test1(name,password) value(%s,%s);'
    cursor.execute(sql,['yy','666'])
    conn.commit()
    cursor.close()
    conn.cursor()
    import  pymysql
    conn = pymysql.connect(
        host="10.0.0.87",
        user="test",
        password="123456",
        database="db10",
        charset="utf8")
    
    # 游标
    cursor = conn.cursor()
    sql = 'insert into test1(name,password) value(%s,%s);'
    cursor.executemany(sql,[("y1",'1'),("y2",'2'),("y3",'3'),("y4",'4')])
    conn.commit()
    cursor.close()
    conn.cursor()

    3. 查询

    import  pymysql
    conn = pymysql.connect(
        host="10.0.0.87",
        user="test",
        password="123456",
        database="db10",
        charset="utf8")
    
    # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    cursor = conn.cursor()
    sql='select * from test1;'
    rows=cursor.execute(sql)
    
    
    # cursor.scroll(3,mode='absolute') # 相对绝对位置移动
    # cursor.scroll(3,mode='relative') # 相对当前位置移动
    
    #res1=cursor.fetchone()
    # res3=cursor.fetchone()
    # res4=cursor.fetchmany(2)
    res5=cursor.fetchall()
    
    print(res5)
    
    
    cursor.close()
    conn.close()

     

     

  • 相关阅读:
    Java NIO使用及原理分析(二)(转)
    Java NIO使用及原理分析 (一)(转)
    虚拟机字节码执行引擎
    虚拟机类加载机制
    浅析Java中的final关键字
    java内存模型7-处理器内存模型
    java内存模型6-final
    最小生成树
    有向图的基本算法
    并查集(Union-Find)算法介绍
  • 原文地址:https://www.cnblogs.com/augustyang/p/11151206.html
Copyright © 2011-2022 走看看