Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #--------- define Interface
- ##PostgreSQL/PostGIS Tools=group
- ##Drop default value/sequence=name
- ##PostgreSQL_Table=vector
- ##Column=field PostgreSQL_Table
- from PyQt4.QtCore import *
- from PyQt4.QtGui import *
- from PyQt4 import *
- from qgis.core import *
- from qgis.gui import *
- from qgis.utils import *
- import psycopg2
- #get the parameters for the tpg table into a directory
- #get the table
- pg_table = processing.getObject(PostgreSQL_Table)
- #create empty dictionary for key/value pairs of the tables connection parameters
- db_params = {}
- db_params['clmn'] = Column
- #iterate over connection string
- progress.setInfo(20*'-' + ' Connection parameters')
- for param in pg_table.dataProvider().dataSourceUri().split(' '):
- key_val = param.split('=')
- progress.setInfo(str(key_val))
- try:
- #set key/value pair
- db_params[key_val[0]] = key_val[1]
- except:
- pass
- #generate the sql statement string
- #the text in round brackets are the keys from the db_params dictionary created above
- #the values belonging to the keys are inserted into the string
- progress.setInfo(20*'-' + ' SQL statement')
- sql = """ALTER TABLE %(table)s
- ALTER COLUMN %(clmn)s DROP DEFAULT;
- DROP SEQUENCE IF EXISTS %(table)s_%(clmn)s_seq;
- COMMIT;""" % db_params
- #remove double quotes
- sql = sql.replace('"','')
- progress.setInfo(sql)
- #make connection string
- constr = """dbname=%(dbname)s host=%(host)s port=%(port)s user=%(user)s password=%(password)s""" % db_params
- progress.setInfo(20*'-' + ' DB Connection string')
- progress.setInfo(constr)
- #make db connection
- con = psycopg2.connect(constr)
- cur = con.cursor()
- #execute the above created sql statement
- progress.setInfo(20*'-' + ' Executing SQL statement ...')
- cur.execute(sql)
- progress.setInfo(20*'-' + ' ... done.')
- SELECT column_default
- FROM information_schema.columns
- WHERE (table_schema, table_name, column_name) = ('/*schema*/', '/*table*/', '/*column*/');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement