autorunfilldb.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python
  2. # $Id:$
  3. from PyQt4 import QtCore, QtGui
  4. import os, sys
  5. try:
  6. import sqlite3 as sqlite
  7. except:
  8. from pysqlite2 import dbapi2 as sqlite
  9. def createdbfiles(tablename):
  10. sqlfile = tablename + '.sql'
  11. infile = tablename + '.txt'
  12. outfile = tablename + '.db'
  13. #erase outfile if exists already :-)
  14. if os.path.exists(outfile):
  15. os.unlink(outfile)
  16. print "Erased old " + outfile
  17. f = open(sqlfile, 'r')
  18. createsql = f.readline()
  19. createsql.rstrip()
  20. toexec = f.readline()
  21. toexec.rstrip()
  22. print "Used to create : " + createsql
  23. print "Used to insert : " + toexec
  24. # connect will create the outfile again
  25. connection = sqlite.connect(outfile)
  26. cursor = connection.cursor()
  27. #creating the DB
  28. cursor.execute(createsql)
  29. #Filling the DB
  30. f = open(infile, 'r')
  31. headerline = f.readline()
  32. items = []
  33. for line in f:
  34. line = line.rstrip()
  35. items = line.split(';')
  36. if len(items) > 2:
  37. items[2] = items[2].decode('string_escape')
  38. # print items[2]
  39. cursor.execute(toexec, items )
  40. #cursor.execute('INSERT INTO software VALUES (null, "John Doe", "jdoe@jdoe.zz")')
  41. #name = "Luke Skywalker"
  42. #email ="use@the.force"
  43. #cursor.execute('INSERT INTO software VALUES (null, ?, ?)', (name, email))
  44. #cursor.lastrowid
  45. connection.commit()
  46. print "*********************************************************"
  47. ##retrieving data
  48. #cursor.execute('SELECT count(*) FROM autorun')
  49. #print cursor.fetchall()
  50. #cursor.execute('SELECT count(*) FROM autorun WHERE Media LIKE "Cart"')
  51. #print cursor.fetchall()
  52. #cursor.execute('SELECT count(*) FROM autorun WHERE Media LIKE "Disk"')
  53. #print cursor.fetchall()
  54. #print
  55. #print "*********************************************************"
  56. #print
  57. #cursor.execute('SELECT * FROM autorun ')
  58. #print '-'*10
  59. #for row in cursor:
  60. # print row
  61. #print '-'*10
  62. createdbfiles('autorun')
  63. createdbfiles('softdb')
  64. createdbfiles('hwimages')