Oracle Database error: ORA-12537: TNS:connection closed - python

Aforementioned is the error I have been getting when I m trying to connect my python with Oracle.
con = cx_Oracle.connect("username/password#IP:PORT/xe")
cx_Oracle.DatabaseError: ORA-12537: TNS:connection closed
I have a doubt about the service name "xe". When I change it to "orclpdb". It gives the same error. Please help me sort this.

Related

Error on Pyodbc 'pyodbc.connection' object has no attribute 'setencoding'

I want to send strings to my SQL server via pyodbc.
Some of my strings are conaining Character like "Ä,Ü,Ö,ã,ó".
While trying to send them i got an Error saying ="quotation mark after the character..."
I Read that i have to use "connection.setencoding(encoding='utf-8') and connection.setdecoding(encoding='utf-8')"
and here comes my Error.
After starting my script i get the following Error:
"Attribute Error: 'pyodbc.Connection' object has no attribute ' setencoding'"
here is my Connections String:
cnxn = pyodbc.connect('DRIVER={FreeTDS},Server=-Serverip-;PORT=1433;DATABASE=-DataBase-;UID=-UID-;PWD=-PWD-;TDS_Version=7.2;charset=utf8')
cnxn.setencoding(encoding='utf-8')
Anyone an idea why im getting this kind of error?
or is there any other way?

Why am I getting this UnicodeDecodeError during a Mysql SELECT query? [duplicate]

This question already has answers here:
What is character encoding and why should I bother with it
(4 answers)
Closed 1 year ago.
I'm running a pretty basic script that is querying a MySQL table, but I'm running into an error that I can't find online. I see a lot of charmap/codec related issues online, but all have to do with reading text or data.
This error is happening when I attempt a MySQL SELECT query, or at least I think it is.
Does anyone know why Python would error on this?
My code reads like this:
Line 188 is where the error occurs.
Please look at the following answer:
Encoding Issue - MySQL
Adding the extra parameters while creating connection should resolve your issue.
con = mdb.connect('loclhost', 'root', '', 'mydb', use_unicode=True, charset='utf8')
Thanks.
alt column contains OSC control character (0x9d). Seems to be a data problem. You could try replacing the character to verify that's the issue but the long term solution should be to not allow those character in a string, convert them to html entities, or specify a connection character set.
SELECT id, REPLACE(alt, UNHEX('9D'), '') ...
See also this.

Mariadb python: Commands out of syncs

so I was trying to create a password manager for myself, using python and mariadb. After creating a table named pw, which contains Name, Account and Passwords 3 columns, I tried to create a function(Search_Passwords(app_name)) which I can use to enter a keyword to search in the database, and it will give me the right passwords. However, I ran into this error message:
Commands out of syncs error message.
I'm new to python and mariadb(using it cause for some reason MySQL doesn't work..), tried to look up for some answers but still can't figure it out. Can anyone help please? Below are other codes I think might be related.
This is what mariadb's table looks like.
Search_Passwords()
class UseDataBase
This is what I found online as a reference version where Search_Passwords() involved.
Sorry if my codes are not perfect... :(
MariaDB Connector/Python by default use unbuffered result sets, which means before executing another cursor all pending result sets need to be fetched or the cursor needs to be closed.
For example the following script
import mariadb
conn= mariadb.connect()
cursor1= conn.cursor()
cursor1.execute("select 1 from dual")
cursor2= conn.cursor()
cursor2.execute("select 2 from dual")
will throw an exception Mariadb.InterfaceError: Commands out of sync; you can't run this command now.
To avoid this, you need to create a buffered cursor:
cursor1= conn.cursor(buffered=True)

MySQL - Returning NULL even though it contains an integer [duplicate]

This question already has answers here:
Python MySQLdb TypeError: not all arguments converted during string formatting
(8 answers)
Closed 5 years ago.
I'm using Python CGI and MySQL (phpmyadmin). I'm trying to run this SQL statement in python: (tk_journey = 620 for this example - where the %s is)
SELECT tk_id, tk_seat_number FROM tickets
WHERE tk_journey = '%s'
ORDER BY tk_id DESC LIMIT 1;
It works perfectly in phpmyadmin and displays what it should (which is this):
tk_id tk_seat_number
94 2
However, when I run this through a python script it returns None/NoneType for each.
I've tried using this: SELECT MAX(tk_id), tk_seat_number FROM tickets WHERE tk_journey = '%s' but that gave the same result.
I tried to use %s without the quotes but it also generated an error - in mysql syntax. I also tried closing and opening the database connection - didn't work.
I've tried researching this problem a lot but to no help.
Here is the python code:
def getSeatNum(jid):
statement = '''SELECT tk_id, tk_seat_number FROM tickets
WHERE tk_journey = '%s'
ORDER BY tk_id DESC LIMIT 1;'''
cursor.execute(statement, jid)
row = cursor.fetchone()
seat_num = row[0]
return(seat_num)
seat_no = getSeatNum(jid)
jid = 620.
The db connection works fine as there are several other functions similar to this selecting/inserting information into tables, but this one seems to be failing?
Thanks in advance!
EDIT: I'm not sure if this is what I am meant to do. This got marked as a duplicate of this question: Python MySQL TypeError. I never got this TypeError. If the program ran without generating an error - it just returned 'NULL', which in turn affected a later program as the variable would just be 'NULL'. The errors I would get were the SQL syntax error as shown above and various Python errors when I was fiddling around to get it to work. Not this TypeError one.
The second argument to cursor.execute() must be a tuple containing the values to fill in all the placeholders in the query. Even if there's only one placeholder, you need a tuple. So it should be:
cursor.execute(statement, (jid,))

Adding data from one Postgresql server into another Postgresql server using Python

I'm pretty new to python and postgresql in general but I'm having problems transferring data from one server into another. Currently I have code to run where I pull data and set that to a variable IT, when I try inserting IT into another Postgresql server, I run into errors.
First I used:
cur = con.cursor()
#Connect cursor to local server
IT = DataPull()
#Pulls the data from the remote Postgresql server and set it equal to IT
command2 = (
"""
INSERT INTO gr_data.it (column_name1,column_name2,column_name3,column_name4,column_name5,column_name6,column_name7) VALUES(?,?,?,?,?,?,?)
""")
cur.execute(command2, IT)
But I end up getting the error:
psycopg2.ProgrammingError: syntax error at or near ","
LINE 2:...column_name4,column_name5,column_name6,column_name7 VALUES<?,?,?,?,?....
^
So I figured it had to do with the question marks, I googled around and found that maybe they should be changed to "%s". Then I received this error:
TypeError: not all arguments converted during string formatting
Any help?
Here's an example of how you should perform the insert using variables:
cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abc'def"))
It is taken from official docs you can find here

Categories

Resources