Python ODBC Wrapper
Revision as of 18:19, 17 June 2008 by PeterHarding (talk | contribs)
ODBC with Python
DBTable Implementation
1 # 2 # Database wrapper class. 3 # 4 import odbc 5 import os 6 import traceback 7 8 class DBTable: 9 """ 10 Wrapper for database table 11 """ 12 FIELD_TYPE = 0 13 14 def __init__( self, oConnection, strTable): 15 self.oConnection = oConnection 16 self.strTable = strTable 17 18 oCursor = oConnection.cursor() 19 oCursor.execute( "SELECT * FROM %s" % strTable) 20 21 self.oFields = [ oField[0] for oField in oCursor.description] 22 self.oFieldDescription = dict( [ (oField[0], 23 oField[1:]) for oField in oCursor.description]) 24 25 def Select( self, strQuery): 26 """ 27 Select records from query 28 29 Takes either SQL of select statement or a dictionary containing 30 field names and values to find. 31 """ 32 self.oCursor = self.oConnection.cursor() 33 if type( strQuery) == type(""): 34 self.oCursor.execute( strQuery) 35 else: 36 # 37 # assume query is a dict 38 # 39 self.oCursor.execute( "SELECT * FROM %s WHERE %s" % (self.strTable, 40 self.DictToWhere( strQuery))) 41 42 def FetchOne( self): 43 """ 44 Get next row of results 45 Returns a dictionary holding field names and values. 46 """ 47 oRow = self.oCursor.fetchone() 48 if oRow: 49 """ 50 Build a dictionary to map field name->value 51 """ 52 return dict([(self.oFields[i], oRow[i]) for i in range(len(oRow))]) 53 else: 54 return None 55 56 def Insert( self, oDict): 57 """ 58 Insert a row in the database 59 Takes a dictionary holding field names and values. 60 """ 61 strFields = oDict.keys() 62 strValues = [] 63 for strField in strFields: 64 strValue = oDict[strField] 65 strType = self.oFieldDescription[strField][DBTable.FIELD_TYPE] 66 67 strValues.append( self.FormatField( strField, strValue)) 68 69 strSQL = "INSERT INTO %s ( %s) VALUES(%s);" % ( self.strTable, 70 ", ".join( strFields), 71 ", ".join( strValues)) 72 print strSQL 73 oCursor = self.oConnection.cursor() 74 oCursor.execute( strSQL) 75 self.oConnection.commit() 76 77 def Delete( self, oDict): 78 """ 79 Delete row based on dictionary contents 80 Takes a dictionary holding field names and values. 81 """ 82 strSQL = "DELETE FROM %s WHERE %s;" % ( self.strTable, self.DictToWhere( oDict)) 83 print strSQL 84 oCursor = self.oConnection.cursor() 85 oCursor.execute( strSQL) 86 self.oConnection.commit() 87 88 def DictToWhere( self, oDict): 89 """ 90 Convert dictionary to WHERE clause. 91 """ 92 strFields = oDict.keys() 93 strExpressions = [] 94 95 for strField in strFields: 96 strValue = oDict[strField] 97 strType = self.oFieldDescription[strField][DBTable.FIELD_TYPE] 98 99 strValue = self.FormatField( strField, strValue) 100 101 strExpressions.append( '%s = %s' % (strField, strValue)) 102 103 return " AND ".join( strExpressions) 104 105 def FormatField( self, strField, strValue): 106 """ 107 Format a field for an sql statement. 108 """ 109 strType = self.oFieldDescription[strField][DBTable.FIELD_TYPE] 110 if strType == 'STRING': 111 return "'%s'" % str(strValue).replace( "'", "''") 112 elif strType == 'NUMBER': 113 return '%d' % int( strValue) 114 else: 115 raise 'unknown field type %s' % strType 116