Questions tagged [pyodbc]

pyodbc is a module that allows you to use Python to make a database connection using ODBC

pyodbc is a Python 2.x and 3.x module that allows you to use ODBC to connect to almost any database from Windows, Linux, OS/X, and more.

It implements the Python Database API Specification v2.0, but additional features have been added to further simplify database programming.

pyodbc is licensed using an MIT license, so it is free for commercial and personal use. You can even use the source code in your own projects.

Installing can be done by running:

pip install pyodbc

Most Linux distributions are starting to provide pre-compiled packages, but those versions are often quite outdated.

Full source code is also available from the GitHub repository.

3768 questions
13
votes
3 answers

How to connect MS Access to Python using pyodbc

I'm having trouble connecting a database in access with pyodbc. I've seen other example codes that appear near identical to mine that work: import pyodbc cnxn = pyodbc.connect('DRIVER={SQL…
Michael
  • 177
  • 1
  • 2
  • 9
13
votes
3 answers

Check if pyodbc connection is open or closed

I often get this error: ProgrammingError: The cursor's connection has been closed. Is there a way to check whether the connection I am using has been closed before I attempt to execute a query? I'm thinking of writing a wrapper to execute queries.…
cammil
  • 9,499
  • 15
  • 55
  • 89
13
votes
3 answers

How can I pass configuration variable values into the pyodbc connect command?

I have a .ini (configuration file) where I have mentioned the server name, Database Name, UserName and Password with which I can connect my app to the MSSQL self.db = pyodbc.connect( 'driver={SQL…
Prasanth
  • 339
  • 1
  • 6
  • 19
12
votes
2 answers

Connect MySQL 3.23 with pyodbc 3.07

I'm trying to connect to an old MySQL 3.23 server from an Ubuntu 16 client with UnixODBC and pyodbc 3.07. I've tried three (3) versions of MySQL Connector/ODBC and two (2) from MariaDB: MySQL-ODBC 5.3.9 Supports only the new mysql authentication…
jhinghaus
  • 770
  • 10
  • 27
12
votes
1 answer

Parallelizing pandas pyodbc SQL database calls

I am currently querying data into dataframe via the pandas.io.sql.read_sql() command. I wanted to parallelize the calls similar to what this guys is advocating: (Embarrassingly parallel database calls with Python (PyData Paris 2015 )) Something like…
user1129988
  • 1,516
  • 4
  • 19
  • 32
12
votes
2 answers

Python - pyodbc call stored procedure with parameter name

I need to call a SqlServer stored procedure from python2.7 via pyodbc module with input parameter name. I tried based on documentation by input parameter order: cursor.execute('{CALL [SP_NAME](?,?)}', ('value', 'value')) It works, but…
Aida.Mirabadi
  • 996
  • 4
  • 10
  • 27
12
votes
1 answer

What happens if you don't close a pyodbc connection?

Just wondering what happens if a connection is not properly closed in pyodbc. Also, do i need to close the cursor before the connection? In my particular use case I included a call to close the connection in a custom DB Class in the .__del__()…
mwoods
  • 317
  • 1
  • 3
  • 9
12
votes
2 answers

create a database using pyodbc

I am trying to create a database using pyodbc, however, I cannot find it seems to be paradox as the pyodbc needs to connect to a database first, and the new database is created within the linked one. Please correct me if I am wrong. In my case, I…
ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
11
votes
2 answers

Calling a stored procedure python

I am writing a script to pull info/update a MsSQL server and i can get my one stored procedure call to work but not my second one in the updateDB function. here is my code the script runs fine no error codes import pyodbc import json import…
user1229126
  • 147
  • 1
  • 1
  • 6
11
votes
5 answers

Python is slow when iterating over a large list

I am currently selecting a large list of rows from a database using pyodbc. The result is then copied to a large list, and then i am trying to iterate over the list. Before I abandon python, and try to create this in C#, I wanted to know if there…
nycynik
  • 7,371
  • 8
  • 62
  • 87
11
votes
2 answers

Unable to import pyodbc on Apple Silicon - Symbol not found: _SQLAllocHandle

I am currently working on a python (3.8) project on my 2021 MacBook Pro with Apple Silicon. Ultimately, the goal is to build a ML model on data I read from an Azure SQL DB using Apple's Tensorflow fork. Therefore, I am developing the project on…
eckha
  • 113
  • 1
  • 5
11
votes
3 answers

Multi-row UPSERT (INSERT or UPDATE) from Python

I am currently executing the simply query below with python using pyodbc to insert data in SQL server table: import pyodbc table_name = 'my_table' insert_values = [(1,2,3),(2,2,4),(3,4,5)] cnxn = pyodbc.connect(...) cursor =…
Mosy
  • 175
  • 1
  • 1
  • 9
11
votes
4 answers

Python Pandas read_sql_query “'NoneType' object is not iterable” error

I am trying to execute a sql and save a result into Panda Dataframe. here is my code. dbserver = 'validserver' filename = 'myquery.sql' database ='validdb' conn = pyodbc.connect(r'Driver={SQL Server};Server='…
ProgSky
  • 2,530
  • 8
  • 39
  • 65
11
votes
4 answers

SQL Server temp table not available in pyodbc code

I'm running a series of complex sql queries in python and it involves temp tables. My auto-commit method doesn't seem to be working to retrieve the data from the temp table. The code snippet I'm using below and this is the output I'm getting:…
sudhareg
  • 317
  • 2
  • 3
  • 10
11
votes
2 answers

Python pyodbc Unicode issue

I have a string variable res which I have derived from a pyodbc cursor as shown in the bottom. The table test has a single row with data ä whose unicode codepoint is u'\xe4'. The Result I get is >>> res,type(res) ('\xe4', ) Whereas the…
rogue-one
  • 11,259
  • 7
  • 53
  • 75