Python Forum
ssues with importing data from ODBC
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ssues with importing data from ODBC
#1
Hi, I am new to Python and I started immediately from importing data I am planning to analyse in pandas(?) to get tables, mean scores etc. I know how I can import that from excel but I have issues with importing data from ODBC. I can do it in excel, in SPSS and in R using this code:

abc <- odbcConnect("asa")
Country <- sqlQuery(abc, "SELECT * FROM dbo.tblCountry")

I am trying to replicate it in Python using this:
import pandas.io.sql
import pyodbc
import sys

server = 'asa'
db = 'dbo.tblCountry'

conn = pyodbc.connect(server=server, database=db)
print(conn)
Unfortunately, I have this error:
Error:
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
What am I doing wrong?
Larz60+ write Jan-31-2022, 05:57 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBcode tags on future posts.
Reply
#2
I don't have experience with this so I might be wrong, but you say this works: abc <- odbcConnect("asa"). So what is "asa"? I guess it is a DSN. And apparently you do not have to specify username and password, so I guess this information is stored in the DSN.
Then in your python source you say:
server = 'asa'
db = 'dbo.tblCountry'
But in the previous (working) example, "dbo.tblCountry" was the name of a table. Not a database. So to do the same as in the (working) example I guess you should do:
conn = pyodbc.connect("DSN=asa")
Note: the parameter you specify is just passed through to the ODBC handler, so it is one quoted parameter. If you need to pass more parameters, it should still be one parameter in Python:
conn = pyodbc.connect("DSN=MSSQLServerDatabase;UID=myuid;PWD=mypwd")
(Don't add unnecessary spaces.)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Importing python data to Textfile or CSV yanDvator 0 1,763 Aug-02-2020, 06:58 AM
Last Post: yanDvator
  Importing data from a text file into an SQLite database with Python macieju1974 7 4,145 Jun-29-2020, 08:51 PM
Last Post: buran
  ODBC iPhone to PC KipCarter 0 1,513 Feb-19-2020, 11:53 AM
Last Post: KipCarter
  accessing Sybase (ASE and/or Anywhere) via ODBC... on Linux zatlas1 0 2,904 Jan-31-2019, 06:48 PM
Last Post: zatlas1
  Importing csv data into sql lite cant figure it out. jimmyvegas29 5 3,769 Jul-16-2018, 02:02 AM
Last Post: jimmyvegas29
  how do I successfully use PDO with my ODBC for MSSQL? cyberion1985 3 6,593 Jun-26-2018, 04:44 PM
Last Post: Maverick494
  Importing data Scott 4 5,107 May-12-2018, 11:22 AM
Last Post: snippsat
  ODBC Not Working Bonediggler 3 2,810 Sep-28-2017, 06:18 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020