Python ODBC connection: not a valid file name error - python

I am new to Python and have been assigned the task to copy all the MS Access database files(we have five) into CSV format using Python. I have searched through lots of posts on Stack Overflow and sketched together this amateur snippet. I need to see the files I have in my MS Access database. Can someone please provide assistance.
Pyodbc Error - Python to MS Access
open access file in python
import pyodbc
conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=T:\\DataDump\\7.18.2016 PCR etrakit.accdb")
conn = pyodbc.connect(conn_string)
cursor = conn.cursor()
cursor.close()
conn.close()
print 'All done for now'

[UPDATED]Try running this
conn_string = ("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\T:\\DataDump\\7.18.2016 PCR etrakit.accdb")
use double backslash instead.

Per this post:
Try doing it as a single line
conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};C:\\T:\\DataDump\\7.18.2016 PCR etrakit.accdb;'
However, I am a bit confused by your file path. On the root of your C:\ drive, you have a directory named T:?
It may also be worth noting that file paths with spaces in the name are not always handled as expected. An alternate approach would be to try and escape the spaces in your file path:
conn_string = r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};C:\\T:\\DataDump\\7.18.2016\ PCR\ etrakit.accdb;'

Related

Python ODBC Driver Manager SQLDriverConnect error

The database teacher in college gave me a task to get all the fields from the "Кураторы" table. Yep, he likes MS Acces...
Seems like something's wrong with my drivers. I'm using Windows 10 N tho. Already have checked ODBC data sources and founded some curious things. There are Microsoft Access Driver, but on ODBC Data Source Administrator (x32) only! x64 one has only one SQL Server driver
Using pyodbc-4.0.30-cp38-cp38-win_amd64 version of pyodbc and x64 Python 3.8
Getting this error:
Traceback (most recent call last):
File "D:/Documents/College/4th grade/DB/13.09.py", line 3, in <module>
conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver(*.mdb, *.accdb)};DBQ=C:\Users\reddk\Desktop\db.accdb')
pyodbc.InterfaceError: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
Here's the code:
import pyobc
conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver(*.mdb, *.accdb)};DBQ=C:\Users\reddk\Desktop\db.accdb') # connecting to DB
cursor = conn.cursor()
cursor.execute('SELECT * FROM Кураторы') # request
result = cursor.fetchall() # converting the result of a query to the list of rows and assign it to the result
for row in result:
print(row) # printing each row of the list
Appreciate any help sm!

I am unable to write to MS Access Database using pyodbc

I am unable to write this df into the access table.
What am I not doing right?
conn_str = (
r'DRIVER={Microsoft Access Driver (*.mdb)};'
r'DBQ=C:\Users\harsh\Desktop\Database1.mdb;'
)
cnxn = pyodbc.connect(conn_str)
SQL = 'SELECT * FROM Index_data;'
dfins = pd.read_sql(SQL, cnxn)
for index, row in dfins.iterrows():
with cnxn.cursor() as crsr:
crsr.execute('select * from df')
conn.commit()
You probably have not installed ODBC driver for MS Access, or its name
"Microsoft Access Driver (*.mdb)"
don't agree with the string used in your program — for newer versions of Microsoft Access it is
"Microsoft Access Driver (*.mdb, *.accdb)".
So verify its name, or install it:
Open Control Panel, select Administrative Tools, then ODBC Data Sources.
New window will open up. Select “User DSN” tab.
Then verify the driver name, or install an appropriate Microsoft Access Driver — see for example Steps to create a New ODBC Connection on Windows 10.

Python "REPLACE" command in a SQL query(Error message)

I am new in python and I need this query to check my database if it is working. There are lots of queries but it is only one of them.
import pyodbc
db_file = 'C:\\Users\\****\\Desktop\\bbbb.mdb' #define the location of your Access file
odbc_conn_str = 'DRIVER={Microsoft Access Driver (*.mdb)};DBQ=%s' %(db_file) # define the odbc connection parameter
conn = pyodbc.connect(odbc_conn_str)
cursor = conn.cursor() # create a cursor
sql_update_statement = "UPDATE NUMARATAJ SET KAPINUMERIK = IIf (ISNUMERIC (LEFT (REPLACE(KAPINO,'-','/'),4)=-1),LEFT(KAPINO,4))" # edit the SQL statement that you want to execute
cursor.execute(sql_update_statement) # execute the SQL statement
cursor.commit()
cursor.close()
conn.close()
When I try to run this code it says;
File "C:\Users\****\Desktop\aa2a.py", line 9, in <module>
cursor.execute(sql_update_statement) # execute the SQL statement
pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Microsoft Access Sürücüsü] Undefined 'REPLACE' function in the expression. (-3102) (SQLExecDirectW)")
How can I fix it, or get it work, can you help me?
It could be:
sql_update_statement = "UPDATE NUMARATAJ SET KAPINUMERIK = LEFT(KAPINO,4) WHERE ISNUMERIC(LEFT(REPLACE(KAPINO,'-','/'),4)"
However, Replace is a VBA function, not Access SQL, so that is probably why you receive the error.
Try simply:
sql_update_statement = "UPDATE NUMARATAJ SET KAPINUMERIK = LEFT(KAPINO,4) WHERE ISNUMERIC(LEFT(KAPINO),4)"
There are several "VBA functions" that were not directly supported by the older "Jet" ODBC driver (Microsoft Access Driver (*.mdb)) but are directly supported by the newer "ACE" ODBC driver (Microsoft Access Driver (*.mdb, *.accdb)). Replace is one of those functions.
So if you really need to use the Replace function you can download and install the 32-bit version of the Access Database Engine Redistributable and use the newer ODBC driver.

