Kasutliidesed lab 2 salvestamise cgi

Allikas: Lambda
#!/usr/bin/python
import cgi
import sqlite
#from pysqlite2 import dbapi2 as sqlite
print 'Content-type: text/plain'
print
try:
  form = cgi.FieldStorage()
  query="""insert into materjalid """
  flds=""
  vals=""
  fields=('laadija','ylikool','teaduskond','ainekood','ainenimetus',
          'oppejounimi','materjalikeel','materjalityyp','materjalinimi','lyhikirjeldus')
  for el in fields:
    if form.has_key(el):
      flds=flds+" "+el+","
      vals=vals+" '"+form[el].value+"',"  
  if not flds:
    print 0
  else:    
    if flds[len(flds)-1]==",":
      flds=flds[0:len(flds)-1]
    if vals[len(vals)-1]==",":
      vals=vals[0:len(vals)-1]    
    query=query+"("+flds+") values ("+vals+")"   
    print query
    #con = sqlite.connect('/var/www/cgi-bin/tammet/materjalid.db')
    con = sqlite.connect('/tmp/tammet/materjalid.db')
    #con = sqlite.connect('materjalid.db')
    cur = con.cursor()
    cur.execute(query)
    con.commit()
    print "1"
except:
  print "0"