0

i use QGIS and i want to know how to run a sql code (fichier.sql) in python console in QGIS

I have used this :

impost os 
os.system("psql \i C:/fichier.sql -d test2 ") 

but it doesn't work Any Help !!!

2
  • What is your goal? It's not possible to simply run SQL from the Python console. We need to know what you are trying to achieve to suggest solutions.
    – underdark
    Jun 2, 2013 at 17:27
  • I need to assign my Database with a script python that use the file.sql
    – Anass Ing
    Jun 2, 2013 at 17:49

1 Answer 1

1

If you work in Python, you must use one of the appropriate Python modules like psycopg2.

If you want to call an external command (psql), see Calling an external command in Python or Execute External Programs, the Python Ways:

The best example of the use of this module is given by the sextante plugin wich use it to call R, GRASS GIS or SAGA GIS

  1. it creates the command in a temporary file (sextante_script.r or grass.script.sh (or .bat))
  2. it executes the command with subprocess.Popen:

with R:

command = ["R", "CMD","BATCH", "--vanilla", RUtils.getRScriptFilename(), RUtils.getConsoleOutputFilename()]
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True)

with GRASS GIS

command = "grass64 " + GrassUtils.grassMapsetFolder() + "/user"
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.