Development Python Help

Discussion in 'Tech Support' started by towelie, 10 Apr 2014.

  1. towelie

    towelie How do I Internet!!

    Joined:
    1 Sep 2011
    Posts:
    399
    Likes Received:
    10
    Hi Guys

    I'm New to Python Programming but i have got to this stage ok, but have hit a wall which i hope you can help me with.

    What i am trying to do is import a CSV file to a DB, This part is working. The problem i am currently having is getting the DB connections to import its setting from a config file.
    Python Version 3.3.4
    Using modules pyodbc, csv, configparser.

    Here is the Code.
    ************************************
    import pyodbc, csv, configparser

    #Read Connection Configuration file for DB

    config = configparser.ConfigParser()
    config.read('DB.cfg')
    config.Sections()
    ['DB']



    connStr = (
    r''Driver=config.get('DB', 'Driver');'' +
    r''Server=config.get('DB', 'UID');'' +
    r''Database=config.get('DB', 'Database');'' +
    r''UID=config.get('DB', 'UID');'' +
    r''PWD=config.get('DB', 'PWD');''
    )
    db = pyodbc.connect(connStr)
    ************************************

    The error i receive is.
    ************************
    r''Driver=config.get('DB', 'Driver');'' +
    ^
    SyntaxError: invalid syntax
    *************************

    Thanks in advance.
    :hip::hip::hip:
     
    Last edited: 10 Apr 2014
  2. badders

    badders Neuken in de Keuken

    Joined:
    4 Dec 2007
    Posts:
    2,635
    Likes Received:
    72
    How about:
    Code:
    
    connStr = (
    "Driver="+ config.get('DB', 'Driver') +
    ";Server=" + config.get('DB', 'UID') +
    ";Database=" + config.get('DB', 'Database') +
    ";UID="+ config.get('DB', 'UID') +
    ";PWD=" + config.get('DB', 'PWD')
    )
     
    towelie likes this.
  3. towelie

    towelie How do I Internet!!

    Joined:
    1 Sep 2011
    Posts:
    399
    Likes Received:
    10
    Thanks Badders didn't work new error was bad syntax for "Driver=" i have a look this weekend.

    I think the issue is the way i am calling the sections from with the pyodbc modules script.
     
  4. Thawn

    Thawn What's a Dremel?

    Joined:
    12 Nov 2013
    Posts:
    26
    Likes Received:
    2
    Your double quotes should be actual double quote chars (") not pairs of single quotes ('') - they may look very similar but they do very different things!
     
    towelie likes this.
  5. towelie

    towelie How do I Internet!!

    Joined:
    1 Sep 2011
    Posts:
    399
    Likes Received:
    10
    Ok have resolved this so thanks to you all. So for anyone interest. Here is the Code and the Config file.

    **CODE**
    import pyodbc, csv, configparser

    #Read Connection Configuration file for DB

    config = configparser.ConfigParser()
    config.read('DB.cfg')
    config.sections()
    []



    connStr = (
    r" Driver="+ config.get('Database', 'Driver') +
    r" Server="+ config.get('Database', 'Server') +
    r" Database="+ config.get('Database', 'Database') +
    r" UID="+ config.get('Database', 'UID') +
    r" PWD="+ config.get('Database', 'PWD') +
    r" trusted_connection="+ config.get('Database', 'trusted_connection')
    )
    #print(connStr) Put in to check string value, before executing.

    db = pyodbc.connect(connStr)
    **DB.CFG**
    CONFIG file
    [Database]
    Driver= {SQL SERVER};
    Server= IP/DNS,1433\INSTANCE;
    Database= DATABASENAME;
    UID= USERNAME;
    PWD= PASSWORD;
    trusted_connection= no;
     

Share This Page