import pymysql
import traceback
#获得链接
def get_conn():
conn = pymysql.connect(
host="localhost",
user="root",
passwd="",
db="news",
charset="utf8"
)
cursor = conn.cursor()
return conn, cursor
#关闭连接
def close_conn(conn, cursor):
if cursor:
cursor.close()
if conn:
conn.close()
#查
def query(sql,*args):
conn, cursor = get_conn()
cursor.execute(sql, args)
res = cursor.fetchall()
close_conn(conn, cursor)
return res
#增删改
def insql(sql, *args):
conn, cursor = get_conn()
cursor.execute(sql, args)
close_conn(conn, cursor)
#更新
def update(sql, *args):
conn, cursor = get_conn()
cursor.execute(sql, args)
conn.commit()
close_conn(conn, cursor)