I tried all possible means. I included backQuotes for the string as suggested some in stack but nothing worked. It repeats the error as usual.
I also tried some queries that worked in other python files still it shows the same. I also tried queries with string without hyphens even it didn't work. I cant find out whats the problem here.
import MySQLdb
import sys
from PyQt4 import QtCore, QtGui, uic
qtCreatorFile = "Studisplay.ui" # Enter file here.
Ui_MainWindow1, QtBaseClass = uic.loadUiType(qtCreatorFile)
class stuDisplay(QtGui.QMainWindow, Ui_MainWindow1,QtGui.QTableWidget):
def __init__(self,ID):
#super(stuDisplay, self).__init__(parent)
QtGui.QMainWindow.__init__(self)
Ui_MainWindow1.__init__(self)
QtGui.QWidget.__init__(self)
self.setupUi(self)
obj = MySQLdb.connect("localhost", "root", "1234567", "python")
#The value of ID here is 14-VEC-244 I also tried `14-VEC-244` but did not work
sql = 'SELECT MEMname FROM Borrowed WHERE MemberID ='+ str(ID)
cursor = obj.cursor()
cursor.execute(sql)
name=cursor.fetchone()
print name
I get this error:
Traceback (most recent call last): File
"/home/gautham/PycharmProjects/LIBALERT/Login.py", line 105, in
pushButton_clicked
self.call = StuSecond.stuDisplay(StuID) File "/home/gautham/PycharmProjects/LIBALERT/StuSecond.py", line 22, in
init
cursor.execute(sql) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 226, in
execute
self.errorhandler(self, exc, value) File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in
defaulterrorhandler
raise errorvalue
_mysql_exceptions.OperationalError: (1054, "Unknown column 'VEC' in 'where clause'")
You're passing a string into your WHERE clause, so it must be quoted within the query string that gets passed to the database, something like so:
sql = "SELECT MEMname FROM Borrowed WHERE MemberID = '" + str(ID) + "'"
so that the finished string looks like
sql = "SELECT MEMname FROM Borrowed WHERE MemberID = '14-VEC-244'"
(Note that the single quotes are "forward" quotes, not backticks.)
This would also be an excellent application for a prepared statement; unfortunately I am not familiar with pyqt and so cannot advise you there.
Related
I am trying to insert the dictionary which is retrieved from a rest API into Snowflake column of variant datatype using the below python script
from sqlalchemy import create_engine
import urllib
import requests
import json
engine = create_engine(
'snowflake://{user}:{password}#{account_identifier}/'.format(
user='UserName',
password='pwd',
account_identifier='account_Identifier'
)
)
jval={"title":"value", "address":[{ "Road":"st xavier's","landmark":"D' Pauls"}]}
try:
connection = engine.connect()
results = connection.execute(
"INSERT INTO db_name.schema_name.sqlalchemy(jval) (select PARSE_JSON('" + json.dumps(jval) + "'))").fetchone()
print(results[0])
finally:
connection.close()
engine.dispose()
But ending up with the following error
File "C:\Users\PycharmProjects\snowflake\venv\lib\site-packages\snowflake\connector\errors.py", line 207, in default_errorhandler
raise error_class(
sqlalchemy.exc.ProgrammingError: (snowflake.connector.errors.ProgrammingError) 001003 (42000): SQL compilation error:
syntax error line 1 at position 133 unexpected 's'.
syntax error line 1 at position 134 unexpected '", "'.
[SQL: INSERT INTO db_name.schema_name.sqlalchemy(jval) (select PARSE_JSON('{"title": "value", "address": [{"Road": "st xavier's", "landmark": "D' Pauls"}]}'))]
(Background on this error at: https://sqlalche.me/e/14/f405)
I take it this is causing due to the single quote (') present in the string. how to handle this error? the same error comes even when using snowflake.connector library as well.
Use parameter binding so that the quoting is handled automatically.
from sqlalchemy import text
...
results = connection.execute(
text(
'INSERT INTO db_name.schema_name.sqlalchemy(jval) (select PARSE_JSON(:some_json))'
),
{'some_json': jval},
).fetchone()
The connection is fine, but the query sentence seems problematic.
query1 = """SELECT * FROM `DATABASE` WHERE `coin` = 'LTC'"""
query2 = """SELECT * FROM `DATABASE` WHERE `coin` = 'LTC' AND `date` > '2019-01-01 15:06:23'"""
And then
import pandas as pd
result = pd.read_sql(query, connection)
It works perfectly fine with query1 but gives such error for query2:
result = pd.read_sql(query, connection)
Traceback (most recent call last):
File "<ipython-input-25-c7c27cfd9a6b>", line 1, in <module>
result = pd.read_sql(query, connection)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 381, in read_sql
chunksize=chunksize)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1413, in read_query
cursor = self.execute(*args)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1386, in execute
raise_with_traceback(ex)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\compat\__init__.py", line 404, in raise_with_traceback
raise exc.with_traceback(traceback)
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pandas\io\sql.py", line 1382, in execute
self.con.rollback()
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pymysql\connections.py", line 808, in rollback
self._execute_command(COMMAND.COM_QUERY, "ROLLBACK")
File "C:\Users\luzhe\Anaconda3\lib\site-packages\pymysql\connections.py", line 1122, in _execute_command
raise err.InterfaceError("(0, '')")
DatabaseError: Execution failed on sql: SELECT * FROM `DATABASE` WHERE `coin` = 'LTC' AND `date` > '2019-01-01 15:06:23'
(0, '')
unable to rollback
I want to know what this "unable to rollback" means and how to solve this multi-condition selection in PyMySQL.
unable to rollback
It means your query has not been successfully executed.
An unclosed connection is usually the cause for that error. You might not have closed the previous connection. You can usually do so with the close method associated with the connection instance.
Started learning mySQL and got stuck on why this command is not working. I had success with UPDATE commands and SELECT * outside the function so I am guess I am making a mistake in calling the function or perhaps the %s needs to be different... My google foo did not find anything so I hope you all can help me!
Thank you so much for looking!
CODE:
def CheckBalance(UserName, BetAmount): #checks to make sure they can afford the bet. Returns 0 for no 1 for yes
import mysql.connector
cnx = mysql.connector.connect(user='root', password='Password',
host='127.0.0.1',
database='crapsdatabase')
c = cnx.cursor()
BankRoll = c.execute("SELECT PlayerBank FROM player WHERE PlayerName = %s", UserName)
if(BankRoll < BetAmount) or (BetAmount < 0):
c.close()
return 0
if(BankRoll >= BetAmount):
c.close()
return 1
From our main program I import the UpdateDatabase and call it
from plugins.database.UpdateDatabase import UpdateBets
a = UpdateBets.CheckBalance("bob", 100)
print(a)
This gives the following error:
C:\python\python.exe C:/Users/Ray/Desktop/bot/plugins/CRAPS/CrapsUpdated.py
Traceback (most recent call last):
File "C:/Users/Ray/Desktop/bot/plugins/CRAPS/CrapsUpdated.py", line 3, in <module>
a = UpdateBets.CheckBalance("bob", 100)
File "C:\Users\Ray\Desktop\bot\plugins\database\UpdateDatabase.py", line 16, in CheckBalance
BankRoll = c.execute("SELECT PlayerBank FROM player WHERE PlayerName = %s", UserName)
File "C:\python\lib\site-packages\mysql\connector\cursor.py", line 515, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "C:\python\lib\site-packages\mysql\connector\connection.py", line 488, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "C:\python\lib\site-packages\mysql\connector\connection.py", line 395, in _handle_result raise errors.get_exception(packet)
mysql.connector.errors.ProgrammingError: 1064 (42000): 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 '%s' at line 1
You should escape string literal in the query with apostrophes, so it should be like this:
c.execute("SELECT PlayerBank FROM player WHERE PlayerName = '%s'", UserName)
I'm trying to run a function (f) every x seconds (in my case 60) which will close an active database connection if one exists and upon completion opens it again.
I am using threading.timer although I'm having trouble passing the connection into the function, and in some situations the function runs repeatedly with nothing else running.
The function needs to return the connection to globals after it completes and I'm finding it hard to pass the connection to the function and assign the return globally from within the function which is how I believe the threading.timer works:
enter code from socketIO_client import SocketIO
import logging
import json
import MySQLdb as mdb
import os
import threading
con = mdb.connect('localhost','username','password','databaseName')
cur = con.cursor()
def f(con):
if 'con' in globals():
con.close()
print ("Connection closed")
os.system('php -f /home/ubuntu/grab.php')
con = mdb.connect('localhost','username','password','databaseName')
cur = con.cursor()
print ("DB Connection opened")
con = mdb.connect('localhost','username','password','databaseName')
cur = con.cursor()
threading.Timer(60,f,con).start(); ######PROBLEM LINE
return con
def on_connect():
print "Connecting to database"
areas = ['EH','BE']
socketIO.emit('subscribe_areas', areas)
def on_message(answer):
print("\nNew message received")
array = (json.loads(answer))
print (array)
runningIdentity = array["value"]
berthID = array["to"]
area = array["area"]
if berthID:
query = ("SELECT crs FROM signalBerth WHERE signalBerth=\'%s\';"%(berthID))
cur.execute(("%s")%(query))
reply = cur.fetchall()
for row in reply:
crs= row[0]
query = "UPDATE service SET lastSeen = \'%s\' WHERE runningIdentity=\'%s"%(crs,runningIdentity)+"\';" #berthID == crs, need to alter
print (("%s")%(query))
cur.execute(("%s")%(query))
con.commit()
print("affected rows = {}".format(cur.rowcount))
socketIO = SocketIO('http://www.realtimetrains.co.uk', 41280) #opens connection
socketIO.on('connect', on_connect) #sends subscription
socketIO.on('message', on_message) #reads data, creates mysql and executes it
con = f(con) ######FIRST CALL TO FUNCTION
socketIO.wait() #Keeps connection openhere
Error:
Traceback (most recent call last): File "input.py", line 49, in
socketIO.wait() #Keeps connection open File "build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 175,
in wait File
"build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 194,
in _process_events File
"build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 202,
in _process_packet File
"build/bdist.linux-x86_64/egg/socketIO_client/init.py", line 327,
in _on_event File "input.py", line 36, in on_message
cur.execute(("%s")%(query)) File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 155, in
execute
charset = db.character_set_name()
_mysql_exceptions.InterfaceError: (0, '') Exception in thread Thread-1: Traceback (most recent call last): File
"/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run() File "/usr/lib/python2.7/threading.py", line 1082, in run
self.function(*self.args, **self.kwargs) TypeError: f() argument after * must be a sequence, not Connection
Perhaps there is a more suited method to my needs, however the important bit it that the connection is closed, the function run and the connection opened again every minute or so. Thought about a cron job, but I'd rather keep my code doing everything.
According to Timer object, its third parameter is args. It is a list, but you pass only the con instead.
You need to replace your problem line with:
threading.Timer(60, f, (con,)).start()
I am working on a project that loads local data onto a local server using SQLAlchemy and a pyramid framework. The following describes the setup:
-The local data are divided in multiple CSV files
-A separate SQL object (called DataSource) is created from each individual CSV file
-The parent of all DataSources is also a DataSource. Thus all CSV data are children
So, the parent DataSource is created, then all the children DataSources are created and linked to the parent. A separate function checks if the DataSource already exists. If not, it is created.
So, firstly we create the parent DS:
sName = 'Parent DS'
oDS = fetch_or_create_data_source(name=sName,author="Parent_DS_author"
)
Then we loop through all CSVs and create the children DS:
for sCSV_name in lCSVs:
oChildDS = fetch_or_create_data_source(name=sCSV_name,parent=oDS)
Here is the problem:
fetch_or_create_data_source creates a keyerror on the second iteration of CSV loading. So the parent and the first child DataSources are created without a problem. The error log below is created on the second child DS. A new DS should be created, since no there exists no DS with the given parent and name. All CSV filenames are unique.
Can anyone see why I get a mysterious key error on the second iteration of the DS creation? I could not find anything on the webs.
(EDIT): I know the error is created in lDS = list(oDS.filter_by(name=name).filter_by(parent=parent)), but I do not understand why, especially after the first two times it runs without a problem. I changed the code so that the parent_id is called instead of the parent object, but the KeyError persists. The error is caused by any filter_by() statement. To check for primitive values, I added some print statements to check the arguments in fetch_or_create_data_source. The results:
name = 'Parent DS' type <type 'str'>
author = 'Parent_DS_author' type <type 'str'>
parent = None
Current DS id = '352' type <type 'int'>
.
.
name = 'tab19' type <type 'str'>
author = '' type <type 'str'>
parent = <pyramidapp.models.DataSource object at 0x7fcb28066b50>
parent_id = 352 type <type 'int'>
Current DS id = '353' type <type 'int'>
.
.
name = 'tab42' type <type 'str'>
author = '' type <type 'str'>
parent = <pyramidapp.models.DataSource object at 0x7fcb28066b50>
parent_id = 352 type <type 'int'>
When I explicitely state that oDS is None, the key error originates from DBSession.flush() in the 3rd iteration.
Why would the first two instances of fetch_or_create_data_source run without problems, but the third not?
Code and traceback:
fetch_or_create_data_source:
def fetch_or_create_data_source(name,parent=None,author=""):
from pyramidapp.models import DBSession,DataSource
oDS = DBSession.query(DataSource)
print "name = '{0}' type {1}".format(name,type(name))
print "author = '{0}' type {1}".format(author,type(author))
print "parent = {0}".format(parent)
if parent:
print "parent_id = {0} type {1}".format(parent.id,type(parent.id))
if parent is None:
lDS = list(oDS.filter_by(name=name).filter_by(parent_id=parent))
else:
lDS = list(oDS.filter_by(name=name).filter_by(parent_id=parent.id)) <=== Key error from here
if len(lDS)==0:
oDS = None
else:
oDS = lDS[0]
if not oDS:
oDS = DataSource()
oDS.name = name
if parent:
oDS.parent = parent
if parent.author:
oDS.author = parent.author
if author:
oDS.author = author
DBSession.add(oDS)
DBSession.flush()
print "Current DS id = '{0}' type {1}".format(oDS.id,type(oDS.id))
return oDS
Pyramidapp.models.DataSource:
class DataSource(Base):
"""
this represents a release of data. It may be a group of documents
or a single spreadsheet
"""
__tablename__ = 'data_sources'
id = Column(Integer,Sequence('data_src_seq'),primary_key=True)
name = Column(String(100))
notes = Column(Text)
title = Column(String(100))
author = Column(String(100))
parent_id = Column(Integer,ForeignKey('data_sources.id'))
parent = relationship('DataSource',backref=backref("children",cascade="all, delete"),remote_side=[id,])
.
.
...
Traceback (most recent call last):
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/pyramid_debugtoolbar-2.0.2-py2.7.egg/pyramid_debugtoolbar/panels/performance.py", line 55, in resource_timer_handler
result = handler(request)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/tweens.py", line 21, in excview_tween
response = handler(request)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 82, in tm_tween
reraise(*exc_info)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/pyramid_tm-0.7-py2.7.egg/pyramid_tm/__init__.py", line 63, in tm_tween
response = handler(request)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/router.py", line 163, in handle_request
response = view_callable(context, request)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/config/views.py", line 355, in rendered_view
result = view(context, request)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/pyramid-1.5b1-py2.7.egg/pyramid/config/views.py", line 477, in _class_requestonly_view
response = getattr(inst, attr)()
File "/home/roman/Critical_ID/big_data/pyramidapp/pyramidapp/views/basic_views.py", line 108, in run_ds_script
oScript.run(dSettings)
File "/home/roman/Critical_ID/big_data/pyramidapp/pyramidapp/scripts/data_source_specific/load_prescriptions_dispensed_in_the_community_data.py", line 82, in run
oChildDS = fetch_or_create_data_source(name=sCSV_name,parent=oDS)
File "/home/roman/Critical_ID/big_data/pyramidapp/pyramidapp/tools/data_source_script_tools.py", line 22, in fetch_or_create_data_source
lDS = list(oDS.filter_by(name=name).filter_by(parent_id=parent.id))
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/query.py", line 2397, in __iter__
self.session._autoflush()
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/session.py", line 1184, in _autoflush
self.flush()
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/session.py", line 1879, in flush
self._flush(objects)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/session.py", line 1997, in _flush
transaction.rollback(_capture_exception=True)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/util/langhelpers.py", line 57, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/session.py", line 1961, in _flush
flush_context.execute()
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/unitofwork.py", line 370, in execute
rec.execute(self)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/unitofwork.py", line 523, in execute
uow
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 64, in save_obj
mapper, table, insert)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 594, in _emit_insert_statements
execute(statement, params)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 717, in execute
return meth(self, multiparams, params)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/sql/elements.py", line 317, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 814, in _execute_clauseelement
compiled_sql, distilled_params
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 927, in _execute_context
context)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1079, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/engine/base.py", line 920, in _execute_context
context)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/SQLAlchemy-0.9.3-py2.7-linux-x86_64.egg/sqlalchemy/engine/default.py", line 425, in do_execute
cursor.execute(statement, parameters)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/PyMySQL-0.6.1-py2.7.egg/pymysql/cursors.py", line 93, in execute
escaped_args = tuple(conn.escape(arg) for arg in args)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/PyMySQL-0.6.1-py2.7.egg/pymysql/cursors.py", line 93, in <genexpr>
escaped_args = tuple(conn.escape(arg) for arg in args)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/PyMySQL-0.6.1-py2.7.egg/pymysql/connections.py", line 698, in escape
return escape_item(obj, self.charset)
File "/home/roman/Critical_ID/big_data/lib/python2.7/site-packages/PyMySQL-0.6.1-py2.7.egg/pymysql/converters.py", line 24, in escape_item
encoder = encoders[type(val)]
KeyError: <class 'pyramidapp.models.DataSource'>
If you start from the very end of the traceback and go a few lines up, you'll see that the error is raised in the PyMySQL lib when it tries to escape the value - supposedly it has some kind of a dict called encoders which contains a mapping of encoders. It looks like it fails when passed an instance if pyramidapp.models.DataSource class.
Surely, PyMySQL knows nothing about SQLAlchemy declarative classes and can only escape primitive types (ints, strings etc). So it's kinda unexpected it even sees the DataSource instance instead.
From your code samples it's not quite clear what causes the problem, but my best guess would be that you're either assigning a class instance to an attribute which is supposed to hold a primitive value, or maybe using a class instance in a query where it expects a primitive value. Something like this:
ds = DataSource(...)
other_ds.parent_id = ds # instead of .parent
or
session.query(DataSource).filter(DataSource.parent_id==ds)
Actually, here it is:
lDS = list(oDS.filter_by(name=name).filter_by(parent=parent))