python写入mysql数据库(已封装)

def insert_mysql(my_sql):
    #连接数据库
    link=pymysql.connect(
    host = '127.0.0.1' # 连接名称,默认127.0.0.1
    ,user = 'root' # 用户名
    ,passwd='******' # 密码
    ,port= 3306 # 端口,默认为3306
    ,db='studentdb' # 数据库名称
    ,charset='utf8' # 字符编码
    )
    cur = link.cursor() # 生成游标对象
    sql=my_sql # SQL语句
    cur.execute(sql) # 执行SQL语句
    link.commit()  
    cur.close() # 关闭游标
    link.close() # 关闭连接

你可能感兴趣的:(mysql,数据库,python)