error using MySQLdb - python

I am newbie to python,I have simple code to connect database using MySQLdb and Python3.4 running in localhost.
Python Code:
#!c:/Python34/python.exe -u
import cgitb ,cgi
import sys
import MySQLdb
conn = MySQLdb.connect(host='localhost',port=3306,
db='example2',
user='root',
password='tiger')
cursor = conn.cursor()
if conn:
print("sucess")
But getting error ,while executing a code
Error:
Traceback (most recent call last):
File "<pyshell#27>", line 4, in <module>
password='tiger')
File "C:\Python34\lib\site-packages\MySQLdb\__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "C:\Python34\lib\site-packages\MySQLdb\connections.py", line 204, in __init__
super(Connection, self).__init__(*args, **kwargs2)
TypeError: an integer is required (got type str)
Please give some suggestion

You need to remove the single quotes from the port.
conn = MySQLdb.connect(host='localhost',
port=3306,
database='example2',
user='root',
password='tiger')

Related

Raspberry pi3 Mysql Python ERROR

Hi I'm trying to connect from python to MySQL but I can't apparently due to below error. I tried it with this...
import MySQLdb
cnx = MySQLdb.connect(user ='phpmyadmin',
passwd ='raspberry',
host ='192.168.0.58',
database ='freddy')
print 'connected'
cnx.close()
and then i changed "Mysqldb" to "Mysql.connector" but doesnt work. This is the error showing :
Traceback (most recent call last):
File "test.py", line 2, in
cnx = MySQLdb.connect(user='phpmyadmin',passwd='raspberry',host='192.168.0.58',database='freddy')
File "/usr/lib/python2.7/dist-packages/MySQLdb/init.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 204, in init
super(Connection, self).init(*args, **kwargs2)
TypeError: 'database' is an invalid keyword argument for this function
....
what should i do? i know my password its fine, but must be somethig else...
The word database should be db and then you should be fine and connect.
Change your loginpart into below code:
cnx = MySQLdb.connect(user ='phpmyadmin',
passwd ='raspberry',
host ='192.168.0.58',
db ='freddy')
This gives you oversight of used keys and values. Enjoy ;-)

Hive - python connection error

Here is code :
#!/usr/bin/env python
import pyhs2
try:
with pyhs2.connect(host='localhost',
port=10001,
authMechanism="PLAIN",
user='root',
password='test',
database='test') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()
#Execute query
cur.execute("select * from raw_stats")
#Return column info from query
print cur.getSchema()
#Fetch table results
for i in cur.fetch():
print i
except Thrift.TException, tx:
print '%s' % (tx.message)
Error!
Traceback (most recent call last): File "/usr/local/py/test.py", line
8, in database='default') as conn: File
"/usr/lib/python2.6/site-packages/pyhs2/init.py", line 7, in
connect
return Connection(*args, **kwargs) File "/usr/lib/python2.6/site-packages/pyhs2/connections.py", line 46, in
init
transport.open() File "/usr/lib/python2.6/site-packages/pyhs2/cloudera/thrift_sasl.py", line
55, in open
self._trans.open() File "/usr/lib64/python2.6/site-packages/thrift/transport/TSocket.py", line
101, in open
message=message) thrift.transport.TTransport.TTransportException: Could not connect to localhost:10001
It resolved by starting hiveServer2 service and changing the port 10000.

Python connect to 000webhost web server MySQL database

I am trying to connect Python to a web server's database, 000webhost I am using. Here is my code:
import MySQLdb
>>> conn = MySQLdb.connect(host="mysql9.000webhost.com",user="user_name",passwd="password",db="db_name")
I think I install MySQLdb successfully. And all the username, password, host, database name are exactly the same as they are in my php file, which works perfectly. However, I got this error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
conn = MySQLdb.connect(host="mysql9.000webhost.com",user="a6969519_123",passwd="lianshiyu08",db="a6969519_shiyu")
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
super(Connection, self).__init__(*args, **kwargs2)
OperationalError: (2003, "Can't connect to MySQL server on 'mysql9.000webhost.com' (60)")
What did I miss?
The database is only reachable from the internal web of your hoster.

