详解Python如何建立数据库连接及插入数据

这篇文章主要为大家详解python如何建立数据库连接及python,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

建立数据库连接

def create_db_connect():    """  brief info for: create_db_connect           建立数据库链接            Args:          Return:          Raise:      """    conn = MySQLdb.connect(host = "rm-uf6wz3f7kb8sx983zo.mysql.rds.aliyuncs.com",                               user = "pv_cms",                               passwd = "pv_cms@123",                               port = 3306,                               charset = "utf8",                               db = "pv_interaction_bigdata")            return conn

插入数据:

def insert_to_info(conn):      cursor = conn.cursor()      sql = '''insert into info values(%s,%s)'''      l = [['liza','mary'],['dh','lxy']]#必须是list      cursor.executemany(sql,l)#执行多条插入数据操作      conn.commit()# 不执行不能插入数据      conn.close()  def insert_into_info(conn):      cursor = conn.cursor()    s  ql = '''insert into info values(%s,%s)'''      l = ('lisa','mary')#必须是tuple      cursor.execute(sql,l)#插入数据操作      conn.commit()# 不执行不能插入数据      conn.close()

【相关推荐】

1. python

立即学习Python免费学习笔记(深入)”;

python

3. python

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享