python+mysql

1.连接

参数:

host='主机地址'  user='用户名'  passwd='密码' db='数据库名'  port=端口  charset='utf8'


# 数据库连接
def Ex_Mysql():
    db = pymysql.connect(host='localhost', user='root', passwd='123', db='csf', port=3306, charset='utf8')
    print("数据库连接")
    cursor = db.cursor()
    cursor.execute("DROP TABLE IF EXISTS dangdangweb_info_detail")
    sql = """CREATE TABLE IF not EXISTS dangdangweb_info_detail (
            id int auto_increment primary key,

            title CHAR(100),
            price CHAR(100),
            source_price CHAR(100),
            discount CHAR(100),
            author CHAR(100),
            publish_time CHAR(100),
            publish CHAR(100),
            comment CHAR(100)
        )
        DEFAULT CHARSET=utf8"""

2. 添加


    # 添加
    db = pymysql.connect(host='localhost', user='root', passwd='123', db='csf', port=3306, charset='utf8')
    print("数据库连接")
    cursor = db.cursor()

    sql = "INSERT INTO dangdangweb_info_detail (title,price,source_price,discount,author,publish_time,publish,comment)\
        VALUES(%s,%s,%s,%s,%s,%s,%s,%s)"%\
        (str(titles), str(prices), str(source_prices), str(discounts), str(authors)
         , str(publish_times), str(publishs), str(comments))

    try:
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
    except:
        # 如果发生错误则回滚
        db.rollback()

    # 关闭数据库连接
    db.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值