首先需要引入依赖
import pymysql
# mysql链接初始化
# cursorclass
def mysql_model():
mysql_content = pymysql.connect(host="你的host", user="用户名",
password="密码",
database="数据库", charset="utf8", port=3306,
cursorclass=pymysql.cursors.DictCursor)
return mysql_content
#但是一定要注意这种拼接方法只能拼接变量为字符串的,如果是不同类型的请往下看
def delAppMysqlStr():
yesterday = "2020-01-08"
del_app_sql = "delete from table where day = '" + yesterday + "'"
mysql_content = mysql_model()
# 创建一个游标
cursor = mysql_content.cursor()
cursor.execute(del_app_sql)
mysql_content.commit()
cursor.close()
mysql_content.close()
return 1
#如果是不同类型请使用参数绑定形式进行赋值
#切记占位符%s一定要和values数量匹配
def addAppMysqlStr():
yesterday = "2020-01-08"
add_sql = "insert into table (`day`,`union_id`,`appid`,`show_count`,`click_count`,`click_reg`,`cpm`,`money`,`third_app_id`,`platform`,`created_at`) value (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
param = (yesterday, 1, 2, 3, 3.223, 2323,
6.66, 11, 2, 123, "萨达所大")
cursor.execute(add_sql, param)
mysql_content.commit()
cursor.close()
mysql_content.close()
return 1