Python connection to MS-SQL 2008

Hi,

I need assistance connecting from Python to MS-SQL 2012.
It seems that the connection is made  (No errors), but I cannot run any statements to get data.

Can anyone help with code for example that will show how the connection should be?

I also tried with pyodbc with no luck:

connection = pyodbc.connect('DRIVER={FreeTDS};SERVER="****";UID="sa" PWD="******";database="*******"')

cur = conn.cursor()
cur.execute('select value_caption from cust_values')
conn.commit()

Open in new window


And get the following error:
Error Message
Can anyone help?
boloney232Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
cur = conn.cursor()
cur.execute('select value_caption from cust_values')
conn.commit()

should be:
cur = connection.cursor()
cur.execute("select value_caption from cust_values")
connection.commit()

Open in new window

Can you try that?
0
clockwatcherCommented:
An unable to connect to datasource error implies that you can't reach the machine that you're trying to connect to.  

Off the bat, your connection string doesn't look formatted correctly.  Do you really have the quotes in it?  They shouldn't be there.   You also don't have a ';' separating your UID from your PWD.  This:

'DRIVER={FreeTDS};SERVER="****";UID="sa" PWD="******";database="*******"')

Should look more like this:

'DRIVER={FreeTDS};SERVER=****;UID=sa;PWD=******;database=*******'

And as far as actually returning data goes (not that you're getting that far in your current code) but ...

conn = pyodbc.connect('DRIVER={FreeTDS};SERVER=****;UID=sa;PWD=******;database=*******')

cur = conn.cursor()
cur.execute('select value_caption from cust_values')
for row in cur.fetchone():
    print row.value_caption

Open in new window

0
boloney232Author Commented:
Hi,

Thank you for the answers.
This is a holiday season here so I wasn't able to test it.

I will test the suggestions today and post the results.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

boloney232Author Commented:
Hi,

I tried the solution you've provided and still get the same error.

Note: I did have the quotation marks and removed them. I did have the ; between the user and password fields but mistakenly removed them after editing the post to include * instead of the real values.
0
clockwatcherCommented:
You could try including the protocol version and the port in your connection string.

...Database=****;TDS_Version=8.0;Port=1433;

But my guess is that you'll need to troubleshoot the connectivity to your sql server.  Is it set up to accept TCP/IP connections?  Is there any firewalling going on.  Try telnetting to the sql server box on port 1433 from your Linux box?

telnet server 1433

Does it accept a connection?

If it does, my next suggestion at troubleshooting would be to actually try setting up a DSN and getting a successful connection with tsql.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
boloney232Author Commented:
Thank you. This addition did the trick (TDS_Version and Port).

I can now connect to the Database and extract information from it.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.