def makeObjInsertStrings( obj, tplObjects = None, blnUseParens=True, blnGetAllAttrib=True ):
# Returns a 3 val tuple, the first two of which are strings which can be dropped into a MySQL Insert statement for (1) column names and (2) values
if not tplObjects:
return None, None, None
if isinstance( obj, tplObjects ): #find out what got passed - valid objects must be included in tuple tplObjects
strDblQuote = '"'
lstCols = list()
lstVals = list()
lstExcludedAttrib = list()
dctObj = vars( obj )
lstObjVarNames = dctObj.keys()
if blnGetAllAttrib:
tplValidTypes = ( types.BooleanType, types.FloatType, types.IntType, types.LongType, types.StringType, types.StringTypes, types.NoneType )
for varName in lstObjVarNames:
val = dctObj[ varName ]
if isinstance( val, tplValidTypes ):
lstCols.append( varName )
if val or val == 0:
lstVals.append( dctObj[ varName ] )
else:
lstVals.append('')
else:
lstExcludedAttrib.append( varName )
if blnUseParens:
strCols = joinListItems( lstCols )
strVals = joinListItems( lstVals )
else:
strCols = joinListItems( lstCols, blnUseParens=False )
strCols = joinListItems( lstVals, blnUseParens=False )
strCols = strCols.replace('"', '')
return strCols, strVals, lstExcludedAttrib
else:
print 'No object passed.'
return None, None, None
评论列表
文章目录