pymysql操作mysql的脚本示例
#!/usr/bin/env python
#-*- coding:UTF-8 -*-
from multiprocessing import Process , Queue
from queue import Queue,Empty as QueueEmpty
import subprocess
import os
import sys
import time
import datetime
import re
import pymysql
import logging
db_cdb20 = None
cursor_cdb20 = None
db_sdb30 = None
cursor_sdb30 = None
sql_dict = {}
center_ipaddr = None
center_http_port = None
C2_SERIES_TABLE = 'c2_series';
C2_PROGRAM_TABLE = 'c2_program';
C2_MAP_SERIES_PROGRAM_TABLE = 'c2_map_series_program'
C2_PRODUCT_TABLE = 'c2_product'
C2_MOVIE_TABLE = 'c2_movie'
C2_MOVIE_MAP_TABLE = 'c2_map_program_movie'
C2_PICTURE_TABLE = 'c2_picture'
C2_PICTURE_MAP_TABLE = 'c2_map_picture_program'
C2_SERIES_UNIMPORT_STATUS = 0;
C2_LIMIT = 100
CDB20_PRO_TABLE = 'program'
CDB20_PRO_SERIES_TABLE = 'programseries'
CDB20_MEDIA_TABLE = 'mediacontent'
CDB20_PRO_MEDIA_TABLE = 'programmediacontent'
CDB20_PIC_TABLE = 'metapicture'
CDB20_PIC_MAP_TABLE = 'picturemap'
CDB20_MEDIA_SERIEL_TYPE = '26'
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
filename='/var/log/sunlight/c2.log',
filemode='a'
);
def insert_opt(db,cursor,tablename,data_dict,lastinsertid=0):
marks = ','.join(['%s'] * len(data_dict))
fields = ','.join(data_dict.keys())
insert_values = [x if x else '' for x in list(data_dict.values())]
insert_sql = "INSERT INTO %s (%s) VALUES (%s)" % (tablename, fields, marks)
cursor.execute(insert_sql, insert_values)
db.commit()
logging.info("\n --sql-- %s \n --value-- %s \n --result-- %d " % (insert_sql, insert_values, cursor.rowcount))
return cursor.rowcount if not lastinsertid else cursor.lastrowid
def convert_c2_series(db_read,cursor_read,import_field,db_write,cursor_write):
global center_ipaddr
global center_http_port
query_c2_series = "SELECT ss.`code`, ss.`name`, ss.`sort_name` AS `titlesortname`, ss.`search_name` AS `titleserachname`, \
ss.`createtime` AS `createdate`, ss.`status`, ss.`description`, ss.`volumncount`, \
ss.`licensing_window_start` AS `licensingwindowstart`, ss.`licensing_window_end` AS `licensingwindowend`, ss.`labels` AS `tag`, ss.`rating`, \
sp.`language`, sp.`release_year` AS releaseyear, sp.`actor_display` AS `actorsdisplay`, sp.`writer_display` AS `writerdisplay`, \
sp.`original_country` AS `country`, sp.`genre` AS `genres`, st.`price` FROM c2_series AS ss \
LEFT JOIN (select * from c2_map_series_program group by seriescode) AS smp ON ss.`code` = smp.seriescode \
LEFT JOIN c2_program AS sp ON smp.programcode = sp.code \
LEFT JOIN c2_product AS st ON st.productid = ss.productid \
WHERE ss.`%s` = '0'" % import_field
logging.info("[ query_c2_series ] : %s" % query_c2_series)
cursor_read.execute(query_c2_series)
series_lists = cursor_read.fetchall()
if series_lists is not None:
for series_list in series_lists:
check_query = "SELECT `programid` FROM program WHERE name = '%s' " % series_list['name']
print(check_query)
logging.info("[ check_query ] : %s" % check_query)
cursor_write.execute(check_query)
get_query = cursor_write.fetchone()
if get_query :
logging.info("get_query programid: %s, skip..." % get_query['programid'])
continue
series_code = series_list.get('code',0)
del series_list['code']
series_list['series_flag'] = 1
series_list['lastupdatedate'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
series_list['rating'] = series_list['rating'] if series_list['rating'] else 0.0
programid = insert_opt(db_write,cursor_write,'program',series_list,1)
if programid:
logging.info("insert program success! [ series - code ]: %s , [ programid ] %s" % (series_code, programid))
#insert program table finished. 以上
query_c2_series_program = "SELECT sp.`name`, sp.`createtime`, sp.`status`,sp.`code`,sp.`productid`,mv.`duration`, smp.`sequence`, pic.`localurl`, pic.`status` AS `picstatus`, \
pic.`createtime` AS `piccreatetime` \
from c2_series AS ss \
LEFT JOIN c2_map_series_program AS smp ON ss.code = smp.seriescode \
LEFT JOIN c2_program AS sp ON smp.programcode = sp.code \
LEFT JOIN c2_map_program_movie AS mmp ON sp.code = mmp.programcode \
LEFT JOIN c2_movie AS mv ON mmp.moviecode = mv.code \
LEFT JOIN c2_map_picture_program AS picmap ON sp.code = picmap.programcode \
LEFT JOIN c2_picture AS pic ON picmap.picturecode = pic.code \
WHERE pic.status = 1 AND picmap.type = 2 AND ss.code = '%s'" % series_code
cursor_read.execute(query_c2_series_program)
query_seriesprogram_lists = cursor_read.fetchall()
if query_seriesprogram_lists:
for query_seriesprogram_list in query_seriesprogram_lists:
query_seriesprogram = "SELECT `programseriesid` FROM %s WHERE `name` = '%s'" % ('programseries', query_seriesprogram_list['name'] )
cursor_write.execute(query_seriesprogram)
if cursor_write.fetchone():
logging.info("query programseriesid from programseries is ok, skipping... [ name ] %s" % query_seriesprogram_list['name'])
continue
ps_lists = {}
ps_lists['name'] = query_seriesprogram_list['name'] if query_seriesprogram_list['name'] else ''
ps_lists['programid'] = programid
ps_lists['lastupdatetime'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%i:%s")
ps_lists['productid'] = query_seriesprogram_list['productid'] if query_seriesprogram_list['productid'] else ''
ps_lists['createtime'] = query_seriesprogram_list['createtime'] if query_seriesprogram_list['createtime'] else ''
ps_lists['sequence'] = query_seriesprogram_list['sequence'] if query_seriesprogram_list['sequence'] else ''
programseriesid = insert_opt(db_write,cursor_write,'programseries',ps_lists,1)
if programseriesid:
#beigin to insert media and content;
logging.info("insert programseries ok! [ programseriesid ] : %d" % programseriesid)
query_mediacontent_name = "SELECT `mediacontentid` FROM mediacontent WHERE `contentname` = '%s'" % query_seriesprogram_list['name']
cursor_write.execute(query_mediacontent_name)
if cursor_write.fetchone():
logging.info("query mediacontentid from mediacontent is ok, skipping... [ name ] %s" % query_seriesprogram_list['name'])
continue
mediacontent_list = {}
mediacontent_list['contentname'] = query_seriesprogram_list['name']
mediacontent_list['duration'] = query_seriesprogram_list['duration'] if query_seriesprogram_list['duration'] else ''
mediacontent_list['createdate'] = query_seriesprogram_list['createtime'] if query_seriesprogram_list['createtime'] else ''
mediacontent_list['status'] = query_seriesprogram_list['status'] if query_seriesprogram_list['status'] else '1'
mediacontent_list['lastupdatetime'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%i:%s")
mediacontent_list['extcontentid'] = query_seriesprogram_list['code'] if query_seriesprogram_list['code'] else ''
mediacontent_lastid = insert_opt(db_write,cursor_write,'mediacontent',mediacontent_list,1)
if mediacontent_lastid:
logging.info("insert mediacontent success! [ mediacontent_lastid ]: %d" % mediacontent_lastid)
programmediacontent_list = {}
programmediacontent_list['objecttype'] = CDB20_MEDIA_SERIEL_TYPE
programmediacontent_list['objectid'] = programseriesid
programmediacontent_list['mediacontentid'] = mediacontent_lastid
programmedia_rowcount = insert_opt(db_write,cursor_write,'programmediacontent',programmediacontent_list)
if programmedia_rowcount:
logging.info("insert programmediacontent success! [ program - name ] : %s" % query_seriesprogram_list['name'])
picture_list = {}
picture_list['picturename'] = query_seriesprogram_list['localurl'].replace("127.0.0.1", center_ipaddr + ":" + center_http_port)
picture_list['fileurl'] = query_seriesprogram_list['localurl'].replace("127.0.0.1", center_ipaddr + ":" + center_http_port)
picture_list['createdate'] = query_seriesprogram_list['piccreatetime']
picture_list['picturetype'] = 2 #缩略图
picture_lastid = insert_opt(db_write,cursor_write,'metapicture',picture_list,1)
if picture_lastid:
logging.info("inset metapicture success! [ name ]: %s" % query_seriesprogram_list['name'])
picturemap_list = {}
picturemap_list['metapictureid'] = picture_lastid
picturemap_list['sequence'] = 1
picturemap_list['objecttype'] = CDB20_MEDIA_SERIEL_TYPE
picturemap_list['objectid'] = programseriesid
picturemap_rowcount = insert_opt(db_write,cursor_write,'picturemap',picturemap_list,1)
if picturemap_rowcount:
logging.info("insert picturemap success! programname:%s" % query_seriesprogram_list['name'])
update_sql = "UPDATE %s SET %s = 1 WHERE name='%s'" % ('c2_program', import_field,query_seriesprogram_list['name'] )
cursor_read.execute(update_sql)
db_read.commit()
else:
logging.info("insert picturemap failed! programname:%s" % query_seriesprogram_list['name'])
else:
logging.info("inset metapicture failed! [ name ]: %s" % query_seriesprogram_list['name'])
else:
logging.info("insert programseries failed! [ programid ] : %d" % programid)
update_sql = "UPDATE %s SET %s = 1 WHERE code='%s'" % ('c2_series',import_field,series_code)
cursor_read.execute(update_sql)
db_read.commit()
else:
logging.info("insert program failed! [ series - code ]: %s , [ programid ] %s" % (series_code, programid))
def convert_c2_program(db_read,cursor_read,import_field,db_write,cursor_write):
global center_ipaddr
global center_http_port
query_vod_sql = "SELECT p.code,p.name, p.sort_name as titlesortname, search_name as titleserachname, pt.price, p.createtime as createdate, \
p.status,p.description,p.language,p.actor_display as actorsdisplay, p.writer_display as writerdisplay, \
p.licensing_window_start as licensingwindowstart, p.licensing_window_end as licensingwindowend , \
p.rating,p.genre as genres,p.labels as tag, p.release_year as releaseyear, p.original_country as country, \
v.duration, \
pic.localurl, pic.status as picstatus, pic.createtime as piccreatedate \
FROM c2_program AS p \
LEFT JOIN c2_product AS pt ON p.productid = pt.productid \
LEFT JOIN c2_map_program_movie AS vp ON p.code = vp.programcode \
LEFT JOIN c2_movie AS v ON vp.moviecode = v.code \
LEFT JOIN c2_map_picture_program AS pmp ON p.code = pmp.programcode \
LEFT JOIN c2_picture AS pic ON pmp.picturecode = pic.code \
WHERE pic.status = 1 AND pmp.type = 2 AND p.series_flag = '0' AND p.`%s` = '0'" % import_field
cursor_read.execute(query_vod_sql)
program_lists = cursor_read.fetchall()
if program_lists is not None:
for list in program_lists:
#将记录插入program表;
if list['name']:
query_sql = "SELECT programid FROM %s WHERE name = '%s'" % ('program', list['name'])
cursor_write.execute(query_sql)
if cursor_write.fetchone():
continue
program_list = {}
program_list['lastupdatedate'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
program_list['volumncount'] = 1
program_list['name'] = list['name'] if list['name'] else ''
program_list['titleserachname'] = list['titleserachname'] if list['titleserachname'] else ''
program_list['titlesortname'] = list['titlesortname'] if list['titlesortname'] else ''
program_list['price'] = list['price'] if list['price'] else ''
program_list['createdate'] = list['createdate'] if list['createdate'] else ''
program_list['status'] = list['status'] if list['status'] else ''
program_list['description'] = list['description'] if list['description'] else ''
program_list['releaseyear'] = list['releaseyear'] if list['releaseyear'] else ''
program_list['language'] = list['language'] if list['language'] else ''
program_list['actorsdisplay'] = list['actorsdisplay'] if list['actorsdisplay'] else ''
program_list['writerdisplay'] = list['writerdisplay'] if list['writerdisplay'] else ''
program_list['licensingwindowstart'] = list['licensingwindowstart'] if list['licensingwindowstart'] else ''
program_list['licensingwindowend'] = list['licensingwindowend'] if list['licensingwindowend'] else ''
program_list['isfree'] = 1 if list['price'] else ''
program_list['rating'] = list['rating'] if list['rating'] else ''
program_list['genres'] = list['genres'] if list['genres'] else ''
program_list['tag'] = list['tag'] if list['tag'] else ''
insert_program_lastid = insert_opt(db_write,cursor_write,'program',program_list,1)
if insert_program_lastid:
logging.info("insert program success! [ name ] : %s" % list['name'])
media_list = {}
media_list['duration'] = list['duration'] if list['duration'] else ''
media_list['contentname'] = list['name'] if list['name'] else ''
media_list['createdate'] = list['createdate'] if list['createdate'] else ''
media_list['extcontentid'] = list['code'] if list['code'] else ''
media_list['status'] = list['status'] if list['status'] else ''
media_list['lastupdatetime'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
insert_media_lastid = insert_opt(db_write,cursor_write,'mediacontent',media_list,1)
if insert_media_lastid:
programmedia_list = {}
programmedia_list['objecttype'] = 3
programmedia_list['objectid'] = insert_program_lastid
programmedia_list['mediacontentid'] = insert_media_lastid
insert_programmedia_lastid = insert_opt(db_write,cursor_write,'programmediacontent',programmedia_list)
if insert_programmedia_lastid:
logging.info("insert programmediacontent success! [ name ] : %s" % list['name'] )
else:
logging.info("insert programmediacontent failed! [ name ] : %s" % list['name'] )
picture_list = {}
picture_list['picturename'] = list['localurl'].replace("127.0.0.1", center_ipaddr + ":" + center_http_port)
picture_list['fileurl'] = list['localurl'].replace("127.0.0.1", center_ipaddr + ":" + center_http_port)
picture_list['createdate'] = list['piccreatedate'] if list['piccreatedate'] else ''
picture_list['picturetype'] = 2
insert_picture_lastid = insert_opt(db_write,cursor_write,'metapicture',picture_list,1)
if insert_picture_lastid:
picturemap_list = {}
picturemap_list['metapictureid'] = insert_picture_lastid
picturemap_list['sequence'] = 1
picturemap_list['objecttype'] = 3
picturemap_list['objectid'] = insert_program_lastid
insert_picture_lastid = insert_opt(db_write,cursor_write,'picturemap',picturemap_list)
logging.info("insert picturemap sucess! [ name ] : %s" % list['name'])
update_sql = "UPDATE %s SET %s = 1 WHERE code = '%s'" % ('c2_program', import_field, list['code'])
cursor_read.execute(update_sql)
db_read.commit()
if cursor_read.rowcount:
logging.info("update c2_program set %s = 1 success!" % import_field)
def main():
global sql_dict
global db_cdb20
global cursor_cdb20
global db_sdb30
global cursor_sdb30
global center_ipaddr
global center_http_port
if not os.path.isfile("/usr/local/sunlight/conf/server.conf"):
print ("error! db config file not found...");
sys.exit(1);
with open("/usr/local/sunlight/conf/server.conf", "r") as s:
for line in s:
if len(line) and ('=' in line):
(sql_key , sql_value) = line.strip().split('=')
sql_dict[sql_key] = sql_value
db_cdb20 = pymysql.connect( \
host=sql_dict['dbhost'], \
port=int(sql_dict['dbport']), \
user=sql_dict['dbuser'], \
password=sql_dict['dbpass'], \
db='cdb20', \
charset='utf8', \
cursorclass = pymysql.cursors.DictCursor \
)
cursor_cdb20 = db_cdb20.cursor()
db_sdb30 = pymysql.connect( \
host=sql_dict['dbhost'], \
port=int(sql_dict['dbport']), \
user=sql_dict['dbuser'], \
password=sql_dict['dbpass'], \
db='sdb30', \
charset='utf8', \
cursorclass = pymysql.cursors.DictCursor \
)
cursor_sdb30 = db_sdb30.cursor()
query_center_ipaddr = "SELECT `paramvalue` FROM systemparameters WHERE `paramname` = 'center_ipaddr'"
cursor_cdb20.execute(query_center_ipaddr)
query_center_ipaddr_result = cursor_cdb20.fetchone()
center_ipaddr = query_center_ipaddr_result['paramvalue'] if query_center_ipaddr_result['paramvalue'] else '127.0.0.1'
query_center_http_port = "SELECT `paramvalue` FROM systemparameters WHERE `paramname` = 'center_http_port'"
cursor_cdb20.execute(query_center_http_port)
query_center_http_port_result = cursor_cdb20.fetchone()
center_http_port = query_center_http_port_result['paramvalue'] if query_center_http_port_result['paramvalue'] else '80'
convert_c2_series(db_cdb20,cursor_cdb20,'import_cdb20',db_cdb20,cursor_cdb20)
logging.info("---------------------------------c2_series import cdb20 finished----------------------------")
convert_c2_program(db_cdb20,cursor_cdb20,'import_cdb20',db_cdb20,cursor_cdb20)
logging.info("---------------------------------c2_program import cdb20 finished----------------------------")
convert_c2_series(db_cdb20,cursor_cdb20,'import_sdb30',db_sdb30,cursor_sdb30)
logging.info("---------------------------------c2_series import sdb30 finished----------------------------")
convert_c2_program(db_cdb20,cursor_cdb20,'import_sdb30',db_sdb30,cursor_sdb30)
logging.info("---------------------------------c2_program import sdb30 finished----------------------------")
cursor_cdb20.close()
db_cdb20.close()
cursor_sdb30.close()
db_sdb30.close()
if __name__ == '__main__':
main()
pymysql操作mysql的脚本示例的更多相关文章
- pymysql操作mysql
一.使用PyMySQL操作mysql数据库 适用环境 python版本 >=2.6或3.3 mysql版本>=4.1 安装 可以使用pip安装也可以手动下载安装.使用pip安装,在命令行执 ...
- Python 3 进阶 —— 使用 PyMySQL 操作 MySQL
PyMySQL 是一个纯 Python 实现的 MySQL 客户端操作库,支持事务.存储过程.批量执行等. PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Py ...
- python使用pymysql操作mysql数据库
1.安装pymysql pip install pymysql 2.数据库查询示例 import pymysql # 连接database conn =pymysql.connect(user=' , ...
- python 3.6 +pyMysql 操作mysql数据库
版本信息:python:3.6 mysql:5.7 pyMysql:0.7.11 ########################################################### ...
- flask + pymysql操作Mysql数据库
安装flask-sqlalchemy.pymysql模块 pip install flask-sqlalchemy pymysql ### Flask-SQLAlchemy的介绍 1. ORM:Obj ...
- PyMySQL操作mysql数据库(py3必学)
一,安装PyMySQL Python是编程语言,MySQL是数据库,它们是两种不同的技术:要想使Python操作MySQL数据库需要使用驱动.这里选用PyMySQL驱动. 安装方式还是使用pip命令. ...
- pymysql操作mysql数据库
1.建库 import pymysql # 建库 try: conn=pymysql.connect( host='127.0.0.1', port=3306, user='root', passwd ...
- 用pymysql操作MySQL数据库
工具库安装 pip install pymysql 连接关闭数据库与增删改查操作 # 导入pymysql库 import pymysql # 打开数据库连接 # 参数1:数据库服务器所在的主机+端口号 ...
- 使用pymysql 操作MySQL数据库
安装 pip install pymysql 注:连接前要有可使用的账户及有权限.可操作的数据库 先来一个栗子: import pymysql # 连接database conn = pymysql. ...
随机推荐
- Tsinghua 2018 DSA PA2简要题解
反正没时间写,先把简要题解(嘴巴A题)都给他写了记录一下. upd:任务倒是完成了,我也自闭了. CST2018 2-1 Meteorites: 乘法版的石子合并,堆 + 高精度. 写起来有点烦貌似. ...
- 总结const、readonly、static三者的区别
const:静态常量,也称编译时常量(compile-time constants),属于类型级,通过类名直接访问,被所有对象共享! a.叫编译时常量的原因是它编译时会将其替换为所对应的值: b.静态 ...
- China MVP Community Connection 2017
在微软北京和小朋友们参加编程一小时活动
- springmvc核心技术
目录 异常处理 类型转换器 数据验证 文件上传与下载 拦截器 异常处理 Spring MVC中, 系统的DAO, Service, Controller层出现异常, 均通过throw Exceptio ...
- C# foreach 值类型及引用类型迭代变量改变的方式
C#中foreach不能改变迭代变量的值 然而此种说法只适用与值类型,更改值类型时会改变在栈上的内存分布 引用类型由于是引用地址的变更,不影响内存分布,所以能够在foreach中更改 至于引用类型中的 ...
- 运行go服务器后台挂起(不看调试信息)
cd程序目录,shell输入go run+项目,接着键盘组合输入CTRL+Z,后台挂起进程. 要结束服务器,则输入fg,查看被挂起的go服务器进程,接着键盘组合输入CTRL+C,中断服务器进程. 此外 ...
- 5 第一个Django第4部分(表单和通用视图)
上一节完成了视图编写,这一节为应用添加投票功能,也就是表单提交. 5.1编写一个简单的表单 5.2使用通用视图 5.3改良视图 5.1编写一个简单的表单 在网页设计中添加Form元素 polls/te ...
- Android 面试100问- 0序0
准备找android方面的工作,现收集面试题,打算收集100个并记录
- Apache配置对外访问默认路径(域名默认访问路径)
摘抄.同以下博主遇到同样问题,故此记录. 作者:ChasingdreamLY 原文:https://blog.csdn.net/qq_26591517/article/details/80414073 ...
- ueditor的用法
今天做了一下百度富文本编辑器,遇到了一些问题,现在来总结一下: (1)jQuery没有引用,解决方法:引用jQuery并且放在所有的js前面 (2)没有报错,但是样式显示不出来.解决方法:css引用的 ...