Error Keyerror 255 when executing pymysql.connect - python

Here is the code
import pymysql
pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)
and the error Traceback was:
data is :::::b'\xff\x02\x00\xff\x81\x15'....##### I was add near line 1279 which is print("data is :::::%s...."%data[i:i+6])
Traceback (most recent call last):
File "C:\Users\123\Desktop\pymysqldebug.py", line 8, in <module>
charset='utf8'
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 709, in __init__
self.connect()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 934, in connect
self._get_server_information()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 1279, in _get_server_information
self.server_charset = charset_by_id(lang).name
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\charset.py", line 39, in by_id
return self._by_id[id]
KeyError: 255
seems like struct.unpack method parse '\xff\' to 255 and assigned to self.server_language, whatever the non-null charset argument passed.
Is this an MySQL version problem?(version 8.0.1-dmr)

To extend the above link-only accepted answer, consider the following changes on your current pymysql install. With MySQL 8, the mysql-python API does not recognize possibly newer character sets and hence the raised KeyError.
To resolve, locate the connectors.py script under the pymysql module found at this directory:
print(pymysql.__file__)
Backup orginal connectors.py script. Then incorporate the following changes.
Original (lines 1268-1269)
self.server_language = lang
self.server_charset = charset_by_id(lang).name
Replace (lines 1268 - 1272)
self.server_language = lang
try:
self.server_charset = charset_by_id(lang).name
except KeyError:
self.server_charset = None
Be careful to include hidden tabs in indentation with above lines.
Reference
PyMySQL Git fix #591

but where's your variable?
import pymysql
|
v
myVariable = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)

Related

Mysql database not connecting to python

I am trying to connect python to mysql by this code--
(I am using the mysql.connect library)
import mysql.connector
cnx = mysql.connector.connect(user='root',
password='password',
host='127.0.0.1',
database='db')
print(cnx)
cnx.close()
But it continues to throw the error--
Traceback (most recent call last):
File "D:/ted/main.py", line 5, in <module>
database='db')
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\__init__.py",
line 179, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 94, in __init__
self.connect(**kwargs)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\abstracts.py",
line 722, in connect
self._open_connection()
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 211, in _open_connection
self._ssl)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\connection.py",
line 141, in _do_auth
auth_plugin=self._auth_plugin)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py",
line 102, in make_auth
auth_data, ssl_enabled)
File "C:\Program Files (x86)\Python36-32\lib\mysql\connector\protocol.py",
line 58, in _auth_response
auth = get_auth_plugin(auth_plugin)(
File "C:\Program Files (x86)\Python36-
32\lib\mysql\connector\authentication.py", line 191, in get_auth_plugin
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin
'caching_sha2_password' is not supported
Process finished with exit code 1
I have started the mysql sever in workbench and the also checked the status.
Also database named "db" is also there
Also checked if the host is proper.
-->Removed ssl also.
It seems that your MySQL Server version is 8.x and in that case the default MySQL connector is caching_sha2_password. In the other hand your error is maybe because your python client connector does not support this Authentication Plugin and you should explicitly change the Authentication Plugin to the old one (mysql_native_password).
cnx = mysql.connector.connect(user='root', password='password',
host='127.0.0.1', database='db',
auth_plugin='mysql_native_password')

Pymysql connection KeyError 255 [duplicate]

Here is the code
import pymysql
pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)
and the error Traceback was:
data is :::::b'\xff\x02\x00\xff\x81\x15'....##### I was add near line 1279 which is print("data is :::::%s...."%data[i:i+6])
Traceback (most recent call last):
File "C:\Users\123\Desktop\pymysqldebug.py", line 8, in <module>
charset='utf8'
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 709, in __init__
self.connect()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 934, in connect
self._get_server_information()
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\connections.py", line 1279, in _get_server_information
self.server_charset = charset_by_id(lang).name
File "D:\Program Files (x86)\Python\Python35\lib\site-packages\pymysql\charset.py", line 39, in by_id
return self._by_id[id]
KeyError: 255
seems like struct.unpack method parse '\xff\' to 255 and assigned to self.server_language, whatever the non-null charset argument passed.
Is this an MySQL version problem?(version 8.0.1-dmr)
To extend the above link-only accepted answer, consider the following changes on your current pymysql install. With MySQL 8, the mysql-python API does not recognize possibly newer character sets and hence the raised KeyError.
To resolve, locate the connectors.py script under the pymysql module found at this directory:
print(pymysql.__file__)
Backup orginal connectors.py script. Then incorporate the following changes.
Original (lines 1268-1269)
self.server_language = lang
self.server_charset = charset_by_id(lang).name
Replace (lines 1268 - 1272)
self.server_language = lang
try:
self.server_charset = charset_by_id(lang).name
except KeyError:
self.server_charset = None
Be careful to include hidden tabs in indentation with above lines.
Reference
PyMySQL Git fix #591
but where's your variable?
import pymysql
|
v
myVariable = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='iDontWannaSay',
db='iDontWannaShow',
charset='utf8'
)

