Python mysql Connector Error when tying to write to database only when not In Table - mysql

I am trying to write some data to a database but only when its not already in the table. Im using the Variables data , data1 ,timestamp, and name.My Python MySql Connector looks like this
mycursor.execute("""INSERT INTO Messung (Date,capacity,Cpu,HostID) VALUES (%s, %s, %s, %s)WHERE NOT EXISTS (SELECT * FROM Messung WHERE Date = 'timestamp' AND HostID = 'name')""", (timestamp ,data ,data1 ,name))
I get the Following Error
mycursor.execute("""INSERT INTO Messung (Date,capacity,Cpu,HostID) VALUES (%s, %s, %s, %s)WHERE NOT EXISTS (SELECT * FROM Messung WHERE Date = 'timestamp' AND HostID = 'name')""", (timestamp ,data ,data1 ,name))
File "/usr/local/lib/python3.6/site-packages/mysql/connector/cursor.py", line 559, in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection.py", line 494, in cmd_query
result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
File "/usr/local/lib/python3.6/site-packages/mysql/connector/connection.py", line 396, 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 'WHERE NOT EXISTS (SELECT * FROM Messung WHERE Date = 'timestamp' AND HostID = 'n' at line 1
I dont get whats wrong with the Syntax
I also dont want to update an Existing entry just to skip them
SAMPLE DATA:
timestamp=2019-11-20 00:00:00
name =testpc
data= 18.14
data1= 58.53
I am Splitting strings like this
2019-11-18 00:00:00 testpc data 11.58 data1 29.20

MySQL Insert Syntax doesn't support where clause in it. May be you can try
INSERT ON DUPLICATE KEY UPDATE.
mycursor.execute("""INSERT INTO Messung (Date,capacity,Cpu,HostID) VALUES (%s, %s, %s, %s) ON DUPLICATE KEY UPDATE Date = 'timestamp' """, (timestamp ,data ,data1 ,name))
https://www.mysqltutorial.org/mysql-insert-or-update-on-duplicate-key-update/

Related

Query to update a table without duplicate in python3

I'm doing a project; I have to update a table with dynamic values from a script.
My table schema, that could be wrong is:
Structure of my datasbase
Structure of the table I am working with
I am trying to insert the new values of AccessToken, RefreshToken, UserID and if there is already a UserID with the same ID just update that values. My code is this one:
try:
with conexion.cursor() as cursor:
query = "INSERT INTO Tokens (AccessToken, RefreshToken, UserID) ON DUPLICATE KEY UPDATE UserID = VALUES(UserID), VALUES (%s, %s, %s, %s)"
val = (AccessToken, RefreshToken, User_ID, User_ID)
cursor.execute(query, val)
conexion.commit()
I don't use well the keys and maybe there's the error.
The error I get in my console it's:
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 'ON DUPLICATE KEY UPDATE UserID = VALUES(UserID), VALUES ('eyJhbGciOiJIUzI1NiJ9.e' at line 1")
That code in VALUES it is actually the AccessToken instead of the User_ID
Any idea what I'm doing wrong?
What about using a SELECT statement in order to search the user's access key?
If it does not exist, run the INSERT statement:
INSERT INTO Tokens (AccessToken, RefreshToken, UserID) VALUES (%s, %s, %s)
If it doesn't, run the UPDATE statement:
UPDATE Tokens SET AcessToken=%s, RefreshToken=%s WHERE UserId=%s
Finally, I solved it with this query:
INSERT INTO Tokens (userID, accessToken, refreshToken) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE accessToken = %s, refreshToken = %s;"
val = (User_ID, AccessToken, RefreshToken, AccessToken, RefreshToken)
cursor.execute(query, val)

How to insert database mysql. Solve?

