[Python-de] Formatierung von SQL-Statements
Fritz Cizmarov
fritz at sol.at
Mit Jul 7 13:44:09 CEST 2004
Am Wed, 07 Jul 2004 12:28:48 +0200
Schrieb Mathias Uebel <mathias.uebel at meeloon.de>:
> Hallo Liste,
> ich brauche Hilfe zur Formatierung von SQL-Statements.
> Ich möchte eine Klasse kreieren, die dynamisch Abfragen in SQLite
> übernimmt. Wenn die Werte in ihrer Anzahl bekannt sind, geht das gut:
>
> def SQLSelFrom( self, table, key=None, value=None ):
> [...]
> try:
> self.cu.execute("SELECT*FROM %s WHERE %s = %s " %
> (table,key,value))
> self.cx.commit()
> [...]
>
> Aber wenn ich eine Liste übergebe, die je nach Tabelle unterschiedlich
>
> lang ist, habe ich mit der Formatierung Schwierigkeiten!
>
> def SQLInsert( self, list ):
> try:
> self.cu.execute("INSERT INTO %s VALUES ((SELECT max(id)
> FROM
> Buchung)+1,%s,%s)" %(table,self.mytime,list))
> self.cx.commit()
> [...]
> Hat jemand eine Idee oder eine Webseite als Tip?
>
> Grusz Mathias
>
Hi Mathias,
def SQLInsert( self, liste ):
tpl = "INSERT INTO %s VALUES ((SELECT max(id) FROM
> Buchung)+1,%s" + ", %s"*len(liste)
try:
self.cu.execute(tpl % ((table, self.mytime)+tuple(liste))
self.cx.commit()
Gruß
Fritz