python to oracle db connection making delay - python

I'm using following code to connect to a remote oracle DB server, But it takes more than 10 second only to create the DB connectivity, then the DB SQL query return result instantly. Same connection made by the nodejs and it works perfectly. checked the connectivity between the source and destination, but cannot see any issue. Can you help me to diagnose this issue. I'm using;
python 3.6 and use oracle instant client libraries( basic) to connect
import cx_Oracle
dsn_tns=cx_Oracle.makedsn('dbhost','port',service_name='SERVICENAME')
conn = cx_oracle.connect(user=r'USER_NAME',password='******',dsn=dsn_tns)
c = conn.cursor()
c.execute("""SIMPLE SELECT QUERY""")
for row in c;
print (row[0],'-',row[1])
conn.close

Sometimes it may happen because of slow random number generation.
Take a look here https://learn.capstorm.com/copystorm/frequently-asked-questions/problems-solutions/how-do-i-fix-random-oracle-login-connection-timeouts/
Or here https://support.oracle.com/epmos/faces/DocContentDisplay?id=1594701.1

Related

Does pyodbc execute commands like "select columnA*2 where columnB!=0 from tableA into tableB" locally or remotely?

I am using pyodbc for generating new tables in a remote SQL server. I wrote the following code, replacing column names, table names, driver name, and server name with dummy names.
import pyodbc
query="select columnA*2 where columnB!=0 from tableA into tableB"
connection = pyodbc.connect('Driver={driverA};''Server=serverA;'
'Trusted_Connection=yes;')
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
cursor.close()
connection.close()
I wonder when I run this this code in my Jupyter notebook (I am not sure if running it in a Jupyter Notebook is relevant, but I figure it doesn't hurt to mention it), does my laptop execute some parts of the code or does my laptop send all these code to serverA and ask serverA to execute the command?
What about the following code? Does my laptop simply send the code to the server and wait for the response? Alternatively, does my laptop asks for the entire tableA and then do some of the computing locally?
cnxn = pyodbc.connect("Driver= {driverA};" "Server=serverA;" "Database=databaseA;"
"Trusted_Connection=yes;")
diagramFinal = pd.read_sql("""select columnA*2 where columnB!=0 from tableA""", cnxn)
cnxn.close()
I read through this documentation without finding relevant information. So, I wonder if someone could help me with figuring it out.

How to use MySQL with Python in a client server setup

I'd like to develop a python project in client - server style.
My Python code will be deployed on a client machine and the MySQL database on the server, so all connections need to flow through a network connection.
I know how to use Python with MySQL with basics such as creating tables and queries etc, however I wanted to learn how to connect my python project to the database over a network protocol.
I searched on Google or YouTube, but only found resources that use a local database connection.
Where can I find Python resources (article, blog, sample, tutorial or video) that explains how to connect to MySQL via the network?
You have server with MySQL database running on it and a python client-application, which needs data from the database?
I think usually client software never connects directly to the database (for security reasons). Instead the client communicates with a server-application (maybe over a REST-API).
How i meant it
This is seems to be a good guide.
https://pynative.com/python-mysql-tutorial/
I tried PyMySql quickly. It works. Only problem is it is in Beta. https://pymysql.readthedocs.io/en/latest/user/examples.html
So I suggest you try the MySQL Connector Python will be slightly painful to setup initially but it comes from the vendor. so you can trust it a bit better.
or alternatively https://pypi.org/project/mysqlclient/
import pymysql.cursors
import pymysql
try:
conn = pymysql.connect(host='10.1.1.1 (remote server ip or hostname)',
user='db_user_name',
password='db_password',
db='name_of_db',
cursorclass=pymysql.cursors.DictCursor)
sql = "select * from users"
with conn.cursor() as cursor:
# Read a single record
sql = "select * from users"
cursor.execute(sql)
result = cursor.fetchall()
print(result)
except Exception as inst:
print(str(inst))
finally:
conn.close()

PyODBC SQL Anywhere 17 Conect to Sybase Kernel Dies

I am Working on Ubuntu 18.04 when I am using Pyodbc connection with SQL Anywhere 17 Driver to connect to a Sybase DB, while trying to establish connection my Jupyter notebook Dies.
The expectation is, I should be able to run this code in Ubunt and connect to a Sybase DB.
I can connect and run query from Windows without problems(using DSN).
I have been working with other driver and SQL Server, MySQL and MariaDB and I have not encountered any problems.
I believe connection to Sybase database needs SQLANYWHERE DRVIER.
If Someone knows how get the connection string which is passed from pyodbc to the server when I use a DSN?(maybe this could give me an idea to know what i'm doing wrong).
Some advice?
Code run in windows without problems
import pyodbc
import pandas as pd
cnxn = pyodbc.connect("DSN=RevDSN")
print(cnxn)
data = pd.DataFrame(pd.read_sql_query(query, cnxn))
cnxn.close()
Since I didn't find a good explanation I am putting this here.
I installed the client from
https://archive.sap.com/documents/docs/DOC-35857
then followed the basic instructions from
https://wiki.scn.sap.com/wiki/display/SQLANY/Installing+SQL+Anywhere+17+on+Ubuntu+14.04
The ODBC Client will not ask for the keys
As the documentation says the important part is running the sa_config.sh and making sure that the exports happen.
Lastly edit the /etc/odbcinst.ini file and add the driver.
For example
[SQL Anywhere 17]
Driver=/opt/sqlanywhere17/lib64/libdbodbc17.so
TDS_Version=5.0
UsageCount=1
Then I used this connection string
import pyodbc
cnxn = pyodbc.connect('Driver={SQL Anywhere 17};LINKS=TCPIP{HOST=<server ip here>};PORT=2638;UID=admin;PWD=<password here>;ENG=<engine name>;DBN=<database name>;')
cursor = cnxn.cursor()
cursor.execute("select top 10 * from dba.<table name>")
for row in cursor:
print(row)
And it worked.

Extract data from client database

I have to send multiple data through Python from a DB allocated in the client to the Main DB in the server, whats the best solution to this? i currently have my web server up and functioning, i can fill my DB locally but i dont really know how to do it remotely, im using Python in my hardware, here is what i have so far in the client:
import mysql.connector
cnx = mysql.connector.connect(user='user', password='pass', host='url?', database='db') #im able to enter with this
cursor = cnx.cursor()
query = ("INSERT INTO IGNORE table " "(id,date,son) " "VALUES( (%s,%s,%s)")
for row in data: #ive already extracted data from the other DB
cursor.execute(query, (row[0],row[1],row[2]))
wich yealds an error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the righ syntax to use near 'IGNORE table(id,date,son) VALUES (number, '2017-11-09 14:33:15', 18.987)' at line 1
One possibility - Make sure your mysqld service is binding to your external ip address (set in your mysql config file on the server). By default I believe it binds to 127.0.0.1 or localhost. If it is binding to localhost, your db will never respond to external requests.
I fixed the issue with the MySQL port in the following way: I opened the port internally from my VM instead of from my Virtual Network (I'm using the Google Cloud Platform).

Connecting to Google hosted MySQL using SSL from python

Performing a basic test. I have a MySQL DB hosted in Google SQL. No issues using MySQL Workbench via SSL from my local computer. So can confirm that access for local IP is correctly setup.
Here is where I am having a problem. Writing a basic connection test program in Python. The program is making an SSL connection to DB and running a query that returns 6 rows. Code listed below.
CNX = mysql.connector.connect(**CONFIG)
cur = CNX.cursor()
cur.execute("select BoroId AS boro_id, BoroName AS boro_name from boro")
for (boro_id, boro_name) in cur:
print "%s %s" % (boro_id, boro_name)
cur.close()
CNX.close()
My issue is that the "for" statement iterates through 0 rows. I do not get any errors. I know the connection part is working and query execution is taking place - on inserting below line after "execute" statement sets value of result_row_count to 6.
result_row_count = cur.rowcount
Official MySQL document states I should be able to iterate through each of the six rows using "for" statement. Any help appreciated.

Categories

Resources