How can I insert the file data into the table mysql?
code:
import pymysql.cursors
import pymysql as MySQLdb
import pymysql
from Bio import SeqIO
try:
conexao = MySQLdb.connect(host="localhost",user="root",passwd="xxx",db="db_teste")
print("conectado")
print(conexao)
except:
print("Não conectado")
for item in SeqIO.parse('seqteste.txt', 'fasta'):
dados = print('>{}\t{}'.format(str(item.description).replace('|', '\t'), item.seq), )
with conexao:
with conexao.cursor() as cursor:
sql = "INSERT INTO `tabelateste` (`id`, `id_name`, `host`, `organism`, `seq`) VALUES(1, %s, %s, %s,%s, %s)"
cursor.execute(sql, (dados, dados, dados, dados, dados,))
conexao.commit()
Erros:
pymysql.err.OperationalError: (1136, "Column count doesn't match value count at row 1")
our:
sql = "INSERT INTO `tabelateste` (`id`, `id_name`, `host`, `organism`, `seq`) VALUES(%s, %s, %s, %s,%s, %s)"
cursor.execute(sql, (dados, dados, dados, dados, dados))
conexao.commit()
error:
line 125, in mogrify
query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string
How to solve this error and be able to insert the data in the mysqldb table?
myseq:
>gb:KX262887|Organism:Zika virus|Strain Name:103451|Segment:null|Subtype:Asian|Host:Human
GTTGTTGATCTGTGTGAATCAGACTGCGACAGTTCGAGTTTGAAGCGAAAGCTAGCAACAGTATCAACAG
GTTTTATTTTGGATTTGGAAACGAGAGTTTCTGGTCATGAAAAACCCAAAAAAGAAATCCGGAGGATTCC
>gb:KX262887|Organism:Zika virus|Strain Name:103451|Segment:null|Subtype:Asian|Host:Human
GTTGTTGATCTGTGTGAATCAGACTGCGACAGTTCGAGTTTGAAGCGAAAGCTAGCAACAGTATCAACAG
GTTTTATTTTGGATTTGGAAACGAGAGTTTCTGGTCATGAAAAACCCAAAAAAGAAATCCGGAGGATTCC
>gb:KX262887|Organism:Zika virus|Strain Name:103451|Segment:null|Subtype:Asian|Host:Human
GTTGTTGATCTGTGTGAATCAGACTGCGACAGTTCGAGTTTGAAGCGAAAGCTAGCAACAGTATCAACAG
GTTTTATTTTGGATTTGGAAACGAGAGTTTCTGGTCATGAAAAACCCAAAAAAGAAATCCGGAGGATTCC
expected exit Output:
mysqldb
id id_name host organism seq
1 gb:KX262887 Human Zika Virus aatgtgttt
Solve?
You must carefully match the columns, the placeholders, and the values so that there is an identical number of each:
sql = "INSERT INTO `tabelateste` (`id`, `id_name`, `host`, `organism`, `seq`) VALUES(%s, %s, %s, %s, %s)"
cursor.execute(sql, (dados, dados, dados, dados, dados))
conexao.commit()
Note how there's 5 columns specified, 5 placeholders, and 5 binds in the tuple now. You had an extra %s.
Conceptually what you want to do is:
(column_name, ...) <-- Columns specified
|
v
( %s , ... ) <-- Placeholders specified
|
v
(bind_value , ... ) <-- Binding on execute()
Note that these must correlate 1:1:1 exactly. Any mismatches will result in errors like you've seen.
change :
for item in SeqIO.parse('seqteste.txt', 'fasta'):
dados = print('>{}\t{}'.format(str(item.description).replace('|', '\t'), item.seq), )
into
import re
cnt = 0
for item in SeqIO.parse('seqteste.txt', 'fasta'):
cnt += 1
s=(str((item.description).strip('')))
pattern_id = "(.*?)\|Organism"
pattern_host = "Host:(.*.)"
pattern_org = "Organism:(.*?)\|Strain"
ids= re.search(pattern_id, s).group(1)
host = re.search(pattern_host, s).group(1)
org = re.search(pattern_org, s).group(1)
dados = [str(cnt), ids ,host , org ,str(item.seq)]
and :
cursor.execute(sql, (dados, dados, dados, dados, dados))
with:
cursor.execute(sql, (dados[0] , dados[1] ,dados[2] ,dados[3] ,dados[4]))
let me know how it behaves, I dont have your database and can't test my code

Add pandas Dataframe to MySQL

