1번 글에서 conf의 모양을 봤다면 DB접속 정보라는 것을 알 수 있다.

DBInfo.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[DB] 
host= 1.1.1.1
user=ecycle
passwd=ecycle123123
db=testdb
charset=utf8
use_unicode=true
sql_insert =    insert into user
                (
                CONNTIME,
                UNO,
                UNAME,
                UEMAIL,
                UPASSWORD
                )
                VALUES(
                str_to_date('%s', '%%Y%%m%%d%%H%%i%%s'),
                '%s',
                '%s',
                '%s',
                '%s'
                )
                ;
 
cs

이 conf파일은 DB정보와 insert 구문을 가지고 있다.
이 파일을 이용해서 DB와 연동된 python 코드를 작성해보자

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
#-*- codig: utf-8 -*-
 
import MySQLdb
import sys
import time
import ConfigParser
import signal
from datetime import datetime
 
 
SHUTDOWN = False
 
def shutdown(sigNum , frame):
    global SHUTDOWN
    SHUTDOWN = True
    __LOG__.Trace("SIGNAL NUM:%s" %sigNum)
    return
 
signal.signal(signal.SIGTERM, shutdown)  # Sig Terminate: 15
signal.signal(signal.SIGINT, shutdown)    # Sig Inturrupt: 2
try:
    signal.signal(signal.SIGHUP, shutdown)  # Sig HangUp: 1
except Exception, e : pass
try:
    signal.signal(signal.SIGPIPE, shutdown) # Sig Broken Pipe: 13
except Exception, e : pass
 
def db_connPool(cfg):
    conn = None
    try:
        conn = MySQLdb.connect(
            host = cfg.get('DB''host'),
            user = cfg.get('DB''user'),
            passwd = cfg.get('DB''passwd'),
            db = cfg.get('DB''db'),
            charset = cfg.get('DB''charset'),
            use_unicode = cfg.getboolean('DB''use_unicode')
            )
    except Exception as e:
        __LOG__Trace("DB connection error : %s" %e)
 
 
    return conn
 
 
def db_insert(params,cfg):
    try:
        
        conn = db_connPool(cfg)
        __LOG__.Trace("DB connected")
 
        #cursor = None
 
        #transaction
    
        conn.autocommit = False
        cursor = conn.cursor()
        strSql = cfg.get('DB','sql_insert') % params
        cursor.execute(strSql)
        __LOG__.Trace("Inserted!")
 
    except Exception as e:
        conn.rollback()
        __LOG__Trace("insert Error : %s" % e)
 
    finally:
        conn.commit()
        if cursor:
            cursor.close()
        if conn:
            conn.close()
 
if __name__ == '__main__':
 
    if len(sys.argv) <5:
        __LOG__.Trace('not enough arguments')
        sys.exit()
    
    __LOG__.Trace("*****DB INSERT START*****")
 
    try:
 
        cfg = ConfigParser.ConfigParser()
        cfg.read("./DBInfo.conf")
        
        
        time = datetime.now().strftime('%Y%m%d%H%M%S')
        nparams = (
            time,
            sys.argv[1],
            sys.argv[2],
            sys.argv[3],
            sys.argv[4]
            
            )
        
        db_insert(nparams,cfg)
        
    except:
        __LOG__.Trace("insert error")
        sys.exit()
 
    __LOG__.Trace("*****complete*****")
 

cs

이 코드는 transaction도 구현하였다. 간단하게
signal은 추후에 정리해서 올리겠다.

+ Recent posts