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
Related
I have a Pandas dataframe df which I want to push to a relational database as a table. I setup a connection object (<Connection>) using SQLAlchemy (pyodbc is the connection engine), and called the command
df.to_sql(<Table_Name>, <Connection>)
which I was able to confirm was written as a table to the desired relational database by visual examination of it in SQL Server Management Studio (SSMS). But in the left-hand-side list of databases and their tables in SSMS I see that it has named it
<Sender_Server>\<Username>.<Table_Name>
where <Sender_Server> is (I think) related to the name of the server I ran the Python command from, <Username> is my username on that server, and <Table_Name> is the desired table name.
When I right-click on the table and select to query the top one thousand rows I get a query of the form
SELECT * FROM [<Data_Base_Name>].[<Sender_Server>\<Username>].[<Table_Name>]
which also has the <Sender_Server>\<Username> info in it. The inclusion of <Sender_Server>\<Username> is undesired behaviour in our use case.
How can I instead have the data pushed such that
SELECT * FROM [<Data_Base_Name>].[<Table_Name>]
is the appropriate query?
By default, .to_sql() assumes the default schema for the current user unless schema="schema_name" is provided. Say, for example, the database contains a table named dbo.thing and the database user named joan has a default schema named engineering. If Joan does
df.to_sql("thing", engine, if_exists="append", index=False)
it will not append to dbo.thing but will instead try to create an engineering.thing table and append to that. If Joan wants to append to dbo.thing she needs to do
df.to_sql("thing", engine, schema="dbo", if_exists="append", index=False)
Using below code to connect to postgres connects me to the default public schema. Where can i mention
the schema name in the connection string? I am trying to insert the data. So when I use dataframe.to_sql('myschema.mytable', engine, if_exists='append', index=False)
It creates a table name with myschema.mytable in public schema instead of inserting the data into mytable which already exist under myschema.
I am using sqlalchemy library in python. Below is my connection string.
engine = create_engine('postgres://user:password#host:5432/dbname')
I tried the jdbc way by appending ?currentSchema=schemaname and ?schema=schemaname but both does not work.
#Belayer, thanks for the response. After some more research, it seems you can mention the schema name while loading the data to database.
dataframe.to_sql('mytable', con=connection, schema='myschema',if_exists='append', index=False)
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.
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.
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.