Thread: SQL with Python

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    SQL with Python


    Hi,

    I have a query I'm trying to execute in Python.
    The database is a PostgreSQL database and i have managed to connect to it fine

    I want to select a subset of data from a table using the code below. It doesnt work because its treating var1 as a column with i want it to treat it as an attribute

    Does anyone see the problem

    Code:
    var1 = 'IE'
    SQLstatement = ('SELECT DISTINCT * '
                    'FROM table_a '
                    'WHERE Country_Code = %s'%(var1,))
    Thank you for your time
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Code:
    var1 = 'IE'
    SQLstatement = ("SELECT DISTINCT * "
                    "FROM table_a "
                    "WHERE Country_Code = '%s'"%(var1,))
    I enclose strings in sql statements in single quotes and have no experience otherwise.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo