I am trying to pass a variable in mysql Query but its keep giving error.
I am referign to this
https://dev.mysql.com/doc/connector-python/en/connector-python-example-cursor-select.html
mydb = mysql.connector.connect(
host="111.222.333.444.555",
user="user",
passwd="pass",
database="database_aname"
)
#print(mydb)
mycursor = mydb.cursor()
mycursor = mydb.cursor(buffered=True)
#lon=((data['hits']['hits'][i]['_source']['#timestamp']))
query = ("SELECT * from device_stock join customers on device_stock.user_id=customers.id WHERE device_stock.imei=%s")
#query = ("SELECT * from device_stock join customers on device_stock.user_id=customers.id WHERE device_stock.imei=0351777090784867")
mycursor.execute(query,(imei))
for row in mycursor:
print (row)
but it gives bellow error
Traceback (most recent call last):
mycursor.execute(query,(imei))
File "C:\Program Files\Python37\lib\site-packages\mysql\connector\cursor_cext.py", line 248, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "C:\Program Files\Python37\lib\site-packages\mysql\connector\connection_cext.py", line 537, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
What can I do?
Thanks
try
mycursor.execute(query,(imei,))
(imei) is not taken as a tuple.
Related
I was trying to connect and make tables in database using python. I was using pycharm for that. I successfully connect pycharm with SQLyog database.
import pymysql
def CreateConn():
return pymysql.connect(host="localhost",database="myfirstDB",user="root",password="",port=3306)
CreateConn()
but as I was trying to make table by code , it shows some lines of error which I don't understand. I changed SQL database engine to SQLite and also tried to changed IDE to jupyter, still it shows error don't know why.
I tried below code for table creation in SQLyog;
def CreateTable():
conn=CreateConn()
cursor=conn.cursor() #helping to execute your query
query="create table student(sid int primary key auto_increment,name VARCHAR(50),email VARCHAR(50),city VARCHAR(50)"
cursor.execute(query)
conn.commit()
print("table created")
conn.close()
CreateTable()
I expected below table in SQLyog database;
expected result of above code in SQLyog database using pycharm
what is got as a result is below error lines;
Traceback (most recent call last):
File "C:\Users\asus\PycharmProjects\pythonProject\Database\Database.py", line 29, in <module>
CreateTable() #CALLING CREATE TABLE FUNCTION
File "C:\Users\asus\PycharmProjects\pythonProject\Database\Database.py", line 24, in CreateTable
cursor.execute(query)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\cursors.py", line 148, in execute
result = self._query(query)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\cursors.py", line 310, in _query
conn.query(q)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 548, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 775, in _read_query_result
result.read()
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 1156, in read
first_packet = self.connection._read_packet()
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\connections.py", line 725, in _read_packet
packet.raise_for_error()
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\pymysql\protocol.py", line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File "C:\Users\asus\PycharmProjects\pythonProject\venv\lib\site-packages\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 '' at line 1")
if (constant.gc in file.sheet_names):
coll = db[constant.gc]
print("Adding to " + constant.gc + " database")
df = file.parse(constant.gc)
df = clean(df)
data_dict = df.to_dict('r')
try:
result = coll.insert_many(data_dict)
nr_inserts = len(result.inserted_ids)
print(str(nr_inserts) + "Cases added to database")
except pymongo.errors.BulkWriteError as bwe:
nr_inserts = bwe.details["nInserted"]
print(nr_inserts)
I keep getting a NaTType error and I can't find which row of the dataframe has the blank date. Unfortunately, it's off of a 39k row Excel file. So, just looking through isn't going to help. I tried an except that theoretically could tell me how many were successfully inserted until the error, and therefore give me a hint on where to look, but it hasn't printed.
The error looks like this:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python38\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "dataimport.py", line 71, in importFromExcel
result = coll.insert_many(data_dict)
File "C:\Python38\lib\site-packages\pymongo\collection.py", line 758, in insert_many
blk.execute(write_concern, session=session)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 511, in execute
return self.execute_command(generator, write_concern, session)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 345, in execute_command
client._retry_with_session(
File "C:\Python38\lib\site-packages\pymongo\mongo_client.py", line 1384, in _retry_with_session
return func(session, sock_info, retryable)
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 339, in retryable_bulk
self._execute_command(
File "C:\Python38\lib\site-packages\pymongo\bulk.py", line 295, in _execute_command
result, to_send = bwc.execute(ops, client)
File "C:\Python38\lib\site-packages\pymongo\message.py", line 898, in execute
request_id, msg, to_send = self._batch_command(docs)
File "C:\Python38\lib\site-packages\pymongo\message.py", line 890, in _batch_command
request_id, msg, to_send = _do_bulk_write_command(
File "C:\Python38\lib\site-packages\pymongo\message.py", line 1382, in _do_bulk_write_command
return _do_batched_op_msg(
File "C:\Python38\lib\site-packages\pymongo\message.py", line 1307, in _do_batched_op_msg
return _batched_op_msg(
File "pandas\_libs\tslibs\nattype.pyx", line 64, in pandas._libs.tslibs.nattype._make_error_func.f
ValueError: NaTType does not support utcoffset
At a guess, ValueError and BulkWriteError are not the same, so nInserted never prints. Does anyone have an idea of how to get the number of successful inserts before the failure?
I doubt that any inserts are performed as the error is likely occurring before the data is passed to mongodb to insert.
In any case, if you want to hunt down which row in the dataframe has the NaT value, try: (subsitute 'date' for your column containing the date)
null_df = df[pd.isnull(df['date'])]
print(null_df)
To remove null dated items use:
df = df[pd.notnull(df['date'])]
It seems like all no error in the code but no idea why I'm getting this.
I was creating a simple GUI app which store user details to a database(postgresql) and also they will be able to search for entries in database. This particular error ocures in this search() function so I haven't added the rest of the code. If necessary I can add them.
Hope I will get some solutions from this community.
def search(id):
conn = psycopg2.connect(dbname="postgres",user="postgres",password="1018",host="localhost",port="5432")
mycursor = conn.cursor()
query = '''select * from demotab where id=%s '''
mycursor.execute(query, (id))
row = mycursor.fetchone()
print(row)
conn.commit()
conn.close()
Getting this error below
Exception in Tkinter callback
Traceback (most recent call last):
File "c:\programdata\anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "appwithDB.py", line 51, in <lambda>
search_button = Button(newframe, text="Search", command=lambda : search(entry_search.get()))
File "appwithDB.py", line 71, in search
mycursor.execute(query, (id))
TypeError: not all arguments converted during string formatting
The second argument to mycursor.execute must be an iterable containing the values to insert in the query
You can use a list: mycursor.execute(query, [id])
or a one-element tuple: mycursor.execute(query, (id,))
Notice the comma. (id) is the same than id. In python, the comma is making the tuple, not the parenthesis.
I am trying to create a table on demand.
def create_host_table(name):
"""Create host server status table.
Arguments:
name (str): name of the host.
"""
class HostMachineStatus(Base):
__tablename__ = '{0}{1}{2}'.format(HOST_TABLE_PREFIX, name, HOST_TABLE_SUFFIX)
id = Column(Integer, primary_key=True)
status = Column(String(7)) # online or offline
datemodified = Column('datemodified', TIMESTAMP, server_default=text(
'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
)
)
datecreated= Column('datecreated', TIMESTAMP)
status_time_delta = ('status_time_delta', TIMESTAMP) # duration how long was online or offline.
table_name = HostMachineStatus.__tablename__
Base.metadata.create_all(engine, Base.metadata.tables[table_name],checkfirst=True)
but the above code throws error:
Traceback (most recent call last):
File "server_status.py", line 97, in <module>
main()
File "server_status.py", line 93, in main
create_host_table(host)
File "server_status.py", line 71, in create_host_table
Base.metadata.create_all(engine, Base.metadata.tables[table_name],checkfirst=True)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/sql/schema.py", line 4316, in create_all
ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2049, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1618, in _run_visitor
visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/sql/visitors.py", line 138, in traverse_single
return meth(obj, **kw)
File "/home/rock64/.local/lib/python3.6/site-packages/sqlalchemy/sql/ddl.py", line 754, in visit_metadata
[t for t in tables if self._can_create_table(t)]
TypeError: 'Table' object is not iterable
From the documentation (emphasis mine):
tables – Optional list of Table objects, which is a subset of the total tables in the MetaData (others are ignored).
So the issue is that you are passing a single table, not a list of tables. (The "is not iterable" in the error output is also a strong hint here.) Try this instead:
Base.metadata.create_all(engine, [Base.metadata.tables[table_name]], checkfirst=True)
I have been spending hours trying to and searching for how to insert strings and decimals into a sql insert statement. I cannot seem to find an answer that works. I am using AWS Lambda with Python 3.6 to process data. I continue to have issues getting the values of variables to inserted into my SQL statement. I am getting the following error message.
My code:
timeStamp = 123456789.123456
thing = "Testing/IoT"
statement = "INSERT INTO `cycles` (`timeStamp`, `thing`) VALUES ({}, {})"
theData = (timeStamp, thing)
logger.info(statement, theData)
cursor = conn.cursor()
cursor.execute(statement, theData)
conn.commit()
Error message:
not all arguments converted during string formatting: TypeError
Traceback (most recent call last): File
"/var/task/recordCyclesRDS.py", line 56, in handler
logger.info(statement, theData) File
"/var/lang/lib/python3.6/logging/init.py", line 1306, in info
self._log(INFO, msg, args, **kwargs) File
"/var/lang/lib/python3.6/logging/init.py", line 1442, in _log
self.handle(record) File
"/var/lang/lib/python3.6/logging/init.py", line 1452, in handle
self.callHandlers(record) File
"/var/lang/lib/python3.6/logging/init.py", line 1514, in
callHandlers hdlr.handle(record) File
"/var/lang/lib/python3.6/logging/init.py", line 863, in handle
self.emit(record) File "/var/runtime/awslambda/bootstrap.py", line
442, in emit msg = self.format(record) File
"/var/lang/lib/python3.6/logging/init.py", line 838, in format
return fmt.format(record) File
"/var/lang/lib/python3.6/logging/init.py", line 575, in format
record.message = record.getMessage() File
"/var/lang/lib/python3.6/logging/init.py", line 338, in getMessage
msg = msg % self.args TypeError: not all arguments converted during
string formatting
I can make this work:
statement = "INSERT INTO `cycles` (`timeStamp`, `thing`) VALUES (123456.123456, 'Testing/IoT')"
Try this:
timeStamp = 123456789.123456
thing = "Testing/IoT"
statement = "INSERT INTO `cycles` (`timeStamp`, `thing`) VALUES ({}, {})"
theData = (str(timeStamp), thing)
logger.info(statement, theData)
cursor = conn.cursor()
cursor.execute(statement, theData)
conn.commit()