Reputation: 261
I'm a newbie to Python and PyQt. I'd like to know how to connect to Postgresql DB using PyQt and display it to a grid window. I'm using Qt Designer. Can someone help me on this? Thank you.
Best Regards, Nethan
Upvotes: 1
Views: 5600
Reputation: 4022
PyQt has database support (which I personally haven't used, so I can't comment on), but should be pretty straightforward documentation if you look at the QDatabase. If your application's api can always have access to Qt, this may be the best approach as they also have some additional models for mapping to the interface.
Another alternative is going with a Python ORM (object relational mapper) such as Django, SQLAlchemy or storm and to define your table's (models) and manually load them into your designer interface.
The way I personally do it, is I've actually built up my own ORM called ORB and a PyQt extension library called ProjexUI. The ORB library is Qt independent so it can be used in non-Qt projects, and the ProjexUI library contains mappings to help use database records in Qt widgets.
For documentation and more information, check out: http://www.projexsoftware.com
For a simple example, create a new UI file by doing:
This creates the PyQt interface portion, next you'll still need to connect the interface to a database backend. The simplest example of how to do this with ORB is:
# import the projexui and orb libraries
import projexui
import orb
# import PyQt modules
import PyQt4.QtGui
import PyQt4.uic
# create our database model
class User(orb.Table):
__db_columns__ = [
orb.Column( orb.ColumnType.String, 'username' ),
orb.Column( orb.ColumnType.String, 'password' ),
orb.Column( orb.ColumnType.Boolean, 'isActive' )
]
# the above model will by default create a PostgreSQL table called default_user with
# the fields _id, username, password and is_active. All of this is configurable, but
# you should read the docs for more info
# create the database information
db = orb.Database('Postgres', DATABASE_NAME) # set your db name
db.setUsername(USER_NAME) # set your db user name
db.setPassword(PASSWORD) # set your password
db.setHost('localhost') # set your host
db.setPort(5432) # set your port
# register the database
orb.Orb.instance().registerDatabase(db)
# sync the datbase (this will create your tables, update columns, etc.)
# NOTE: this should not always be called, it is here as an example
db.sync( dryRun = False ) # dryRun will just output the SQL calls
#-----------------------------
# End Database Code
#-----------------------------
class ExampleDialog(QtGui.QDialog):
def __init__( self, parent = None ):
super(ExampleDialog, self).__init__(parent)
# load your UI file
PyQt4.uic.loadUi(UI_FILE, self) # use the UI_FILE you saved
# connect the tree to the model
self.uiOrbTREE.setTableType(User)
# that is all you have to do to link the tree to the model and vice-versa,
# this is the most simple example of how to do this - you can do more as far
# as linking queries and sorting and such, but you should read the docs on
# the site
# launch as an application
if ( __name__ == '__main__' ):
# always check for an existing application first!
app = None
if ( not QtGui.QApplication.instance() ):
app = QtGui.QApplication(sys.argv)
dialog = ExampleDialog()
dialog.show()
# execute the app if we created it
if ( app ):
app.exec_()
Upvotes: 3