• python使用pymysql链接mysql数据库实现增删改查


    1.新增

    import pymysql
    import datetime
    #打开数据库连接
    db = pymysql.connect("localhost", "root", "123456", "pymysqltest")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor=db.cursor()
    # SQL 插入语句
    sql="insert into user(name,sex,age) values('张三','李四',23)"
    try:
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
    except:
        # 如果发生错误则回滚
        db.rollback()
    # 关闭数据库连接
    db.close()

    插入datetime类型的数据时,可以将时间格式化成字符串。

    import pymysql
    import datetime
    #打开数据库连接
    db = pymysql.connect("localhost", "root", "123456", "pymysqltest")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor=db.cursor()
    nowdate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    print(nowdate)
    sql="insert into user(addtime) values('%s')" % (nowdate)
    #sql="insert into user(addtime) values('2018-09-02 21:22:22')"
    try:
        cursor.execute(sql)
        db.commit()
    except:
        db.rollback()
    db.close()

    2.删除和更新

    import pymysql
    import datetime
    #打开数据库连接
    db = pymysql.connect("localhost", "root", "123456", "pymysqltest")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor=db.cursor()
    # SQL 对应删除更新的sql
    #sql="delete from user where age > '%d'" % (20)
    sql="update user set age=age+1 where sex = '%c'" % ('')
    try:
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
    except:
        # 如果发生错误则回滚
        db.rollback()
    # 关闭数据库连接
    db.close()

    3.查询

    Python查询Mysql使用 fetchone() 方法获取单条数据, 使用fetchall() 方法获取多条数据。

    • fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
    • fetchall(): 接收全部的返回结果行.
    • rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。

    查询所有

    import pymysql
    import datetime
    #打开数据库连接
    db = pymysql.connect("localhost", "root", "123456", "pymysqltest")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor=db.cursor()
    # SQL 查询
    sql="select * from user"
    try:
        # 执行sql语句
        cursor.execute(sql)
        # 得到所有记录
        result = cursor.fetchall()
        for row in result:
            id=row[0]
            name=row[1]
            sex=row[2]
            age=row[3]
            print("id=%d,name=%s,sex=%s,age=%d"%(id,name,sex,age))
    except:
        # 如果发生错误则回滚
        print("Error: unable to fetch data")
    # 关闭数据库连接
    db.close()

    结果:

    id=1,name=张三,sex=男,age=21
    id=2,name=李四,sex=男,age=22
    id=3,name=王二,sex=男,age=23
    id=4,name=赵一,sex=女,age=24

    查询第一条

    import pymysql
    import datetime
    #打开数据库连接
    db = pymysql.connect("localhost", "root", "123456", "pymysqltest")
    # 使用 cursor() 方法创建一个游标对象 cursor
    cursor=db.cursor()
    # SQL 查询
    sql="select * from user"
    try:
        # 执行sql语句
        cursor.execute(sql)
        # 得到所有记录
        result = cursor.fetchone()
        print("id=%d,name=%s,sex=%s,age=%d"%(result[0],result[1],result[2],result[3]))
    except:
        # 如果发生错误则回滚
        print("Error: unable to fetch data")
    # 关闭数据库连接
    db.close()

    结果:

    id=1,name=张三,sex=男,age=21
  • 相关阅读:
    为什么要用泛型呢
    sql语句优化
    SQL Server 2008 问题——已成功与服务器建立连接,但是在登录过程中发生错误。
    CHECKFORILLEGALCROSSTHREADCALLS = FALSE
    数据类型 text 和 varchar 在 add 运算符中不兼容
    js中数组对象去重的方法
    json字符串、json对象、数组之间的转换
    如何用jQuery获得select的值
    SQL Server 性能调优
    TSQL 数据类型转换
  • 原文地址:https://www.cnblogs.com/dbutil/p/9566188.html
Copyright © 2020-2023  润新知