#-*- coding:utf-8 -*-
import sys
import mysql.connector
import chardet
import datetime
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
collection_status =0
created_time = datetime.datetime.now().strftime("%y-%m-%d %h:%m:%s")
#檢視是否插入主題事件的表中
def testinsertintotable():
print '開始'
db = mysql.connector.connect(host='182.00.00.00', port='3306', user='root', password='*****',
database='university_event_analyse', use_unicode=true)
cursor = db.cursor()
url =''
theme ='有沒有今年被石大錄取的研究生呀?'
main_view ='有的話認識一下唄'
follow_count =34
post_type = '校園環境'
created_time =datetime.datetime.now().strftime("%y-%m-%d %h:%m:%s")
#print type(created_time)
source ='貼吧'
post_time ='2017-05-01 19:21'
sql = "insert into daily_event(url,theme, main_view, follow_count, \
post_type,created_time,source,collection_status,post_time) \
values ('%s','%s', '%s', '%d', '%s', '%s', '%s','%d','%s')" % \
(url,theme, main_view, follow_count,post_type,created_time,source,collection_status,post_time)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
#testinsertintotable()
#測試把文字資料插入資料庫
def testtextinsertintotable():
print '開始'
db = mysql.connector.connect(host='182.00.00.00', port='3306', user='root', password='*****',
database='university_event_analyse', use_unicode=true)
cursor = db.cursor()
fr = codecs.open(r'd:\pythonfiles\teiba\new\finallydata\testdata\20170614.event', 'rb', 'utf-8')
r_data = fr.readlines()
for line in r_data:
if line.strip() != '':
line_list = line.strip().split('\001')
url = line_list[6]
theme = line_list[0]
main_view = line_list[1]
follow_count = line_list[2]
post_type = line_list[3]
source = line_list[5]
post_time = line_list[4]
#print url
sql_query = "select * from daily_event \
where url = '%s'" % url
sql = "insert into daily_event(url,theme, main_view, follow_count, \
post_type,created_time,source,collection_status,post_time) \
values ('%s','%s', '%s', '%d', '%s', '%s', '%s','%d','%s')" % \
(url, theme, main_view, int(follow_count), post_type, created_time, source, collection_status, post_time)
sql_update = "update daily_event set theme= '%s' , main_view = '%s', follow_count = '%d',post_type = '%s', \
created_time ='%s', source = '%s',collection_status = '%d',post_time = '%s'" % (theme, main_view, int(follow_count), post_type, created_time, source, collection_status,post_time)+"where url = '%s'" % url
try:
#cursor.execute(sql)
cursor.execute(sql_query)
results = cursor.fetchall()
if len(results):
# print len(results)
# print results
# print results[0][1]
cursor.execute(sql_update)
db.commit()
else:
#print results[0][1]
print 'nida'
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
testtextinsertintotable()
mysql插入或更新
現有user表,userid為使用者id,做為資料表user的主鍵 由於userid不可以重複,而這裡userid直接作為主鍵。為防止併發操作,插入語句可以這樣設計 不存在userid則插入,否則更新 insert into user userid,nickname,role,createtime,...
MySQL插入更新 replace into
建表 create table keywords type varchar 255 not null comment 分類 value bigint 11 unsigned default null comment 資料量 primary key type name engine innodb de...
Mysql批量插入更新
批量插入,使用mybatis批量語法 insert id insertbatch parametertype list insert into tbl1 a,b,c values foreach collection list item obj separator foreach insert 使用...