python数据库小脚本
def mysql_conn(): while 1: try: conn, mysql = mysql_conn_e() return conn, mysql except Exception as e: print("save 链接数据库 出错") time.sleep(1) def mysql_conn_e(): conn = pymysql.connect(host=DBHOST, port=DBPORT, user=DBUSER, password=DBPWD, database=DBNAME, charset=DBCHAR) mysql = conn.cursor(cursor=pymysql.cursors.DictCursor) return conn, mysql
conn, mysql = mysql_conn() conn.ping(reconnect=True)
mysql.execute(sql_1)
result_all = mysql.fetchall()
// update,insert,delete
conn.ping(reconnect=True)
mysql.execute(sql_2, (canshu,))
conn.commit()
mysql.close()
conn.close()