[Python-de] Formatierung von SQL-Statements
Mathias Uebel
mathias.uebel at meeloon.de
Don Jul 8 11:18:03 CEST 2004
Hartmut Goebel wrote:
> [...]
> Es ist besser, das _immer_ zu berücksichtigen. Danmit kann Du den Code
> auch ein andermal wiederverwenden, ohne eine Sicherheitslücke
> aufzureissen.
> - --
> Schönen Gruß - Regards
> Hartmut Goebel
Okay, richtig!
Ich habe jetzt das:
def _formatValue(self,value):
if value is None:
return "Null"
elif isinstance(value,str):
return "%s" % value.replace("'", "''")
elif isinstance(value, unicode):
return ("%s" %
value.replace("'","''")).encode("latin1")
elif isinstance(value,(int,long,float)):
return str(value)
else:
raise valueError("inrecognised type %s for database field" % value)
def SQLInsert( self, table, list ):
# SQL String
#list.insert(0,self.mytime)
tmp = [table,self.mytime]
for item in list:
item = self._formatValue(item)
tmp.append(item)
tpl = "INSERT INTO %s VALUES (NULL,'%s'" + (", '%s'"*len(list))
+ ")"
try:
self.cu.execute(tpl % tuple(tmp))
self.cx.commit()
Gibt es noch Verbesserung? Bin weitehin für Hilfe und Anregung dankbar!
Grusz Mathias
--
Lieber lachende Pinguine als tanzende Büroklammern!
CU in www.meeloon.de
--