55# to you under the Apache License, Version 2.0 (the
66# "License"); you may not use this file except in compliance
77# with the License. You may obtain a copy of the License at
8- #
8+ #
99# http://www.apache.org/licenses/LICENSE-2.0
10- #
10+ #
1111# Unless required by applicable law or agreed to in writing,
1212# software distributed under the License is distributed on an
1313# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
2424import sys
2525import os
2626
27+
2728class dbConnection (object ):
28- def __init__ (self , host = "localhost" , port = 3306 , user = 'cloud' , passwd = 'cloud' , db = 'cloud' ):
29+ def __init__ (self , host = "localhost" , port = 3306 , user = 'cloud' ,
30+ passwd = 'cloud' , db = 'cloud' ):
2931 self .host = host
3032 self .port = port
31- self .user = str (user ) # Workaround: http://bugs.mysql.com/?id=67306
33+ self .user = str (user ) # Workaround: http://bugs.mysql.com/?id=67306
3234 self .passwd = passwd
3335 self .database = db
34-
36+
3537 def execute (self , sql = None , params = None ):
3638 if sql is None :
3739 return None
3840
3941 resultRow = []
40- with contextlib .closing (mysql .connector .connect (host = str (self .host ),
41- port = int (self .port ),
42- user = str (self .user ),
43- password = str (self .passwd ),
44- db = str (self .database ))) as conn :
42+ with contextlib .\
43+ closing (mysql .connector .connect (host = str (self .host ),
44+ port = int (self .port ),
45+ user = str (self .user ),
46+ password = str (self .passwd ),
47+ db = str (self .database ))) as conn :
4548 conn .autocommit = True
4649 with contextlib .closing (conn .cursor (buffered = True )) as cursor :
4750 cursor .execute (sql , params )
@@ -54,19 +57,21 @@ def execute(self, sql=None, params=None):
5457
5558 def executeSqlFromFile (self , fileName = None ):
5659 if fileName is None :
57- raise cloudstackException .InvalidParameterException ("file can't not none" )
58-
60+ raise cloudstackException .\
61+ InvalidParameterException ("file can't not none" )
62+
5963 if not os .path .exists (fileName ):
60- raise cloudstackException .InvalidParameterException ("%s not exists" % fileName )
61-
64+ raise cloudstackException .\
65+ InvalidParameterException ("%s not exists" % fileName )
66+
6267 sqls = open (fileName , "r" ).read ()
6368 return self .execute (sqls )
64-
69+
6570if __name__ == "__main__" :
6671 db = dbConnection ()
6772 '''
6873 try:
69-
74+
7075 result = db.executeSqlFromFile("/tmp/server-setup.sql")
7176 if result is not None:
7277 for r in result:
@@ -76,6 +81,6 @@ def executeSqlFromFile(self, fileName=None):
7681 '''
7782 print db .execute ("update vm_template set name='fjkd' where id=200" )
7883 for i in range (10 ):
79- result = db .execute ("select job_status, created, last_updated from async_job where id=%d" % i )
84+ result = db .execute ("select job_status, created, \
85+ last_updated from async_job where id=%d" % i )
8086 print result
81-
0 commit comments