I'm upgrading a database application from Python 2.7 to 3.4. I originally used mysqldb, but I'm trying to switch to mysql-connector. My database connection fails with an internal TypeError. Here's the code and the error:
import mysql.connector
try:
dbh = mysql.connector.connect("localhost","pyuser","pypwd","jukebox")
except mysql.connector.Error as err:
print("Failed opening database: {}".format(err))
exit(1)
and here's the error:
# python loadcd.py
Traceback (most recent call last):
File "loadcd.py", line 12, in <module>
dbh = mysql.connector.connect("127.0.0.1","pyuser","pypwd","jukebox")
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/__init__.py", line 179, in connect
return MySQLConnection(*args, **kwargs)
File "/usr/local/lib/python3.4/dist-packages/mysql/connector/connection.py", line 57, in __init__
super(MySQLConnection, self).__init__(*args, **kwargs)
TypeError: __init__() takes 1 positional argument but 5 were given
I'm at a loss. The exact same connection works with mysqldb, and I can connect with the same credentials using PHP or at the command line.
You should provide keyword arguments:
dbh = mysql.connector.connect(host="localhost",user="pyuser",password="pypwd",database="jukebox")
Use this and replace the values as per your configuration:
import mysql.connector
mydb = mysql.connector.connect(host="localhost",
user="yourusername",
password="yourpassword")
print(mydb)
Related
I am receiving "TypeError: bad argument type for built-in operation" error when calling cursor.columns from pyodbc, Traceback the issue to threading.py -> currentThread function...
I have installed my python environment on a new HP Z2 desktop PC running :
Win10 enterprise x64
Python 2.7.13 on win32
PyScripter 3.5.1.0 x86
and suddenly received a very strange error while trying to insert data into my database using pyodbc module.
My code -
class clsDataBaseWrapper():
def __init__(self,ServerAddress='WIGIG-703\SQLEXPRESS',DataBase='QCT_Python',dBEn=1):
if (dBEn) :
self.DataBase=DataBase
self.cnxn = pyodbc.connect("Driver={SQL Server}"+
"""
;Server={0};
Database={1};
Trusted_Connection=yes;""".format(ServerAddress,DataBase))
self.cursor = self.cnxn.cursor()
else : print "\n*** Be Aware - you are not writing data to the DataBase !!! ***"
def InsertData(self,Table,Data,dBEn):
if (dBEn) :
columns = self.cursor.columns(table=Table, schema='dbo').fetchall()
insertQuery = "insert into {0} values ({1})".format(Table, ','.join('?' * len(columns)))
try :
self.cursor.execute(insertQuery, Data)
self.cnxn.commit()
except Exception,e : print 'could not insert data into DB - ',e
else : pass
The error I receive -
C:\Python27\lib\threading.py:1151: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
return _active[_get_ident()]
Traceback (most recent call last):
File "C:\Qpython\IsonM_TC2\Services_IsonM_TC2.py", line 335, in <module>
test.InsertData('PLL_CL_PN_PLL',[],1)
File "C:\Qpython\IsonM_TC2\Services_IsonM_TC2.py", line 87, in InsertData
columns = columns.fetchall()
File "C:\Qpython\IsonM_TC2\Services_IsonM_TC2.py", line 87, in InsertData
columns = columns.fetchall()
File "C:\Python27\lib\bdb.py", line 49, in trace_dispatch
return self.dispatch_line(frame)
File "C:\Python27\lib\bdb.py", line 67, in dispatch_line
self.user_line(frame)
File "<string>", line 126, in user_line
File "C:\Python27\lib\threading.py", line 1151, in currentThread
return _active[_get_ident()]
TypeError: bad argument type for built-in operation
This is very strange because it is working perfectly on other old PC I have running the same environment.
What do I miss here ? tried re-installing python, pyscripter ...
Inserting multiple MySQL records using Python
Error: Python 'tuple' cannot be converted to a MySQL type
ERROR CODE:
Traceback (most recent call last):
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 181, in to_mysql
return getattr(self, "_{0}_to_mysql".format(type_name))(value)
AttributeError: 'MySQLConverter' object has no attribute '_tuple_to_mysql'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in _process_params
res = [to_mysql(i) for i in res]
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 432, in <listcomp>
res = [to_mysql(i) for i in res]
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\conversion.py", line 184, in to_mysql
"MySQL type".format(type_name))
TypeError: Python 'tuple' cannot be converted to a MySQL type
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "python_mysql_2.py", line 22, in <module>
my_cursor.execute(mike_placeholders,records_list)
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 557, in execute
psub = _ParamSubstitutor(self._process_params(params))
File "C:\Users\POM\AppData\Local\Programs\Python\Python37-32\lib\site-packages\mysql\connector\cursor.py", line 437, in _process_params
"Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; Python 'tuple' cannot be converted to a MySQL type
Python Code:
#import sql.connector
import mysql.connector
#Create connection, added db we created#
connection = mysql.connector.connect(
host='localhost',
user='root',
password='123',
database='testdb_1'
)
#Create cursor for the connection
my_cursor = connection.cursor()
#Create SQL statement with placeholders and put in variable
mike_placeholders="INSERT INTO users (name,email,age) VALUES (%s, %s, %s) "
#Create list (array) of records
records_list = [('Tim','Tim#tim.com',32), ('Mary','Mary#mary.com',40), ('Sam','Sam#sam.com',50), ('Fred','Fred#fred.com',22) ]
#Execute cursor, requires SQl statement variable, record variable
my_cursor.execute(mike_placeholders,records_list)
#Commit the connection to make the change on the database
connection.commit()
Ahhh, I used the wrong Python term.
I should have used executemany when working with a tuple.
my_cursor.executemany(mike_placeholders,records_list)
You can't pass a list to my_cursor.execute(), you need to iterate over the list:
for values in records_list:
my_cursor.execute(mike_placeholders, values)
Or you could repeat the (%s, %s, %s) multiple times and do it all in a single query by flattening the list of tuples.
mike_placeholders="INSERT INTO users (name,email,age) VALUES " + ", ".join(["(%s, %s, %s)"] * len(records_list))
my_cursor.execute(mike_placeholders, sum(records_list))
use my_cursor.executemany(mike_placeholders,records_list)
If you have multiple elements which are saved in a list or tuple then use,
cursor.executemany(query,list) or cursor.executemany(query,tuple)
You must use a for loop and INSERT item by item
for x in records_list:
my_cursor.execute(mike_placeholders, x)
connstr = """Provider=Microsoft.SQLSERVER.CE.OLEDB.3.5;DataSource=first.sdf;"""
conn = adodbapi.connect(connstr)
cur = conn.cursor()
getresult="select * from ft"
cur.execute(getresult)
result=cur.fetchall()
How can i solve the following error?
Traceback (most recent call last):
File "e:\python1\sqlcompactdb\compact.py", line 7, in <module>
connection = adodbapi.connect(connection_string)
File "C:\Users\khan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\adodbapi\adodbapi.py", line 116, in connect
raise api.OperationalError(e, message)
adodbapi.apibase.OperationalError: (InterfaceError("Windows COM Error: Dispatch('ADODB.Connection') failed.",), 'Error opening connection to "Provider=Microsoft.SQLSERVER.CE.OLEDB.4.0; Data Source=E:\\python1\\sqlcompact\\first.sdf;"')
As the error implies, this issue stems from an error when the module tries to make an ADO database connection.
Specifically, when the following code executes
pythoncom.CoInitialize()
c = win32com.client.Dispatch('ADODB.Connection')
This is most likely due to hardware issues like the lack of the correct provider for the needed connection.
Solutions to a similar problem can be found at Connecting to SQLServer 2005 with adodbapi
While connecting to Hive2 using Python with below code:
import pyhs2
with pyhs2.connect(host='localhost',
port=10000,
authMechanism="PLAIN",
user='root',
password='test',
database='default') as conn:
with conn.cursor() as cur:
#Show databases
print cur.getDatabases()
#Execute query
cur.execute("select * from table")
#Return column info from query
print cur.getSchema()
#Fetch table results
for i in cur.fetch():
print i
I am getting below error:
File
"C:\Users\vinbhask\AppData\Roaming\Python\Python36\site-packages\pyhs2-0.6.0-py3.6.egg\pyhs2\connections.py",
line 7, in <module>
from cloudera.thrift_sasl import TSaslClientTransport ModuleNotFoundError: No module named 'cloudera'
Have tried here and here but issue wasn't resolved.
Here is the packages installed till now:
bitarray0.8.1,certifi2017.7.27.1,chardet3.0.4,cm-api16.0.0,cx-Oracle6.0.1,future0.16.0,idna2.6,impyla0.14.0,JayDeBeApi1.1.1,JPype10.6.2,ply3.10,pure-sasl0.4.0,PyHive0.4.0,pyhs20.6.0,pyodbc4.0.17,requests2.18.4,sasl0.2.1,six1.10.0,teradata15.10.0.21,thrift0.10.0,thrift-sasl0.2.1,thriftpy0.3.9,urllib31.22
Error while using Impyla:
Traceback (most recent call last):
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\Scripts\HiveConnTester4.py", line 1, in <module>
from impala.dbapi import connect
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\impala\dbapi.py", line 28, in <module>
import impala.hiveserver2 as hs2
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\impala\hiveserver2.py", line 33, in <module>
from impala._thrift_api import (
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\impala\_thrift_api.py", line 74, in <module>
include_dirs=[thrift_dir])
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\thriftpy\parser\__init__.py", line 30, in load
include_dir=include_dir)
File "C:\Users\xxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\thriftpy\parser\parser.py", line 496, in parse
url_scheme))
thriftpy.parser.exc.ThriftParserError: ThriftPy does not support generating module with path in protocol 'c'
thrift_sasl.py is trying cStringIO which is no longer available in Python 3.0. Try with python 2 ?
You may need to install an unreleased version of thrift_sasl. Try:
pip install git+https://github.com/cloudera/thrift_sasl
If you're comfortable learning PySpark, then you just need to setup the hive.metastore.uris property to point at the Hive Metastore address, and you're ready to go.
The easiest way to do that would be to export the hive-site.xml from the your cluster, then pass --files hive-site.xml during spark-submit.
(I haven't tried running standalone Pyspark, so YMMV)
I know that I need to "import MySQLdb" to connect to a MySQL database. But what is the name of the library that we need to import when we are using "cleardb mysql"?
I am hard coding as below to connect, but I guess due to wrong library, I am getting errors. Below are my points to explain my situation::
1) I have installed "MySQldb", and imported it through import keyword.
2) when I use port number in the connectivity syntax, I got "TypeError: an integer is required".
db = MySQLdb.connect("server_IP",3306,"uid","pwd","db_name")
so I removed the port number
import MySQLdb
db = MySQLdb.connect("server_IP","uid","pwd","db_name")
cur = db.cursor()
and the error vanishes. Is that the right method?
3) Everything goes fine until I execute the function "curson.execution("SELECT VERSION()")" to execute sql queries.
curson.execution("SELECT VERSION()")
I am getting error as below
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
cursor.execute("use d_7fc249f763d6fc2")
File "path_to\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "path_to\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (2006, 'MySQL server has gone away')
So, is this happening due to library that I have imported? or, if the library is correct, then what seems to be the issue?
The port number is the fifth positional argument, not the second.
db = MySQLdb.connect("server_IP", "uid", "pwd", "db_name", 3306)