mysql数据插入覆盖和时间戳的问题及解决
作者:YF-海纳百川 时间:2024-01-21 02:15:26
数据插入覆盖和时间戳问题
1.数据插入问题
当我们insert数据时,插入datetime类型,此类型的字段,存储数据格式为: YYYY-MM-DD,它支持的范围为'1000-01-01'到'9999-12-31',并且允许使用字符串或数字为此列复制。
日期赋值时,允许“不严格”语法:任何标点符都可以用做日期部分或时间部分之间的间割符。例如,'98-12-31 11:30:45'、'98.12.31 11+30+45'、'98/12/31 11*30*45'和'98@12@31 11^30^45'是等价的,对于不合法的将会转换为:0000-00-00 00:00:00
我们传的时候 insert into test(count_data) values (20190922). 不要把20190922转为2019-09-22
使用ignore关键字,避免重复插入记录可以使用:(主健会变化)
insert ignore into student_task_trace (student_id) VALUES (20)
使用Replace,如果旧记录与新记录有相同的值,则在新记录 * 入之前,旧记录被删除,存入新纪录:
REPLACE INTO student_task_trace (student_id) VALUES (20)
如果有主键的情况下,可以通过惟一索引来防止重复数据的插入
数据插入一个事件的时候 有好几个不同的值 总会覆盖 ,所以需要给不同值的字段设置个唯一索引。
除非清空(truncate 表名)
2. 时间加减问题
如果想做时间加减的话 必须把时间变成unix时间戳 如当前时间—创建时间
SELECT user_id, device, UNIX_TIMESTAMP(create_time) create_time from t_user
int(time.time() - create_time). 不转unix无法操作
3. 时间格式化问题
如果想让时间正常显示,就需要在写sql的时候给他做格式化
SELECT DATE_FORMAT(grant_end_time, '%%Y-%%m-%%d') grant_end_time from xxxx
覆盖旧数据的mysql插入
1、添加唯一索引需要保证该值只有一个
2、唯一索引需要放在最前面
3、更新的数据需要在list后面再添加一个来更新
from requests_html import HTMLSession
import pymysql
import json
import time
def mysql_db():
a = html_data()
#print(tuple1)
db = pymysql.connect(
host="localhost",
user="root",
passwd="zdl12345",
database = "runoob_db",
charset = "utf8"
)
cc = db.cursor()
#sql1 = "create table sites2(id int primary key not null auto_increment,rn varchar(255) ,nn varchar(255),ol varchar(255),c2name varchar(255))"
#cc.execute(sql1)
#sql3 = "alter table sites2 add unique (nn)"
#cc.execute(sql3)
#print("add success")
cc.execute("select * from sites2")
results = cc.fetchall()
#print(zb_list)
list = []
for i in target_data:
#print(i)
for k in i.keys():
#print(key)
if k == "c2name":
list = [i["nn"],i["rn"],str(i["ol"]),i["c2name"],str(i["ol"])]
tuple1 = tuple(list)
#print(tuple1)
sql = "insert into sites2(nn, rn, ol, c2name) values (%s, %s, %s, %s) on duplicate key update ol = %s"
val = tuple1
cc.execute(sql,val)
db.commit()
db.close()
print("insert success")
def html_data():
global target_data
session = HTMLSession()
url = "https://www.douyu.com/gapi/rkc/directory/1_1/1.html"
#headers = {'Accept-Charset': 'UTF-8'}
database = session.get(url)
data = database.html.text
#print(data)
jsondata = json.loads(data)
#print(jsondata)
target_data = jsondata["data"]["rl"]
#print(target_data[0])
#print(type(target_data))
#return target_data
#html_data()
#while True:
mysql_db()
time.sleep(1)
data2 = time.time()
s = data2 - data1
来源:https://www.cnblogs.com/yuanfang0903/p/10870627.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python项目报错:bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requests
![](https://img.aspxhome.com/file/2023/4/92114_0s.jpg)
JS判断元素是否在可视区域技巧详解
![](https://img.aspxhome.com/file/2023/8/136008_0s.jpg)
Python xlrd/xlwt 创建excel文件及常用操作
![](https://img.aspxhome.com/file/2023/5/115795_0s.png)
详解Python数据结构与算法中的顺序表
![](https://img.aspxhome.com/file/2023/2/115142_0s.png)
Python3.6实现根据电影名称(支持电视剧名称),获取下载链接的方法
![](https://img.aspxhome.com/file/2023/2/90752_0s.png)
python编程使用协程并发的优缺点
关于Python中字符串的各种操作
pandas创建series的三种方法小结
asp 存储过程分页代码第1/2页
![](https://img.aspxhome.com/file/UploadPic/20112/5/201125112228776s.jpg)
Python实现求最大公约数及判断素数的方法
常用的三种修改mysql最大连接数的方法
python-字典dict和集合set
Python之批量创建文件的实例讲解
简单的抓取淘宝图片的Python爬虫
关于401状态码的含义和处理方式
![](https://img.aspxhome.com/file/2023/7/132307_0s.png)
SqlServer 巧妙解决多条件组合查询
termux中matplotlib无法显示中文问题的解决方法
![](https://img.aspxhome.com/file/2023/6/128516_0s.png)
python 画函数曲线示例
![](https://img.aspxhome.com/file/2023/8/65108_0s.jpg)
python实现图片插入文字
![](https://img.aspxhome.com/file/2023/7/120417_0s.jpg)