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.
Related
Here is the full error
Traceback (most recent call last):
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/test.py", line 26, in <module>
handler()
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/test.py", line 17, in handler
cursor.execute('SELECT * FROM Reads')
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/cursors.py", line 148, in execute
result = self._query(query)
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/cursors.py", line 310, in _query
conn.query(q)
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/connections.py", line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/connections.py", line 775, in _read_query_result
result.read()
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/connections.py", line 1156, in read
first_packet = self.connection._read_packet()
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/connections.py", line 725, in _read_packet
packet.raise_for_error()
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "Desktop/Work/RDS_LAMDA_Stuff/RDS_Node_Stuff/pymysql/err.py", line 143, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Reads' at line 1")
For context here is my code
from multiprocessing import connection
import pymysql
from pymysql.cursors import Cursor
# RDS config
endpoint = '********'
username = '********'
password = '********'
database_name = '*********'
#connection config
connection = pymysql.connect(host=endpoint,user=username,passwd=password,db=database_name)
def handler():
cursor = connection.cursor()
cursor.execute('SELECT * FROM Reads')
rows = cursor.fetchall()
for row in rows:
print("{0} {1} {2} {3}".format(row[0], row[1], row[2], row[3]))
handler()
I have 2 other tables in my database which work fine when i query them however this table seems to be the only one causing an error.
Feel free to ask for more if this is too vague
It's worth checking manual in cases like this - and Reads is a reserved word therefore must be backticked - better still don't use reserved words or you will forever have to remember to backtick in code..
fixed: just using mysql.connector package now.
i am a few programming with python now and i wanted to create a use login/logout system with a database linked to a self created web platform for managment, logging, etc...
now i wanted to perform a query to get all users from my database but for some reason im not able to get any results i tried:
# as requested, connector method.
def initiate_connection(self):
return MySQLdb.connect("localhost", "root", "", "tester")
# This works !
def get_database_version(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT VERSION();")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
# This doesnt? :(
def get_users(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT name FROM users")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
But i get an uknown column error, so i tried selecting everything to see what i get from that result: Nonetype, Also ! i am able to retrieve version from database so i assume im connected properly.
Im pretty clueless in what im doing wrong here any ideas?
Also db structure is:
db->tester
table->users
- id
- name
- password
- salt
- email
Edit:
Actual error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 25, in <module>
print(database_handler().get_users())
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 18, in get_users
curs.execute("SELECT name FROM users")# Query command
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
res = self._query(query)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
db.query(q)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\connections.py", line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'name' in 'field list'")
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'
)
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'
)
I'm currently running a script to insert values (a list of tuples) into a MySQL database, using the execute many function. When I use a small number of rows (`1000), the script runs fine.
When I use around 40,000 rows, I receive the following errors:
cursor.executemany( stmt, trans_frame)
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2538, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-1-66b44e71cf5a>", line 1, in <module>
cursor.executemany( stmt, trans_frame)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 253, in executemany
r = self._query('\n'.join([query[:p], ',\n'.join(q), query[e:]]))
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 346, in _query
rowcount = self._do_query(q)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 310, in _do_query
db.query(q)
OperationalError: (2006, 'MySQL server has gone away')
Any suggestions?
sql ='SET GLOBAL max_allowed_packet=500*1024*1024'
cursor.execute(sql)
You could try setting the max_allowed_packet parameter just for one session:
sql ='SET SESSION max_allowed_packet=500M'
cursor.execute(sql)
sql = ...
args = ...
cursor.executemany(sql, args)
If this works, you could leave the code as it is, or change your my.cnf file (knowing that that solves the executemany problem).