Connecting and testing a JDBC driver from Python

I'm trying to do some testing on our JDBC driver using Python.
Initially figuring out JPype, I eventually managed to connect the driver and execute select queries like so (reproducing a generalized snippet):
from __future__ import print_function
from jpype import *
#Start JVM, attach the driver jar
jvmpath = 'path/to/libjvm.so'
classpath = 'path/to/JDBC_Driver.jar'
startJVM(jvmpath, '-ea', '-Djava.class.path=' + classpath)
# Magic line 1
driver = JPackage('sql').Our_Driver
# Initiating a connection via DriverManager()
jdbc_uri = 'jdbc:our_database://localhost:port/database','user', 'passwd')
conn = java.sql.DriverManager.getConnection(jdbc_uri)
# Executing a statement
stmt = conn.createStatement()
rs = stmt.executeQuery ('select top 10 * from some_table')
# Extracting results
while rs.next():
''' Magic #2 - rs.getStuff() only works inside a while loop '''
print (rs.getString('col_name'))
However, I've failed to to batch inserts, which is what I wanted to test. Even when executeBatch() returned a jpype int[], which should indicate a successful insert, the table was not updated.
I then decided to try out py4j.
My plight - I'm having a hard time figuring out how to do the same thing as above. It is said py4j does not start a JVM on its own, and that the Java code needs to be prearranged with a GatewayServer(), so I'm not sure it's even feasible.
On the other hand, there's a library named py4jdbc that does just that.
I tinkered through the dbapi.py code but didn't quite understand the flow, and am pretty much jammed.
If anyone understands how to load a JDBC driver from a .jar file with py4j and can point me in the right direction, I'd be much grateful.
add a commit after adding the records and before retrieving.
conn.commit()
I have met a similar problem in airflow, I used teradata jdbc jars and jaydebeapi to connect teradata database and execute sql:
[root#myhost transfer]# cat test_conn.py
import jaydebeapi
from contextlib import closing
jclassname='com.teradata.jdbc.TeraDriver'
jdbc_driver_loc = '/opt/spark-2.3.1/jars/terajdbc4-16.20.00.06.jar,/opt/spark-2.3.1/jars/tdgssconfig-16.20.00.06.jar'
jdbc_driver_name = 'com.teradata.jdbc.TeraDriver'
host='my_teradata.address'
url='jdbc:teradata://' + host + '/TMODE=TERA'
login="teradata_user_name"
psw="teradata_passwd"
sql = "SELECT COUNT(*) FROM A_TERADATA_TABLE_NAME where month_key='202009'"
conn = jaydebeapi.connect(jclassname=jdbc_driver_name,
url=url,
driver_args=[login, psw],
jars=jdbc_driver_loc.split(","))
with closing(conn) as conn:
with closing(conn.cursor()) as cur:
cur.execute(sql)
print(cur.fetchall())
[root#myhost transfer]# python test_conn.py
[(7734133,)]
[root#myhost transfer]#
In py4j, with your respective JDBC uri:
from py4j.java_gateway import JavaGateway
# Open JVM interface with the JDBC Jar
jdbc_jar_path = '/path/to/jdbc_driver.jar'
gateway = JavaGateway.launch_gateway(classpath=jdbc_jar_path)
# Load the JDBC Jar
jdbc_class = "com.vendor.VendorJDBC"
gateway.jvm.class.forName(jdbc_class)
# Initiate connection
jdbc_uri = "jdbc://vendor:192.168.x.y:zzzz;..."
con = gateway.jvm.DriverManager.getConnection(jdbc_uri)
# Run a query
sql = "select this from that"
stmt = con.createStatement(sql)
rs = stmt.executeQuery()
while rs.next():
rs.getInt(1)
rs.getFloat(2)
.
.
rs.close()
stmt.close()

How do I import an .accdb file into Python and use the data?

I am trying to figure out a way to create a program that allows me to find the best combination of data based on several different factors.
I have a Microsoft Access file with creature data in it. Attack, Defense, Health, Required Battle skill to use and several other bits of info.
I am trying to import this .accdb (Access 2013) file and be able to access the stored data.
I am going to try to make a program that scans all the data and runs all possible combinations (sets of 5 creatures) to find the strongest combination of creatures for different required battle skills (ex: 100 battle skill would use creature 1, 2, 3, 4 and 5 where 125 battle skill would use creature 3, 5, 6, 8, and 10)
The main thing I need help with first is being able to import the data base for easy access so I do not have to recreate the data in python and so I can use the same program for new access databases in the future.
I have installed https://code.google.com/p/pypyodbc/ but can't seem to figure out how to get it to load an existing file.
Edit
I tried to use the code from Gord's answer, modified to fit my info.
# -*- coding: utf-8 -*-
import pypyodbc
pypyodbc.lowercase = False
conn = pypyodbc.connect(
r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
r"Dbq=C:\Users\Ju\Desktop\Dark Summoner.accdb;")
cur = conn.cursor()
cur.execute("SELECT Number, Name, Atk, Def, HP, BP, Species, Special FROM Impulse AA+");
while True:
row = cur.fetchone()
if row is None:
break
print (u"Creature with Number {1} is {1} ({2})".format(
row.get("CreatureID"), row.get("Name_EN"), row.get("Name_JP")))
cur.close()
conn.close()
Was getting an error with the print line so added () around it.
I am now getting this error, similar to what I was getting in the past.
Traceback (most recent call last):
File "C:\Users\Ju\Desktop\Test.py", line 6, in <module>
r"Dbq=C:\Users\Ju\Desktop\Dark Summoner.accdb;")
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 2434, in __init__
self.connect(connectString, autocommit, ansi, timeout, unicode_results, readonly)
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 2483, in connect
check_success(self, ret)
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 988, in check_success
ctrl_err(SQL_HANDLE_DBC, ODBC_obj.dbc_h, ret, ODBC_obj.ansi)
File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 964, in ctrl_err
raise Error(state,err_text)
pypyodbc.Error: ('IM002', '[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified')
I looked through the pypyodbc.py file at the lines mentioned in the error code, but could not figure it out. I tried to remove the "r" from the beginning of r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" and tried a space between r and "Driver because I did not know what it was for, But got a different error.
Edit
I checked my files as suggested. I believe I am running 64bit. I checked both the 32 bit and 64 bit versions. I have Microsoft Access Driver (*.mdb, *.accdb) in the 64 bit but not in the 32 bit. I am using the 2013 version of Microsoft Visual Studios.
Edit
Working now!
My final working code in case it helps anyone in the future.
# -*- coding: utf-8 -*-
import pypyodbc
pypyodbc.lowercase = False
conn = pypyodbc.connect(
r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
r"Dbq=C:\Users\Ju\Desktop\Dark Summoner.accdb;")
cur = conn.cursor()
cur.execute("SELECT Number, ID, Name, Atk, Def, HP, BP, Species, Special FROM Impulse_AA");
while True:
row = cur.fetchone()
if row is None:
break
print (u"ID: {1} {2} Atk:{3} Def:{4} HP:{5} BP:{6} Species: {7} {8}".format(
row.get("Number"), row.get("ID"), row.get("Name"), row.get("Atk"),
row.get("Def"), row.get("HP"), row.get("BP"), row.get("Species"), row.get("Special") ))
cur.close()
conn.close()
Say you have a database file named "Database1.accdb" with a table named "Creatures" containing the following data:
CreatureID Name_EN Name_JP
---------- -------- -------
1 Godzilla ゴジラ
2 Mothra モスラ
A minimalist Python script to read the data via pypyodbc on a Windows machine would look something like this:
# -*- coding: utf-8 -*-
import pypyodbc
pypyodbc.lowercase = False
conn = pypyodbc.connect(
r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" +
r"Dbq=C:\Users\Public\Database1.accdb;")
cur = conn.cursor()
cur.execute("SELECT CreatureID, Name_EN, Name_JP FROM Creatures");
while True:
row = cur.fetchone()
if row is None:
break
print(u"Creature with ID {0} is {1} ({2})".format(
row.get("CreatureID"), row.get("Name_EN"), row.get("Name_JP")))
cur.close()
conn.close()
The resulting output is
Creature with ID 1 is Godzilla (ゴジラ)
Creature with ID 2 is Mothra (モスラ)
Edit
Note that to use the "Microsoft Access Driver (*.mdb, *.accdb)" driver you need to have the Access Database Engine (a.k.a "ACE") installed on your machine. You can check whether you have 32-bit or 64-bit Python by running the following script:
import struct
print("running as {0}-bit".format(struct.calcsize("P") * 8))
Armed with that information you can download and install the matching (32-bit or 64-bit) version of the Access Database Engine from here
Microsoft Access Database Engine 2010 Redistributable

Categories

Resources