How do I access a .mdb file from python? - python

I have my database in msacess 2000 .mdb format which I downloaded from the net and now I want to access that database from my program which is a python script.
Can I call tables from my programs??
it would be very grateful if anyone of you please suggest me what to do

For whoever pass by, another option is using mdbtools - which can export the MDB database to a re-usable format, like CSV- check: http://mazamascience.com/WorkingWithData/?p=168
If you don't need to update the mdb file, just to import legacy data, doing something akin to the recipe on the link above is easier than get pyodbc working properly and, it does not require access to a Windows machinne

If you work on Windows, then you can use ODBC and use odbc module (ActiveState Python has it by default, this is part of win32 extensions), or pyodbc module. Have a look at answers to: How to connect pyodbc to an Access (.mdb) Database file
If you use Jython you can use JDBC-ODBC bridge:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
db = DriverManager.getConnection('jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\\Nwind.mdb', usr, passwd)

Create an ODBC DSN wit hthis MDB. Python can access ODBC data sources.

Related

csv->Oracle DB autoscheduled import

I have basic csv report that is produced by other team on a daily basis, each report has 50k rows, those reports are saved on sharedrive everyday. And I have Oracle DB.
I need to create autoscheduled process (or at least less manual) to import those csv reports to Oracle DB. What solution would you recommend for it?
I did not find such solution in SQL Developer, since it is upload from file and not a query. I was thinking about python cron script, that will autoran on a daily basis and transform csv report to txt with needed SQL syntax (insert into...) and then python will connect to Oracle DB and will ran txt file as SQL command and insert data.
But this looks complicated.
Maybe you know other solution that you would recommend yo use?
Create an external table to allow you to access the content of the CSV as if it were a regular table. This assumes the file name does not change day-to-day.
Create a scheduled job to import the data in that external table and do whatever you want with it.
One common blocking issue that prevents using 'external tables' is that external tables require the data to be on the computer hosting the database. Not everyone has access to those servers. Or sometimes the external transfer of data to that machine + the data load to the DB is slower than doing a direct path load from the remote machine.
SQL*Loader with direct path load may be an option: https://docs.oracle.com/en/database/oracle/oracle-database/19/sutil/oracle-sql-loader.html#GUID-8D037494-07FA-4226-B507-E1B2ED10C144 This will be faster than Python.
If you do want to use Python, then read the cx_Oracle manual Batch Statement Execution and Bulk Loading. There is an example of reading from a CSV file.

Should an embedded SQLite DB used by CLI app be uploaded to version-control (Git)?

I'm working on a Python CLI app that has to manage some data on a sqlite db (creating, updating and deleting records). I want the users to be able to install the app and use it right away. So my question is, can I just upload an empty sqlite db to GitHub? Or should I just upload a schema file and during installation build the db in a build step? I suppose if going the second way, users should have sqlite pre-installed or else the installation will fail. What I want is for them to just install the app, without worrying about dependencies and such.
When it comes to SQLite, My understanding is that SQLite is generally used as an embedded DB thus users wouldn't need to have SQLite preinstalled. (Of course, it can be used as a standalone DB server, but it's mainly known for its "ease of embeddability" aka...simply just run). Without any effort, in the embedded form, the client itself would create this db.
Using SQLite is just a one-liner as:
conn = sqlite3.connect('my.db')
or
conn = sqlite3.connect('/path/to/my.db')
Or even in-memory (as cache)
conn = sqlite3.connect(':memory:')
When this line runs, it would create a connection by either opening the file (if it exists) or create this file (as an empty DB) if the file is not present. In short, The SQLite library will always read the existing file or create it if it doesn't exist. Thus, You will always have a running DB out of the box. (The only time I can see it failing is if this db file is corrupt for some reason or the SQLite library cannot create the file in a location due to permission issues)
From a user perspective (or developer perspective for that matter), there is nothing that needs to be done to install SQLite. There are no external dependencies for embedded DB or anything to be preinstalled. It simply works. If there are other applications that share this database, they just need to open the particular db file and that's it.
Therefore coming back to your main question, the general best practice is that the application instantiates the database (Whatever the DB is for that matter) on its first run by importing the SQL/Schema (and initial data) file (SQL File, CSV, JSON, XML, from code etc...). The SQL/Schema file can be maintained along with the application source in Github (or whatever VCS) or packaged with the binary in the packaged format (zip, tar...etc) that is given for distribution. So in your case, the second approach that you have thought of might be better. This is even good from a code maintenance and review perspective.
It is best not to upload the "database" as a binary, rather instantiate it on the first run and populate it with data.
If your sqlite db have some pre tables and records, you should upload it to vc in order to be used by the users. but if you need a clean db for each instance of your project I suggest creating db during the initialization process of your app.
Also if your app needs some pre-data inside the db, one of the best practices is to put the data into a file like predata.json and during initialization, create db and import it into the db.

SAS to Python - Remote Access to Oracle database

I understand how to connect remotely to Oracle database in python:
import cx_Oracle
connstr = 'Oracle_Username/Oracle_Password#IP_Address:Port/Instance'
conn = cx_Oracle.connect(connstr)
However I have SAS scripts and want to mimic the same procedure in Python but am struggling to understand the role of path and schema in the following SAS script and if it needs to be incorporated into the Python script?
libname ora oracle user=oracle-user
password=oracle-password
path=oracle-path
schema=schema-name;
I have read through documentation but not being familiar with SAS, it is still very vague.
The PATH= option specifies the TNS entry for the Oracle database. Get your DBA to translate that for you into the syntax you need to replace the #IP_Address:Port/Instance in your connection string.
The value after USER= is what you called Oracle_Username and the value after PASSWORD= is what you called Oracle_Password.
The value of the SCHEMA= option specifies which schema in Oracle the SAS libref will use. So if the SAS code later references a dataset by the name ORA.MYTABLE then it means the table MYTABLE in the schema schema-name. In direct Oracle code you could reference that table directly as schema-name.MYTABLE.
Pathname= is TNS entry configured in Oracle(sever related details are configured here)
Schema= is user schema
If you are able to connect Oracle you can access any table like below
Schema_name.table_name

python cx_Oracle to dump database objects another server

I want to dump oracle objects like tables and stored procedures using cx_Oracle from python ,
is any tutorial how to do this ?
If you are looking for the source code for tables you can use the following:
select DBMS_METADATA.GET_DDL('TABLE','<table_name>') from DUAL;
for stored procedures you can use
select text from all_source where name = '<procedure name>'
In general this is not a cx_Oracle specific problem, just call the oracle specific tables (like all_source) or functions (like get_ddl) and read it in like any other query. There are more of these sorts of tables (like user_source for source that you the specific user own) in Oracle, but I'm doing this off the top of my head and don't have easy access to an Oracle db to remind myself.

Accessing table names in .mdb file using Python package Adodbapi

I'm writing an application in Python and I'm using the Adodbapi package to handle my sql queries. For one part of the code, I need to communicate with an Access '97 (no, it's not a typo) database, and I want to get a list of the table names via an SQL statement. I've tried a bunch of statements using MSysObjects but I run into permissions isssues - no I don't have the option of opening the db in access and changing the permissions. I would imagine that Adodbapi has a way of accessing Schema information but I can't manage to find it.
Thanks in advance for your help.

Categories