這篇文章主要為大家詳解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免費學習筆記(深入)”;
3.?python
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END