Connect to lan database using python - python

I have set up xampp to both of my PCs with success. When I try to connect to the localhost of PC 1 while I am on PC 1 through python, everything works fine.
But when I am trying to connect to PC 2 xampp sql I cant get a connection.
I have cofigured xampp to accept connections from other devices in the lan and I can access it perfectly through my web browser but not from my python programm.
This is my code:
def GetResults(self):
try:
cnx = mysql.connector.connect(user='test', password= '123456', host='paraliass-pc', port='3306', database='test')
cursor = cnx.cursor()
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your username or password")
self.statusBar().showMessage("Wrong Username or Password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
self.statusBar().showMessage("Database does not exist")
if cnx :
print("Connected")
self.statusBar().showMessage("Connected")
cursor = cnx.cursor()
cursor.execute("SELECT * FROM STAFF WHERE NAME LIKE '" + self.textEdit.text() + "%' OR SURNAME LIKE '" + self.textEdit.text() + "%'" + "OR RANK LIKE '" + self.textEdit.text() + "%'")
I am getting this error which does not appear when I am connecting to the same pc I am running the program on
UnboundLocalError: local variable 'cnx' referenced before assignment
By the way, if i change the username or password on the localhost to be wrong on purpose, I get the same error.

You should consider granting privileges to connect remotely to MySQL, and bind MySQL to use the IP address and port of your choosing. By default, MySQL will only listen for connections via localhost/socket.

Related

Connect to MySQL database using python

So I am having a super hard time connecting to a local database using the python mysql.connector module.
So I am trying to connect using the highlighted connection. I use the password abcdefghijkl to log into the SQL environment. I am trying to connect to a database named flight_school.
My python script looks like so.
import mysql.connector
mydb = mysql.connector.connect("localhost", "root", "abcdefghijkl", "flight_school")
print(mydb.is_connected())
This above code in the arguments in the following order i.e.
hostname = localhost,
user = 'root',
password = 'abcdefghijkl', and
database name = 'flight_school'.
It's just not working. I get the following error.
I would really appreciate some advice, please.
Please read always the official documentation
Your cooenction stirng has to have this form(if you do it this way=
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="testpaaword",
database="testdb"
)
Check out SQL-Alchemy module, works wonders from my experience.
Please read always the official documentation: https://dev.mysql.com/doc/connector-python/en/connector-python-connectargs.html
import mysql.connector
from mysql.connector import errorcode, MySQLConnection
try:
db_connection = MySQLConnection(user='root', password='', port='3306', database='your_database')
print("Database connection made!")
except mysql.connector.Error as error:
if error.errno == errorcode.ER_BAD_DB_ERROR:
print("Database doesn't exist")
elif error.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("User name or password is wrong")
else:
print(error)
else:
db_connection.close()
cursor = db_connection.cursor()
sql = ("commands")
cursor.execute(sql)

Can't connect to webserver mysql with Python, but it's works fine in Php

I'm trying to connect to my webserver MySQL (my domain MySQL server) with Python but I keep getting this error:
2013: Lost connection to MySQL server during query
import mysql.connector
from mysql.connector import errorcode
try:
cnx = mysql.connector.connect(user='myuser', password='mypass',
host='myhost',
database='mydb',
connect_timeout=10000)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()
Result: 2013: Lost connection to MySQL server during query
Connecting to this mysql server works fine in PHP.
Also connnecting to localhost MySQL (XAMPP) server works with Python too.
Any idea why I keep getting this error when im trying to connect with Python to webserver MySQL database?
The issue has been fixed, it turns out the database hosting got bad settings, so for some reason they did not receive my calls. It has been fixed by switching to a new host.

Connecting to Oracle Database Using Python

I am trying to connect to Oracle database using the following script;
import cx_Oracle
username = user
password = pwd
host = host
port = '1521'
service = service
dbconn = cx_Oracle.connect(username + '/' + password + '#'+ host + ':' + port + '/' + service)
But I got the following error;
TNS:Connect timeout occurred
Can anybody please suggest me what is going wrong here?
# importing module
import cx_Oracle
# Create a table in Oracle database
try:
con = cx_Oracle.connect('scott/tiger#localhost')
# Now execute the sqlquery
cursor = con.cursor()
# Creating a table srollno heading which is number
cursor.execute("create table student(srollno number, \
name varchar2(10), efees number(10, 2)")
print("Table Created successful")
except cx_Oracle.DatabaseError as e:
print("There is a problem with Oracle", e)
# by writing finally if any error occurs
# then also we can close the all database operation
finally:
if cursor:
cursor.close()
if con:
con.close()

Can't connect to mysql using python's mysql.connector

I'm using a Mac (OS 10.10.5), PyCharm, Python 3.5 and MySQL. MySQL has been working with PHP on the same machine. I'm trying to connect to it using Python and getting the error message:
enter code here2003: Can't connect to MySQL server on 'localhost::3306' (8 nodename nor servname provided, or not known)
Can someone list the diagnostic steps so I can correct the problem? Thanks, Doug
Below is the connection code:
import mysql.connector
from mysql.connector import errorcode
try:
cnn = mysql.connector.connect(
host="localhost:", # your host, usually localhost
user="root", # your username
password="root", # your password
database="bb_cards") # name of the data base
print("It Works!!")
except mysql.connector.Error as e:
if e.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with username or Password")
elif e.errno == errorcode.ER_BAD_DB_ERROR:
print("Database Does not exist")
else:
print(e)
You have a colon where there shouldn't be one:
host="localhost:" # remove the : -> host="localhost"
127.0.0.1::3306 is not the same as 127.0.0.1:3306
There are somethings I make out from your code.
we can provide host="localhost" no semicolons. or we can provide a host/server value like 'host':'127.0.0.1:<port name>'
Check your port again to connect the database
We should always close the connection viz. cnn.close() when our task is complete.

MySQL Connector in Python

Trying to connect to my MySQL DB on my VPS. I'm getting the following error, when using the below code;
(I've stripped out my credentials - however I know they work, as I use them for my PHP dev as well.
CODE
import mysql.connector as mysql
from mysql.connector import errorcode
try:
conn = mysql.connect( user= %USER%
,password=%PW%
,host = %HOST%
,database = %DB%
)
except mysql.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exists")
else:
print(err)
else:
conn.close()
ERROR MESSAGE
2003: Can't connect to MySQL server on '%HOST%:3306' (10060 A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond)
Connector That I'm using
http://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-2.0.1-py3.4.msi
Any idea what I'm doing wrong?
Check the GRANT on your MySQL database. It might be that you haven't been GRANTed permission to connect from that host with the given username and password.
The MySQL error number is 2003. I usually find it helpful to do a Google search to see if others have had the problem:
http://dev.mysql.com/doc/refman/5.0/en/access-denied.html
Is there a firewall between the client machine and the database server? I would hope that port 3306 would be blocked by default. If that's true, you'll need to set up a firewall rule to allow access from the client IP to the database IP via port 3306.

Categories

Resources