mysql數(shù)據(jù)插入覆蓋和時(shí)間戳的問(wèn)題及解決
數(shù)據(jù)插入覆蓋和時(shí)間戳問(wèn)題
1.數(shù)據(jù)插入問(wèn)題
當(dāng)我們insert數(shù)據(jù)時(shí),插入datetime類型,此類型的字段,存儲(chǔ)數(shù)據(jù)格式為: YYYY-MM-DD,它支持的范圍為'1000-01-01'到'9999-12-31',并且允許使用字符串或數(shù)字為此列復(fù)制。
日期賦值時(shí),允許“不嚴(yán)格”語(yǔ)法:任何標(biāo)點(diǎn)符都可以用做日期部分或時(shí)間部分之間的間割符。例如,'98-12-31 11:30:45'、'98.12.31 11+30+45'、'98/12/31 11*30*45'和'98@12@31 11^30^45'是等價(jià)的,對(duì)于不合法的將會(huì)轉(zhuǎn)換為:0000-00-00 00:00:00
我們傳的時(shí)候 insert into test(count_data) values (20190922). 不要把20190922轉(zhuǎn)為2019-09-22
使用ignore關(guān)鍵字,避免重復(fù)插入記錄可以使用:(主健會(huì)變化)
insert ignore into student_task_trace (student_id) VALUES (20)
使用Replace,如果舊記錄與新記錄有相同的值,則在新記錄被插入之前,舊記錄被刪除,存入新紀(jì)錄:
REPLACE INTO student_task_trace (student_id) VALUES (20)
如果有主鍵的情況下,可以通過(guò)惟一索引來(lái)防止重復(fù)數(shù)據(jù)的插入
數(shù)據(jù)插入一個(gè)事件的時(shí)候 有好幾個(gè)不同的值 總會(huì)覆蓋 ,所以需要給不同值的字段設(shè)置個(gè)唯一索引。
除非清空(truncate 表名)
2. 時(shí)間加減問(wèn)題
如果想做時(shí)間加減的話 必須把時(shí)間變成unix時(shí)間戳 如當(dāng)前時(shí)間—創(chuàng)建時(shí)間
SELECT user_id, device, UNIX_TIMESTAMP(create_time) create_time from t_user
int(time.time() - create_time). 不轉(zhuǎn)unix無(wú)法操作
3. 時(shí)間格式化問(wèn)題
如果想讓時(shí)間正常顯示,就需要在寫sql的時(shí)候給他做格式化
SELECT DATE_FORMAT(grant_end_time, '%%Y-%%m-%%d') grant_end_time from xxxx

覆蓋舊數(shù)據(jù)的mysql插入
1、添加唯一索引需要保證該值只有一個(gè)
2、唯一索引需要放在最前面
3、更新的數(shù)據(jù)需要在list后面再添加一個(gè)來(lái)更新
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
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
MySQL主庫(kù)binlog(master-log)與從庫(kù)relay-log關(guān)系代碼詳解
這篇文章主要介紹了MySQL主庫(kù)binlog與從庫(kù)relay-log關(guān)系的相關(guān)內(nèi)容,涉及部分代碼,需要的朋友可以參考。2017-10-10
MySQL中參數(shù)sql_safe_updates在生產(chǎn)環(huán)境的使用詳解
這篇文章主要給大家介紹了關(guān)于MySQL中參數(shù)sql_safe_updates在生產(chǎn)環(huán)境使用的相關(guān)資料,并給大家分享了解決mysql sql_safe_updates不支持子查詢更新的方法,分享出來(lái)供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-11-11
MySQL登錄時(shí)出現(xiàn)ERROR 1045: Access denied for&
本文已解決MySQL登錄時(shí)出現(xiàn)Access denied for user ‘root‘@‘localhost‘ (using password: YES)無(wú)法打開(kāi)的相關(guān)報(bào)錯(cuò)問(wèn)題,并總結(jié)提出了幾種可用解決方案,又遇到同樣問(wèn)題的朋友可以參考閱讀下本文2024-09-09

