一,pymysql连接数据库(查)
def Connect_Database(sql):
try:
conn=pymysql.connect(user="用户名",password="密码",host="数据库IP",database='数据库')
cur=conn.cursor()
cur.execute(sql)
data=cur.fetchall()
return data
except Exception as e:
print(e)
finally:
cur.close()
conn.close()
def select_user():
sql="select * from user"
date=Connect_Database(sql)
二,pymysql提交数据(添改删)
def Commint_Data(sql):
try:
conn = pymysql.connect(user="你的用户名",password="你的密码",host="你的数据库IP",database='你的数据库')
cur = conn.cursor()
cur.execute(sql)
conn.commit()
except Exception as e:
conn.rollback()
print(e)
finally:
cur.close()
conn.close()
def add_user():
sql="insert into user(username,password)values("张三","123456")
Commint_Data(sql)