error when using mysql.connector for python to connect database

I am newbie to python ,started with implementing small programs ,now trying with connecting database .I installed python 3.4 and mysql.connector 2.0.4 .
Below given code is what i used to connnect the database
#!"C:\python34\python.exe"
import sys
import mysql.connector
print("Content-Type: text/html;charset=utf-8")
print()
conn = mysql.connector.connect(host='localhost:8051',
database='test',
user='root',
password='tiger')
if conn.is_connected():
print('Connected to MySQL database')
But i am getting error as given below . not getting why this error are occurring ,because of setup environment is wrong or of some other reason
Please suggest
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 448, in open_connection
socket.SOL_TCP)
File "C:\Python34\lib\socket.py", line 533, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11004] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#8>", line 2, in <module>
password='tiger',database='test')
File "C:\Python34\lib\site-packages\mysql\connector\__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 95, in __init__
self.connect(**kwargs)
File "C:\Python34\lib\site-packages\mysql\connector\abstracts.py", line 719, in connect
self._open_connection()
File "C:\Python34\lib\site-packages\mysql\connector\connection.py", line 209, in _open_connection
self._socket.open_connection()
File "C:\Python34\lib\site-packages\mysql\connector\network.py", line 464, in open_connection
errno=2003, values=(self.get_address(), _strioerror(err)))
mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on 'localhost:8051:3306' (11004 getaddrinfo failed)
The error message is quite clear:
Can't connect to MySQL server on 'localhost:8051:3306'
See the double port definition?
host='localhost:8051'
should most likey be
host='localhost', port='8051'
You are using the connection paramater wrong. I think you need to do this:
conn = mysql.connector.connect(host='localhost',
port=8051,
database='test',
user='root',
password='tiger')
Check out this doc for more detail.
The default Port for MySQL is 3306 , when you don't pass "port" argument to mysql.connector.connect it assumes you are using default port which is 3306 , if you use another port you should pass :
port=xxxx
to the constructor.
For me, I added all connector on my IDE.
mysql-connector
mysql-connector-async-dd
mysql-connector-python
mysql-connector-python-dd
mysql-connector-repackaged
then you can see your all database names here:
import mysql.connector
mydb = mysql.connector.connect(host='localhost',user='root', passwd = 'Root#1234')
mycursor = mydb.cursor()
mycursor.execute("show databases")
for i in mycursor:
print(i)

Python: unable connect with database

Python 3.4 with psycopg2
I used this guide to set up a basic psycopg2 connection like so:
#!/usr/bin/python
import psycopg2
import sys
import pprint
def main():
conn_string = "dbname='CIBTST' host='XX.XX.XXX.XX' port='XXXX' user='XXXXX' password='XXXX'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("My_select")
records = cursor.fetchall()
pprint.pprint(records)
if __name__ == "__main__":
main()
I get this traceback:
Traceback (most recent call last):
File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 14, in <module>
main()
File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 8, in main
conn = psycopg2.connect(conn_string)
File "C:\Python34\lib\site-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError
When I try the same select statement in another client (Toad) it seems to operate correctly. Any suggestions will be great. Thank you!
EDIT: The source of problem is: PORT. If I run:
#!/usr/bin/python
import psycopg2
import sys
import pprint
def main():
conn_string = "dbname='CIBTST' host='XX.XX.XXX.XX' user='XXXXX' password='XXXX'"
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
cursor.execute("My_select")
records = cursor.fetchall()
pprint.pprint(records)
if __name__ == "__main__":
main()
Then I get:
Traceback (most recent call last):
File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 14, in <module>
main()
File "C:\Users\pi24926\Desktop\Python\doSMS.py", line 8, in main
conn = psycopg2.connect(conn_string)
File "C:\Python34\lib\site-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "XXXXXXX" and accepting
TCP/IP connections on port 5432?
I can only connect under port 1522 for some reason. Any ideas?
The problem was with database...
It is an Oracle, so i should use cx_Oracle instead of psycopg2.

Categories

Resources