1

I am building a plugin for QGIS which will have a combo box listing data from PostGreSQL. I have already connected PostGreSQL. Now I would want to make this a plugin and add the list of jobs to the combo box .

import psycopg2
from PyQt4.QtGui import*
from qgis.core import QgsMessageLog
msg = QMessageBox()
msg.setText("Message Box Created")
msg.show()
try:
connection = psycopg2.connect(user = "****",
                              password = "********",
                              host = "******",
                              port = "******",
                              database = "******")

cursor = connection.cursor()
# Print PostgreSQL Connection properties
print("connected")
#print ( connection.get_dsn_parameters(),"\n")

# Print PostgreSQL version
cursor.execute("SELECT version();")
record = cursor.fetchone()
print("You are connected to - ", record,"\n")
#creating table in database
'''create_table_query = CREATE TABLE TEST
      (ID INT PRIMARY KEY     NOT NULL,
      job_desc          TEXT    NOT NULL,
      username         REAL); 
cursor.execute(create_table_query)'''
connection.commit()
print("Table created successfully in PostgreSQL ")
except (Exception, psycopg2.Error) as error :
print ("Error while connecting to PostgreSQL", error)
finally:
#closing database connection.
    if(connection):
        cursor.close()
        connection.close()
        print("PostgreSQL connection is closed")
  • Could you clarify a bit? Where do you want this combobox? Is it a dialog that pops up after clicking a button on the toolbar? Is it linked to an attribute form? anyway, there is a plugin called pluginbuilder. I always start my plugins from there. – Lennert De Feyter Feb 5 at 7:21
  • Aside from if you really have to, begin to develop a plugin for QGIS 3, QGIS 2 and Python 2 are not longer maintained. – J. Monticolo Feb 5 at 8:23
  • @LennertDeFeyter It will be a combo box that pops up after clicking a button on the toolbar - namely the button will be connecting to my POSTGRES database and the combo box would show a list of jobs( eg job 1, job 2 etc) – SOHINI GOSWAMI Feb 5 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.