The used command is not allowed with this MySQL version when using Python

I am getting the following error message when I run this program on a mac. It works fine on the original Linux computer:
File "setupPpiDb.py", line 406, in modules
createDataBase(pdbsToAnalyzeWithChains)
File "setupPpiDb.py", line 202, in createDataBase
''' %(os.path.join(RESULTS_DIR, 'PerAtomDistance.csv')))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 412, in _query
rowcount = self._do_query(q)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/cursors.py", line 375, in _do_query
db.query(q)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/MySQLdb/connections.py", line 276, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1148, 'The used command is not allowed with this MySQL version')
I am running Python3 on a mac and downloaded the latest community download version MySQL Database. The complete command is the following:
cursor.execute('''
load data local infile '%s' into table interfaceDist fields terminated by ',' optionally enclosed by '"' lines terminated by '\n' ignore 1 lines (PDB,Chains,Chain,ResId,Symbol,Atom,MinDist);
''' %(os.path.join(RESULTS_DIR, 'PerAtomDistance.csv')))
I have read that people have issues the local infile command. However, it has a problem with the second line of the command and I connect to the database in the following way with local_infile=1:
MySQLdb.connect(host='localhost', user=USER, passwd=PASSWD, db=DB_NAME, local_infile=1)
Any ideas on how to fix the bug?

I need a hint for sqlite3

I am trying to retrieve values from db and I am stack from start because sqlite3 is unable to open the db file after I was giving the right path.
Version 1:
sqlite_file = os.environ.get('DATABASE_URL', 'sqlite:///db.sqlite')
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
Version 2:
sqlite_file = app.config['SQLALCHEMY_DATABASE_URI']
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
Where:
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL', 'sqlite:///db.sqlite')
In both situations the error is
Error on request:
Traceback (most recent call last):
File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 209, in run_wsgi
execute(self.server.app)
File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 197, in execute
application_iter = app(environ, start_response)
File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/ubuntu/workspace/avb/app.py", line 222, in <module>
conn = sqlite3.connect(sqlite_file)
sqlite3.OperationalError: unable to open database file
For sqlite3.connect() start out using the actual filename (e.g. example.db) as the argument.
Same for environ.get(): use the actual filename, not the sqlite:/// address.

Pypyodbc.DataError trouble with database

I am trying to figure out why doesn't my test programme in python work.
I can access the database just fine from MySql Workbench, and I think I did everything right with the programming part, I also went to Administrative Tools and added my database to ODBC Database Sources, here is my test programme, if anyone can figure out what's wrong:
import pypyodbc
conn = pypyodbc.connect("DSN=database")
def func():
l = []
cur = conn.cursor()
try:
cur.execute("SELECT foo FROM table")
except pypyodbc.DatabaseError:
pass
conn.commit()
for i in cur:
l.append(i)
conn.close()
cur.close()
func()
The Error I'm getting is:
Traceback (most recent call last):
File "D:/path/test.py", line 21, in <module>
func()
File "D:/path/test.py", line 14, in func
for i in cur.fetchall():
File "D:/path/test.py", line 1819, in fetchall
row = self.fetchone()
File "D:/path/test.py", line 1893, in fetchone
check_success(self, ret)
File "D:/path/test.py", line 986, in check_success
ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
File "D:/path/test.py", line 956, in ctrl_err
raise DataError(state,err_text)
pypyodbc.DataError: ('22018', '[22018] [MySQL][ODBC 5.3(a) Driver][mysqld-5.5.5-10.0.17-MariaDB]')
Error 22018 is an "invalid character" error. One common cause is trying to use the ANSI ("(a)") version of MySQL Connector/ODBC to retrieve Unicode data. In that case the solution is to use the Unicode ("(w)") version of the driver instead.

Categories