From: Drew Fisher Date: Sun, 19 Jul 2009 07:28:57 +0000 (-0700) Subject: Add support for merging separate databases. X-Git-Url: http://git.zarvox.org/static/%7B%7B%20url_for%28%27main.login_page%27%29%20%7D%7D?a=commitdiff_plain;h=bfda711206cc881c80f0084441f0e027cfdf4bd1;p=wp3.git Add support for merging separate databases. Also refactor MainApp to import the entire QtCore namespace - I keep forgetting to specify, and I import all of QtCore everywhere else anyway. --- diff --git a/editperson.py b/editperson.py index 0387b19..fb62efb 100644 --- a/editperson.py +++ b/editperson.py @@ -50,7 +50,6 @@ class EditPerson(QWidget): self.resize(800,600) self.show() - def setupModel(self): self.model = QSqlTableModel(self, self.db) self.model.setEditStrategy(QSqlTableModel.OnManualSubmit) @@ -211,6 +210,8 @@ class EditPerson(QWidget): self.photo.setText("No photo available") self.pb_changephoto.setEnabled(True) + def reselect(self): + self.updateTable(self.search_bar.text()) def updateTable(self, text): if text.isEmpty(): self.model.setFilter("") diff --git a/mainapp.py b/mainapp.py index 14ee865..3b2d1ac 100644 --- a/mainapp.py +++ b/mainapp.py @@ -1,4 +1,4 @@ -from PyQt4 import QtCore +from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtSql import * @@ -34,16 +34,17 @@ class MainApp (QMainWindow): 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): @@ -66,7 +67,7 @@ class MainApp (QMainWindow): 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): @@ -76,12 +77,57 @@ class MainApp (QMainWindow): 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.