Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #--------- define Interface
  5. ##PostgreSQL/PostGIS Tools=group
  6. ##Drop default value/sequence=name
  7. ##PostgreSQL_Table=vector
  8. ##Column=field PostgreSQL_Table
  9.  
  10. from PyQt4.QtCore import *
  11. from PyQt4.QtGui import *
  12. from PyQt4 import *
  13.  
  14. from qgis.core import *
  15. from qgis.gui import *
  16. from qgis.utils import *
  17.  
  18. import psycopg2
  19.  
  20. #get the parameters for the tpg table into a directory
  21. #get the table
  22. pg_table = processing.getObject(PostgreSQL_Table)
  23. #create empty dictionary for key/value pairs of the tables connection parameters
  24. db_params = {}
  25. db_params['clmn'] = Column
  26. #iterate over connection string
  27. progress.setInfo(20*'-' + ' Connection parameters')
  28. for param in pg_table.dataProvider().dataSourceUri().split(' '):
  29. key_val = param.split('=')
  30. progress.setInfo(str(key_val))
  31. try:
  32. #set key/value pair
  33. db_params[key_val[0]] = key_val[1]
  34. except:
  35. pass
  36.  
  37. #generate the sql statement string
  38. #the text in round brackets are the keys from the db_params dictionary created above
  39. #the values belonging to the keys are inserted into the string
  40. progress.setInfo(20*'-' + ' SQL statement')
  41. sql = """ALTER TABLE %(table)s
  42. ALTER COLUMN %(clmn)s DROP DEFAULT;
  43. DROP SEQUENCE IF EXISTS %(table)s_%(clmn)s_seq;
  44. COMMIT;""" % db_params
  45. #remove double quotes
  46. sql = sql.replace('"','')
  47. progress.setInfo(sql)
  48.  
  49. #make connection string
  50. constr = """dbname=%(dbname)s host=%(host)s port=%(port)s user=%(user)s password=%(password)s""" % db_params
  51. progress.setInfo(20*'-' + ' DB Connection string')
  52. progress.setInfo(constr)
  53. #make db connection
  54. con = psycopg2.connect(constr)
  55. cur = con.cursor()
  56. #execute the above created sql statement
  57. progress.setInfo(20*'-' + ' Executing SQL statement ...')
  58. cur.execute(sql)
  59. progress.setInfo(20*'-' + ' ... done.')
  60.  
  61. SELECT column_default
  62. FROM information_schema.columns
  63. WHERE (table_schema, table_name, column_name) = ('/*schema*/', '/*table*/', '/*column*/');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement