Python连接数据库

import pymysql

def ceshi():
    conn = pymysql.connect(
        host = "127.0.0.1",
        port = 3306,
        db = "crawled",
        user = 'root',
        passwd = '123456',
    )
    cursor = conn.cursor()
    sql = "select * from crawled_goods where batch_id=2019071400 and channel = '*******'"
    cursor.execute(sql)
    result = cursor.fetchall()
    print(result)
    sql_2 = "update crawled_goods set good_index='%s' where id='%s'" % (60, id)
    try:
        cursor.execute(sql_2)
        conn.commit()  # 没有异常就提交,然后才能生效
        print(11111)
    except Exception as e:  # 出现异常后将异常信息记入到e中,然后打印出异常,以供查看
        print(e)
        conn.rollback()  # 如果有异常就回滚

if __name__ == "__main__":
    ceshi()

  

原文地址:https://www.cnblogs.com/qiaoer1993/p/11189293.html