I am attempting to connect to SQL Server Management Studio. My code is below and returns the error: "init() got an unexpected keyword argument 'trusted'." I am using pymysql, would like to connect using Windows Authentication, and am running on 64 bit windows machine. Any help would be appreciated.
Edit - removed trusted. Error I am now receiving is 'No connection could be made because the target machine actively refused it'
import pymysql
import pymysql.cursors
conn = pymysql.connect(host = 'DESKTOP-6CIMC97')
Have you checked out the example on GitHub? trusted doesn't seem to be required.
https://github.com/PyMySQL/PyMySQL/blob/master/example.py
#!/usr/bin/env python
from __future__ import print_function
import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='mysql')
cur = conn.cursor()
cur.execute("SELECT Host,User FROM user")
print(cur.description)
print()
for row in cur:
print(row)
cur.close()
conn.close()
Related
I am learning how to do MySQL in python using the mysql.connector module. However, whenever I try creating a connection, I get the error "mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:3306' (10061 No connection could be made because the target machine actively refused it)".
I done the exact same as the example that I am using states, and it still isn't working.
Below is the two different type of the example provided:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
print(mydb)
and
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="myusername",
password="mypassword"
)
print(mydb)
I tried exactly what the example said, and it didn't work. I also tried using a different username and password, but kept on getting the same error.
Any idea what I can do to get the expected output which, according to the second example that I am referring to, is:
<mysql.connector.connection.MySQLConnection object ar 0x016645F0>
?
Thank you in advance
import mySQL.connector
Mybd=mysql.connector.connect(
host='myusername',
user='myusername',
password='mypassword',
database='mydatabase')
Cursor=mycon.cursor()
Cursor.execute('show databases')
Data=cursor.fetchall()
for row in data:
Print(row)
mycon.close()
I'm trying to connect to an oracle db using python through PyCharm, below is my code and a screenshot of the connection details
Code:
import cx_Oracle
try:
conn = cx_Oracle.connect('sys/123#//localhost:1521/XEPDB1')
except:
print("Connection Error")
exit()
Output
Connection Error
There are multiple ways to do that, either with SID or service name
SID :
import cx_Oracle
dsn_tns = cx_Oracle.makedsn('server', 'port', 'sid')
conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns)
Service name :-
import cx_Oracle
dsn_tns = cx_Oracle.makedsn('server', 'port', service_name='service_name')
conn = cx_Oracle.connect(user='username', password='password', dsn=dsn_tns)
You can refer to this documentation HERE
I'm trying to use import mysql.connector to SELECT * from a table with ~1.9million rows in a remote database.
When I run the same query using the mysql cli client everything works just fine so I don't believe the issue is network or serverside. I've tried increasing connection_timeout to ridiculous numbers, but I still run into mysql.connector.errors.InterfaceError: 2013: Lost connection to MySQL server during query
Any ideas what to try next?
import mysql.connector
mydb = mysql.connector.connect(
host="mysql.prod.example.com",
user="user",
password="pass",
database="db",
auth_plugin='mysql_native_password',
connection_timeout=1000000
)
mycursor = mydb.cursor(dictionary=True)
mycursor.execute("SELECT * FROM table")
update_list = []
for row in mycursor:
row_id = row["id"]
row_ip = row["ip"]
print("IP: "+row_ip)
newlist = [row_id,row_ip,"/128"]
update_list.append(newlist)
Switching from Python2 to 3 solved my problem.
- #!/usr/bin/python
+ #!/usr/bin/python3
I am having problems connecting to a remote Oracle DB using cx_Oracle in my python application.
I have tried a lot of different ways of configuring/formulating my connection string based on a lot of google searching etc but I seem to get the same error message almost each time no matter what I try.
My attempts looks like this:
import cx_Oracle
ip = '[IP ADDRESS]'
port = [PORT]
service_name = '[SERVICE NAME]'
dsn = cx_Oracle.makedsn(ip, port, service_name=service_name)
db = cx_Oracle.connect('[USERNAME]', '[PASSWORD], dsn)
Result: DatabaseError: ORA-12170: TNS:Connect timeout occurred
import cx_Oracle
conn_str = '[USERNAME]/[PASSWORD]#[HOST IP]/[SERVICE NAME]'
conn = cx_Oracle.connect(conn_str)
Result: DatabaseError: ORA-12170: TNS:Connect timeout occurred
import cx_Oracle
user= '[USERNAME]'
pwd = '[PASSWORD]'
host = '[HOST IP]'
service_name = '[SERVICE NAME]'
portno = '[PORT]'
conn = cx_Oracle.connect(user, pwd, '{}:{}/{}'.format(host,portno,service_name))
Result: DatabaseError: ORA-12170: TNS:Connect timeout occurred
import cx_Oracle
connstr = '[USERNAME]/[PASSWORD]#[SERVICE NAME]'
conn = cx_Oracle.connect(connstr)
Result: DatabaseError: ORA-12154: TNS:could not resolve the connect identifier specified
I have Toad installed on my PC and have no problems what so ever connecting to the DB with that.
Any ideas what could be the problem ?
Thanks in advance
I have this standard way to connect
import cx_Oracle
host="myserver"
port=myport
sid='myservicename'
user='myuser'
password='mypassword'
sid = cx_Oracle.makedsn(host, port, service_name=sid)
connection = cx_Oracle.connect(user, password, sid, encoding="UTF-8")
cursor = connection.cursor()
cursor.execute('select 1 from dual')
And it works without any issue. In your case, it is quite strange that you got a timeout, which normally indicates a network problem rather than a connection issue. If you are using cx_Oracle version 8, you don't need to specify the encoding as UTF-8 as it is the default one.
See how it works.
C:\python>type testconn.py
#from __future__ import print_function # needed for Python 2.7
import cx_Oracle
import os
host="myserver"
port=myport
sid='database_service_name'
user='myuser'
password='mypassword'
sid = cx_Oracle.makedsn(host, port, service_name=sid)
connection = cx_Oracle.connect(user, password, sid, encoding="UTF-8")
cursor = connection.cursor()
cursor.execute('select 1 from dual')
for row in cursor:
print(row)
C:\python>python testconn.py
(1,)
C:\python>
I am using mysql.connector to connect to a mysql DB, while i can connect manually to the db using sql server management, if i try connecting via code, it returns this error after awhile :
mysql.connector.errors.OperationalError: 2055: Lost connection to MySQL server at 'host:1234', system error: 10054 An existing connection was forcibly closed by the remote host
These are the connection details :
connection = mysql.connector.connect(host='host',
port = '1234',
database='DBname',
user='Usr',
password='pwd')
If I create a local mysql DB, the connection works just fine.
I assume some security stuff is going on, anyone else had encountered this situation ? Anything that I'm doing wrong? Should I add anything to the connection.connect input ?
Full code for reference :
import mysql.connector
from mysql.connector import Error
connection = mysql.connector.connect(host='host',
port = '1234',
database='DBname',
user='Usr',
password='pwd')
sql_select_Query = "select * from TableName"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
print("Total numb of rows selected is : ", cursor.rowcount)
print("\nPrinting each row")
for row in records:
print(row)
connection.close()