-from PyQt4 import QtCore
+from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *
def createActions(self):
self.fileNewAction = QAction("&New Database",self)
- QtCore.QObject.connect( self.fileNewAction, QtCore.SIGNAL("triggered()"), self.newFile)
+ QObject.connect( self.fileNewAction, SIGNAL("triggered()"), self.newFile)
self.fileOpenAction = QAction("&Open Database",self)
- QtCore.QObject.connect( self.fileOpenAction, QtCore.SIGNAL("triggered()"), self.openFile)
+ QObject.connect( self.fileOpenAction, SIGNAL("triggered()"), self.openFile)
self.fileImportAction = QAction("&Import (Merge)", self)
- QtCore.QObject.connect( self.fileImportAction, QtCore.SIGNAL("triggered()"), self.mergeWizard)
+ QObject.connect( self.fileImportAction, SIGNAL("triggered()"), self.mergeWizard)
self.fileQuitAction = QAction("&Quit",self)
- QtCore.QObject.connect( self.fileQuitAction, QtCore.SIGNAL("triggered()"), self.quit)
- QtCore.QObject.connect( self.chooseaction, QtCore.SIGNAL("editPerson()"), self.editPersonSlot)
- QtCore.QObject.connect( self.editperson, QtCore.SIGNAL("done()"), self.returnToMainMenu)
- QtCore.QObject.connect( self, QtCore.SIGNAL("databaseChanged(QString)"), self.editperson.updateDB)
+ QObject.connect( self.fileQuitAction, SIGNAL("triggered()"), self.quit)
+ QObject.connect( self.chooseaction, SIGNAL("editPerson()"), self.editPersonSlot)
+ QObject.connect( self.editperson, SIGNAL("done()"), self.returnToMainMenu)
+ QObject.connect( self, SIGNAL("databaseChanged(QString)"), self.editperson.updateDB)
+ QObject.connect( self, SIGNAL("mergeDone()"), self.editperson.reselect )
def createMenus(self):
q = QSqlQuery(self.db)
q.exec_(MainApp.schema)
self.statusBar().showMessage("Closed the existing database, created " + fileName)
- self.emit(QtCore.SIGNAL("databaseChanged(QString)"),self.db.databaseName())
+ self.emit(SIGNAL("databaseChanged(QString)"),self.db.databaseName())
else:
self.statusBar().showMessage("Cancelled creating new database")
def openFile(self):
self.db.setDatabaseName(fileName)
self.db.open()
self.statusBar().showMessage("Successfully loaded database " + fileName)
- self.emit(QtCore.SIGNAL("databaseChanged(QString)"),self.db.databaseName())
+ self.emit(SIGNAL("databaseChanged(QString)"),self.db.databaseName())
else:
self.statusBar().showMessage("Canceled loading database")
def mergeWizard(self):
fileName = QFileDialog.getOpenFileName(self, "Import which dataset?", ".", "Databases (*.db)" )
- print "and then we'd import data from",fileName
+ if not fileName.isEmpty():
+ tempdb = QSqlDatabase.addDatabase("QSQLITE","tempdb")
+ tempdb.setDatabaseName(fileName)
+ tempdb.open()
+ q = QSqlQuery(self.db)
+ import_q = QSqlQuery(tempdb)
+ import_q.exec_("SELECT * FROM people;")
+ rec = import_q.record()
+ col_id = rec.indexOf("id")
+ col_forename = rec.indexOf("forename")
+ col_surname = rec.indexOf("surname")
+ col_netid = rec.indexOf("netid")
+ col_email = rec.indexOf("email")
+ col_birthday = rec.indexOf("birthday")
+ col_phone = rec.indexOf("phone")
+ col_major = rec.indexOf("major")
+ col_dorm = rec.indexOf("dorm")
+ col_room = rec.indexOf("room")
+ col_photo = rec.indexOf("photo")
+ col_createtime = rec.indexOf("createtime")
+ col_mtime = rec.indexOf("mtime")
+ #print "Number of columns:",rec.count()
+ records_merged = 0
+ while import_q.next():
+ q.prepare("INSERT INTO people (netid, forename, surname, email, birthday, phone, major, dorm, room, createtime, mtime, photo )"
+ "VALUES (:netid, :forename, :surname, :email, :birthday, :phone, :major, :dorm, :room, :createtime, :mtime, :photo )" )
+ q.bindValue(":netid", import_q.value(col_netid) )
+ q.bindValue(":forename", import_q.value(col_forename) )
+ q.bindValue(":surname", import_q.value(col_surname) )
+ q.bindValue(":email", import_q.value(col_email) )
+ q.bindValue(":birthday", import_q.value(col_birthday) )
+ q.bindValue(":phone", import_q.value(col_phone) )
+ q.bindValue(":major", import_q.value(col_major) )
+ q.bindValue(":dorm", import_q.value(col_dorm) )
+ q.bindValue(":room", import_q.value(col_room) )
+ q.bindValue(":createtime", import_q.value(col_createtime) )
+ q.bindValue(":mtime", import_q.value(col_mtime) )
+ q.bindValue(":photo", import_q.value(col_photo) )
+ q.exec_()
+ print "Inserted", import_q.value(col_forename).toString(), import_q.value(col_surname).toString(), "into DB"
+ records_merged = records_merged + 1
+ self.statusBar().showMessage(QString("Successfully merged %1 records from %2").arg(QString(str(records_merged))).arg(fileName))
+ tempdb.close()
+ self.emit(SIGNAL("mergeDone()"))
+ else:
+ self.statusBar().showMessage("Canceled merging databases")
def quit(self):
# You could do cleanup, like closing/flushing the database, an "Are you sure you want to quit?"
# modal dialog, or saving the window layout/state.