-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb_operation.py
More file actions
206 lines (170 loc) · 5.71 KB
/
Copy pathdb_operation.py
File metadata and controls
206 lines (170 loc) · 5.71 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
import time
import random
"""
Configure settings
"""
DB_IP=
DB_NAME=
DB_VM_NAME=
DB_USER=
DB_PASS =
"""
connect to database
Parameters:
db_ip, ip address of database
db_user, username of database
db_pass, password of database
db_name, name of database
Return:
a handle of the connected database if success
or None
"""
def connect_to_db(db_ip,db_user,db_pass,db_name):
try:
db = MySQLdb.connect(db_ip,db_user,db_pass,db_name)
db.set_character_set('utf8')
if(db == None):
return None
return db
except Exception as e:
print e
return None
"""
Connect to database with default setting
Parameters:
Return:
a handle of the connected database if success
or None
"""
def connect_to_db_simple():
db = None
count = 5
while True:
count = count - 1
db = connect_to_db(DB_IP,DB_USER,DB_PASS,DB_NAME)
if db is not None or count < 0:
break
random.seed()
time.sleep(random.randint(1, 5))
return db
#=============================================================#
#select one field value from table
#@para db, database pointer
#@para table_name, table name
#@para field_name, the name of indexed field,
# value is static_dot_uri or static_path_image_uri
#@para r_id, index a record
#@return value of indexed field
#=============================================================#
def select_field_from_table(db,table_name,field_name,r_id):
cursor = db.cursor()
sql="select %s from %s where id = %s;"%(field_name,table_name,r_id)
try:
cursor.execute(sql)
result=cursor.fetchall()
result=str(result[0])
result=result.split('(')[1].split(',')[0]
except Exception,e:
print "error to task table:",e
return ''
return result
def select_field_by_vmip(db,table_name,field_name,ip):
cursor=db.cursor()
sql="select %s from %s where vm_ip = '%s';"%(field_name,table_name,ip)
print sql
try:
cursor.execute(sql)
result=cursor.fetchall()
if(len(result)>0):
result=str(result[0])
result=result.split('(')[1].split(',')[0]
else:
result=''
except Exception,e:
print "error to task table:",e
return ''
return result
#=========================================================#
#insert a record with field=value
#@para db, db handle
#@para field, field name
#@para value, field value
#=========================================================#
def insert_into_db(db,table_name,field,value):
cursor = db.cursor()
sql="insert into %s (%s) values ('%s');"%(table_name,field,value)
#print sql
try:
cursor.execute(sql)
except Exception,e:
print 'some exception has happened when insert into db',e
return
#========================================================#
#update a record with field=value
#@para db, db handle
#@para table_name, table name
#@para field, field name
#@para value, field value
#@para r_id, index of record
#========================================================#
def update_one_record(db,table_name,field,value,r_id):
cursor=db.cursor()
sql="update %s set %s = '%s' where id = %s;"%(table_name,field,value,r_id)
#print sql
try:
cursor.execute(sql)
except Exception,e:
print 'some exception has happened when update db',e
return
#==========================================================#
#update table 'vm' by ip address
#==========================================================#
def update_one_vm(db,table_name,field,value,ip):
cursor=db.cursor()
sql="update %s set %s = '%s' where vm_ip = '%s';"%(table_name,field,value,ip)
#print sql
try:
cursor.execute(sql)
except Exception,e:
print 'some exception has happened when update db',e
return
#===========================================================#
#update a record in task table with new values of dot&svg uris
#@para db, handle of db
#@para r_id, index of a record
#@para dot_uri, uri of static call graph dot file
#@para svg_uri, uri of static call svg file
#===========================================================#
def update_one_record_simple(db,r_id,dot_uri,svg_uri):
cursor=db.cursor()
sql="update %s set %s = '%s', %s = '%s' where id = %s;"%(TABLE_TASK_NAME,FN_S_DOT_URI,dot_uri,FN_S_SVG_URI,svg_uri,r_id)
#print sql
try:
cursor.execute(sql)
except Exception,e:
print 'some exception has happened when update db',e
return
#============================================================#
#db=connect_to_db(DB_IP,DB_USER,DB_PASS,DB_NAME)
#
#if(db == None):
# print 'connect to db error'
#else:
# result=select_field_from_table(db,TABLE_TASK_NAME,"static_dot_uri",338)
# #print select_field_from_table(db,TABLE_TASK_NAME,"")
# print result
# db.close()
#
#==================================================================#
#db=connect_to_db(DB_IP,DB_USER,DB_PASS,DB_NAME)
#if (db == None):
# print 'failed to connect to db'
#else:
# #insert_into_db(db,'dynamic_dot_uri','20bd793e141d9b5967f1061f483b27eb')
# #update_one_record(db,'dynamic_dot_uri','44ffbad6fe4adbb347a67fa87ffafab9',1)
# update_one_record_simple(db,338,'30f714a740a51560f0fda4513eedfa23','601197cff878760874ea019cea30e4a1')
# db.close()
#==================================================================#