I am trying to add a section of dataframe to mySQL database and I am getting an error on my syntax
#connection to database
conn = mysql.connector.connect(host='localhost', user='root', passwd='passed')
cur = conn.cursor() #create cursor
# Insert DataFrame records one by one.
for index, row in final_df.iterrows():
cur.execute("INSERT IGNORE INTO player ([full_name], [first_name], [last_name], [name_FIBA_format], [dob], [age], [height], [real_gm_profile], [game_log_url]) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", (row['full_name'], row['first_name'], row['last_name'], row['name_FIBA_format'], row['dob'], row['age'], row['height'], row['real_gm_profile'], row['game_log_url']));
conn.commit()
conn.close()
The error message I get is
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 487, in cmd_query
self._cmysql.query(query,
_mysql_connector.MySQLInterfaceError: 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 '[full_name], [first_name], [last_name], [name_FIBA_format], [dob], [age], [heigh' at line 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "connect_db_player_profile_update.py", line 157, in <module>
cur.execute("INSERT IGNORE INTO player ([full_name], [first_name], [last_name], [name_FIBA_format], [dob], [age], [height], [real_gm_profile], [game_log_url]) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", (row['full_name'], row['first_name'], row['last_name'], row['name_FIBA_format'], row['dob'], row['age'], row['height'], row['real_gm_profile'], row['game_log_url']));
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/cursor_cext.py", line 264, in execute
result = self._cnx.cmd_query(stmt, raw=self._raw,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/connection_cext.py", line 491, in cmd_query
raise errors.get_mysql_exception(exc.errno, msg=exc.msg,
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 '[full_name], [first_name], [last_name], [name_FIBA_format], [dob], [age], [heigh' at line 1
Any help would be greatly appreciated as been going round in circles for two weeks now. The Dataframe columns match with the the table in the database. I am using Python3, Pandas and MySQL community edition
Thank you
MySQL does not use square brackets, [...] for column identifiers but backticks. Consider also using executemany converting all rows to list of values avoiding the iterrows loop. Below reindex ensures column subset and order.
sql = """INSERT IGNORE INTO player (`full_name`, `first_name`, `last_name`,
`name_FIBA_format`, `dob`, `age`, `height`,
`real_gm_profile`, `game_log_url`)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"""
final_df = final_df.reindex(['full_name', 'first_name', 'last_name', 'name_FIBA_format',
'dob', 'age', 'height', 'real_gm_profile', 'game_log_url'],
axis='columns')
cur.executemany(sql, final_df.to_numpy().tolist())
conn.commit()
Edited,
if you use sqlalchemy module for connecting to mysql, like
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
engine = create_engine('mysql+mysqlconnector://[user]:[pass]#[host]:[port]/[schema]', echo=False)
you can use to_sql method on the dataframe
df.to_sql("player",con=engine)
to insert specific column, you might select only those columns

Python MySql and variables in query

What is the trick to update two fields "wybor", "odebrano" using variables in Python
csv_data = csv.reader(file('tmp/orders.csv'))
for field in csv_data:
id = field[0]
data = field[1]
login = field[2]
wybor = field[3]
godzina = field[4]
odebrano = field[5]
mysql.execute('INSERT INTO orders(id, data, login, wybor, godzina, odebrano) VALUES (%s, %s, %s, %s, %s, %s) ON DUPLIKATE KEY UPDATE wybor = VALUES(%s), odebrano = VALUES(%s)', [id, data, login, wybor, godzina, odebrano])
Error : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version
It probably should be ON DUPLICATE KEY, not ON DUPLIKATE KEY. Also, I don't think you need (or want) the VALUES keyword in the UPDATE clause.
INSERT INTO orders
(id, data, login, wybor, godzina, odebrano)
VALUES
(%s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
wybor = %s, odebrano = %s

ERROR 1064 (42000) - MySQL error in INSERT ... SELECT query

As part of a MySQL trigger I'm writing, I've got an INSERT ... SELECT query that is returning :
ERROR 1064 (42000) at line 7: 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 'pp2 (curr_code, pricing_id, pid, title, startdate, enddate, priority, enabled) S' at line 33
INSERT INTO product_pricing pp2 (curr_code, pricing_id, pid, title, startdate, enddate, priority, enabled)
SELECT cc, `pp1`.`pricing_id`, `pp1`.`pid`, `pp1`.`title`, `pp1`.`startdate`, `pp1`.`enddate`, `pp1`.`priority`, `pp1`.`enabled`
FROM product_pricing pp1
WHERE pp1.pp_id = NEW.pp_id
ON DUPLICATE KEY UPDATE pp2.pp_id=(SELECT newppid := pp2.pp_id);
I'm not sure if it's the cc part? That's a declared variable in the trigger but it should work given that you should be able to do a SELECT 'hello', t.col1 FROM table t
Any suggestions as to what the error is greatly received.
The INSERT syntax doesn't allow for aliases.
INSERT INTO table [ ( column [, ...] ) ]
{ DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) [, ...] | query }
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
Remove that pp2 from the INSERT query