• Pycharm操作数据库


    Pymysql

    • 用于连接mysql数据库

    连接数据库

    data_ip = "192.168.34.128"
    data_name = "lch"
    data_pwd = "lch123"
    database = "day35_work"
    import pymysql #导入模块
    db = pymysql.connect(data_ip,data_name,data_pwd,database) #打开数据库连接
    cursor = db.cursor()  #创建一个游标对象
    create_book_sql = 
    """
    create table book02(name char(16),owner char(16),press char(16),price int(5),date date)
    """
    cursor.execute(create_book_sql) #执行create_book_sql
    

    import pymysql
    db = pymysql.connect(data_ip,data_name,data_pwd,database) #打开数据库连接
    cursor = db.cursor() #创建一个游标对象
    insert_book02_sql = """
    insert into book02 values
    ("学python从开始到放弃","alex","人民大学出版社",50,20180701),
    ("学mysql从开始到放弃 ","egon","北京工业地雷出版社",60,20180603),
    ("学html从开始到放弃","alex","机械工业出版社",20,20180401),
    ("学css从开始到放弃","wusir","机械工业出版社",120,20180502),
    ("学js从开始到放弃","wusir","机械工业出版社",100,20180730)
    """
    try:
        cursor.execute(insert_book02_sql) #执行语句
        db.commit() #事物号提交
    except:
        db.rollback()  #若报错,事物号回滚  
    db.close() ##关闭数据库连接
    

    import pymysql
    db = pymysql.connect(data_ip,data_name,data_pwd,database)
    cousor = db.cursor()
    delete_sql = "delete from book02 where price = 20"
    try:
        cousor.execute(delete_sql)
        db.commit()
    except:
        db.rollback()
    db.close()
    

    import pymysql
    db = pymysql.connect(data_ip,data_name,data_pwd,database)
    cousor = db.cursor()
    update_sql = "update book02 set price = 500 where price = 50"
    try:
        cousor.execute(update_sql)
        db.commit()
    except:
        db.rollback()
    db.close()
    

    import pymysql
    db = pymysql.connect(data_ip,data_name,data_pwd,database)
    cursor = db.cursor()
    sleetc_sql = "select * from book02"
    try:
        cursor.execute(sleetc_sql)
        data = cursor.fetchall() #获取所有数据
        for i in data:  
            print(i)
    except:
        print("ERROR")
    db.close()
    

  • 相关阅读:
    UVALIVE 4819 最大流
    Directx 3D编程实例:随机绘制的立体图案旋转
    PHP漏洞全解(四)-xss跨站脚本攻击
    PHP漏洞全解(三)-客户端脚本植入
    PHP漏洞全解(二)-命令注入攻击
    PHP漏洞全解(一)-PHP网站的安全性问题
    BT5下安装Metasploit4.5方法
    Ubuntu使用apt-get安装本地deb包
    Linux按照时间查找文件
    Linux系统备份与还原
  • 原文地址:https://www.cnblogs.com/Nayears/p/12133165.html
Copyright © 2020-2023  润新知