python通过配置文件连接mysql
来源:互联网 发布:2017科幻电影推荐知乎 编辑:程序博客网 时间:2024/06/06 16:58
之前在python中连接mysql是直接在python文件中写的,这次把数据库信息放在单独一个配置文件中
这是linux环境下
下面列出了select update insert 语句
#!/bin/env python#-*-coding:utf-8-*-import sysimport MySQLdb import jsonimport timeimport oscur_time="\""+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+"\""print cur_timetask_id = raw_input("input task_id:")with open('conf/default.conf','r') as confFile: confStr = confFile.read()conf = json.JSONDecoder().decode(confStr);###########################################connect table resultdbStaticResult = conf['database']['db_bim_rap_result_db'];conn = MySQLdb.connect(host=dbStaticResult['host'],\ user=dbStaticResult['user'],\ passwd=dbStaticResult['password'],\ db=dbStaticResult['database'],\ port=dbStaticResult['port']);cur = conn.cursor();#rap_query_sequence_random_url = sys.stdin.readline().strip()#insertion = 'insert into `user-portrait`(`task_id`, `user-portrait_file_url`, `insert_datetime`, `update_datetime` ) values ("' + sys.argv[1] + '", "' + rap_query_sequence_random_url + '",' + cur_time + ' ,'+ cur_time + ' )'selection = 'select * from `user-portrait` where task_id =' + task_id try: cur.execute(selection) rows = cur.fetchall() if (rows.__len__() != 0): print rows[0][4] # is_deleted update_result = 'update `user-portrait` set is_deleted = 1 where task_id =' + task_id cur.execute(update_result)except: print "exception on execute sql"conn.commit()conn.close();########################################### mv task_id task_id_bak# 进入指定目录下修改文件名,用shell命令执行失败old_task = "task_" + task_id new_task = old_task + "_bak"mv_command = "mv " + old_task + " " + new_taskprint mv_command#os.system('cd /home/work/local/job/sf/strategy/') # 用单引号或者双引号都可以#os.system('pwd')os.chdir('/home/work/local/job/sf/strategy/')print os.getcwd()if (os.path.exists(old_task)): print "file exist and start rename" os.system(mv_command)print "all process done"
{ "database":{ "task":{ "host":"st01-nsbk-201106-zf-28.st01.****.com", "port":3306, "database":"bim_statistic_strategy_db", "user":"*****", "password":"******" }, "db_bim_rap_result_db":{ "host":"st01-nsbk-201106-zf-28.st01.****.com", "port":3306, "database":"bim_rap_result_db", "user":"****", "password":"******" }, "db_bim_statistic_result_db":{ "host":"st01-nsbk-201106-zf-28.st01.****.com", "port":3306, "database":"bim_statistic_result_db", "user":"****", "password":"******" } }}
阅读全文
0 0
- python通过配置文件连接mysql
- Java连接Mysql:通过配置文件
- Java通过配置文件连接Mysql和Oracle数据库
- 通过修改配置文件设置mysql的最大连接数
- Java通过配置文件连接Mysql和Oracle数据库实例详解
- hibernate连接mysql配置文件
- hibernate连接mysql配置文件
- hibernate连接mysql配置文件
- Hibernate配置文件连接Mysql
- hibernate连接mysql配置文件
- 通过properties配置文件连接数据库
- 通过读取配置文件数据库连接
- jdbc通过配置文件连接数据库
- 通过properties配置文件连接数据库
- java通过配置文件连接数据库
- 通过python的obbc模块连接mysql数据库
- python 使用mysqldb模块通过ssh隧道连接mysql
- python 使用sqlalchemy,mysqldb模块通过ssh隧道连接mysql
- 集合List中消除重复值与取得两个List中的交、并、差集
- python重点语法总结(二)----@操作符
- Unity3D脚本依赖RequireComponent(自动添加关联的脚本)
- Mysql主从配置,读写分离
- dbf文件的解析方法
- python通过配置文件连接mysql
- Parallel.ForEach
- MYSQL --Subquery returns more than 1 row查询结果多于一行
- java学习知识积累-Mybatis中Mapper内置方法细解
- ov7725鹰眼
- Spring AOP浅谈及使用注解方法测试
- ZOJ1610 Count the Colors(线段树+思路)
- 高德,百度,Google地图定位偏移以及坐标系转换
- Promise简单实现