python通過配置檔案連線mysql

2021-08-04 22:32:33 字數 2459 閱讀 9918

之前在python中連線mysql是直接在python檔案中寫的,這次把資料庫資訊放在單獨乙個配置檔案中

這是linux環境下

下面列出了select update insert 語句

#!/bin/env python

#-*-coding:utf-8-*-

import sys

import mysqldb

import json

import time

import os

cur_time="\""+time.strftime('%y-%m-%d %h:%m:%s',time.localtime(time.time()))+"\""

print cur_time

task_id = raw_input("input task_id:")

with open('conf/default.conf','r') as conffile:

confstr = conffile.read()

conf = json.jsondecoder().decode(confstr);

##########################################

#connect table result

dbstaticresult = 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_task

print 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"

conf/default.conf的配置檔案如下

,

"db_bim_rap_result_db":,

"db_bim_statistic_result_db":

}}

Spring AOP 通過配置檔案方式

1.寫乙個切面類 securitycontrol,其中有個方法 public class securitycontrol private void checksecurity joinpoint joinpoint object obj joinpoint.getargs for int i 0 i...

python配置檔案

將所有的 和配置都變成模組化可配置化,這樣就提高了 的重用性,不再每次都去修改 內部,這個就是我們逐步要做的事情,可配置化 coding utf 8 flask 配置資訊 在主程式中 from flask import flask flask name 方案一 物件名.congif 資料名 單獨配置...

python 配置檔案

配置檔案2 ini properties conf 等都是配置檔案3 section 片段 option 選項4 同乙個section下option都是唯一的56 語法7 secion 8 option values9 1011 例如 12 student1 13 name 小丫14 age 23 ...