So I am inserting data measurements into my MySQL database and sometimes I have null values that need to be inserted. Bu I always get the same error and I don't understand why. If the value isn't None, the measurement is inserted, but not the other way around.
This is the part of the code where a measurement is inserted.
sql = ("INSERT INTO `measurement` (`station_id`, `metric_name`, `raw_value`, `corrected_value`, `quality`, `failure`, `failure_type`, `timestamp`) " + \
"VALUES (%s, %s, %s, %s, %s, %s, %s, %s);")
if str(message["failure"]) == False:
failure = 0
else:
failure = 1
if str(message["true_value"]) == None:
cursor.execute(sql, (str(sensor_id[0][0]), metric, None, str(message["value"]), str(message["quality"]), failure, message["failure_type"], timestamp))
else:
cursor.execute(sql, (str(sensor_id[0][0]), metric, str(message["true_value"]), str(message["value"]), str(message["quality"]), failure, message["failure_type"], timestamp))
This is the error I get:
mysql.connector.errors.DatabaseError: 1265 (01000): Data truncated for column 'raw_value' at row 1
Column raw_value is configured as a double in the database.
Related
I seem to get a SQL syntax error after I add in the python code for the BETWEEN AND SQL query.
Code in question:
sqlInsertQuery = """
INSERT INTO `table` (`row1`, `row2`, `row3`, `row4`, `row5`, `row6`, `row7`, `date_modified`, `row9`)
VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)
ON DUPLICATE KEY UPDATE `row2` = %s, `row3` = %s, `row4` = %s, `row5` = %s, `row6` = %s, `row7` = %s, `date_modified` = %s, `row9` = %s
WHERE `date_modified` BETWEEN %s AND %s
"""
between1 = "07/18/2021 00:00:00"
between2 = "08/03/2021 00:00:00"
sqlInsertData = (row1, row2, row3, row4, row5, row6, row7, row8, row9, row2, row3, row4, row5, row6, row7, `date_modified`, row9, between1, between2)
# Link to the method I used for this statement
# https://stackoverflow.com/questions/65225153/how-to-insert-variables-into-mysql-query
databaseConnectionCursor.execute(sqlInsertQuery, sqlInsertData)
Error in question:
mariadb.ProgrammingError: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near WHERE row8 BETWEEN ? AND ? at line 1
Is there something wrong with how I'm utilizing %s for my BETWEEN AND statement? The INSERT INTO statement worked fine until after I appended the BETWEEN AND statement. All the rows are formatted as VARCHAR for my table and the variables are declared as a string.
You can't use a WHERE clause in an INSERT statement; that's why that's a syntax error. Such a thing is simply not in the spec for MySQL/MariaDB INSERT statements.
I'm trying to create a table using psycopg2 and then insert outer data from some jsons. For this reason I defined 2 function: create_words_table() and insert_data():
con = psycopg2.connect(
database="dbname",
user="username",
password="password",
host="127.0.0.1",
port="5432"
)
cur = con.cursor()
def create_words_table():
cur.execute(""" CREATE TABLE WORDS
(word_ CHAR(50),
POS_ CHAR(20),
case_ CHAR(20),
animacy_ CHAR(20),
tense_ CHAR(20),
person_ CHAR(20),
number_ CHAR(20),
gender_ CHAR(20),
mood_ CHAR(20),
tonality_ CHAR(20),
POS_score_ REAL NOT NULL,
NEU_score_ REAL NOT NULL,
NEG_score_ REAL NOT NULL );""")
def insert_data(word):
# handle_dict() is a function for getting outer data.
if handle_dict(word) is not None:
(word_from_dict, pos_from_dict, case_from_dict, gender_from_dict,
mood_from_dict, number_from_dict, person_from_dict, tense_from_dict,
tonality_from_dict, pos_score_from_dict, neu_score_from_dict,
neg_score_from_dict) = handle_dict(word)
cur.execute('''INSERT INTO WORDS (word_, POS_, case_, gender_, mood_,
number_, person_, tense_, tonality_, pos_score_, neu_score_, neg_score_)
VALUES (word_from_dict, pos_from_dict, case_from_dict, gender_from_dict,
mood_from_dict, number_from_dict, person_from_dict, tense_from_dict,
tonality_from_dict, pos_score_from_dict, neu_score_from_dict,
neg_score_from_dict)''')
con.commit()
con.close()
So, attempting to execute them:
if __name__ == '__main__':
create_words_table()
logger.info("table created")
insert_data()
logger.info("Records inserted successfully")
I got further error:
Traceback (most recent call last):
File "path\to\file", line 178, in <module>
main()
File "path\to\file", line 172, in main
insert_data()
File "path\to\file", line 148, in insert_data
cur.execute('''
psycopg2.errors.UndefinedColumn: ОШИБКА: столбец "word_from_dict" не существует
# ERROR: column "word_from_dict" does not exist ###
LINE 4: VALUES (word_from_dict, pos_from_dict, case_from_dic...
Looking at documentation, I don't see any mistake applying simple INSERT query and can't understand why psycopg2 takes a tuple after word VALUE as a table's fields.
If anybody could clear me this, I'll be very grateful.
the problem is in your query , you need to change your query to this and pass the tuple as param:
cur.execute('''INSERT INTO WORDS (word_, POS_, case_, gender_, mood_,
number_, person_, tense_, tonality_, pos_score_, neu_score_, neg_score_)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''',handle_dict(word))
I want to insert multiple records to a mysql db using python. I use mysql.connector. but I receive error. when I try to insert a record without formatting it works but multiple line with formatting doesn't!
I've used ? instead of %s but I still receive this error.
my_nodes = []
myconnection = mysql.connector.connect(
host='127.0.0.1', user='root', passwd='1234', db='job_graph1')
mycursor=myconnection.cursor()
for nodes in range(1, 33):
weight = random.randint(0, 100)
my_record = (nodes, weight, False, False)
my_nodes.append(my_record)
sqlqu = "INSERT INTO t_node(n_id,n_weight,is_entry,is_exit) VALUES(%S, %S, %s, %s)"
mycursor.executemany(sqlqu, my_nodes)
the error I receive is:
Failed processing format-parameters; %s" % err)
mysql.connector.errors.ProgrammingError: Failed processing format-parameters; 'tuple' object cannot be interpreted as an integer
So you need to remove %S in your sql request.
Because it causes this error :
print("(%s, %S)"%(1, 2))
ValueError: unsupported format character 'S' (0x53) at index 6
So instead of VALUES(%S, %S, %s, %s) use VALUES(%s, %s, %s, %s)
I'm sure I'm making a simple mistake, but I be darned if I can figure it out. I am making a temperature/humidity monitor using a RPi and a DHT22. Code is in python. Works great. I'd like to dump the variable data collected into a MySQL db, but my insert query keeps failing. I can insert straight strings, but can't seem to figure out how to reference the variables. Here is my code
import time
time.sleep(2)
import MySQLdb
temperature = 60.0
humidity = 30.0
IP_Add = "123.456.78.9"
location = "basement"
name = "home"
while True:
humidity = humidity
temperature = temperature
fTemperature = (temperature * 9 / 5) + 32
name = 'home'
if humidity is not None and temperature is not None:
print('Temp={1:0.1f} Humidity={0:0.1f}%'.format(temperature, humidity))
else:
print('Whoops')
myDB = MySQLdb.connect(host="localhost", port = 3306, user = "root", passwd = "********", db = "PLAYTHING")
cur = myDB.cursor()
try:
#query1 = "INSERT INTO `testdata`(`Name`) VALUES (name)"
#query2 = "INSERT INTO `testdata`(`Name`, `Location`) VALUES (name, location)"
#query3 = "INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)VALUES (%s, %s, %s)", ('No_ID2', 'basement', 30.5)
query4 = "INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)VALUES {0} {1} {2}".format ('No_ID2', 'basement', 30.5)
#query5 = "INSERT INTO testdata(`Name`, `Location`, `T-Reading`, `H-Reading`) VALUES ('Friday3', 'location', 72.5, 29.6)"
cur.execute(query)
myDB.commit()
print ("Commit Sucessful")
except (MySQLdb.Error, MySQLdb.Warning) as e:
print(e)
cur.close()
myDB.close()
time.sleep(10)
I have checked the MySQLdb docs at https://mysqlclient.readthedocs.io/user_guide.html#functions-and-attributes
which offers this as a guide
"""INSERT INTO breakfast (name, spam, eggs, sausage, price)
VALUES (%s, %s, %s, %s, %s)""",
[
("Spam and Sausage Lover's Plate", 5, 1, 8, 7.95 ),
("Not So Much Spam Plate", 3, 2, 0, 3.95 ),
("Don't Wany ANY SPAM! Plate", 0, 4, 3, 5.95 )
] )
but that seems not to work for me.
Query 1 and 2 execute but enter no data, col's 3 and 4 are NULL.
Query 3 gives me this message "TypeError: query() argument 1 must be string or read-only buffer, not tuple"
Query 4 enters no data and gives me this: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'No_ID2 basement 30.5' at line 1")
Query 5 is successful, but doesn't solve the problem of getting the variables from the program and inserting them into the db.
If someone would point out my error I would appreciate it.
Issues with the queries:
#1 and #2: name and location in VALUES (name, location) are considered as column names in database, thus no data.
#3: As Ilja pointed out, the tuple should be in execute() call. This should be the way to go.
query3 = ("INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)"
+ " VALUES (%s, %s, %s)")
cur.execute(query3, ('No_ID2', 'basement', 30.5))
#4: To put value directly into VALUES, string must be quoted. Below is the right format. Note: This is for experimental only as it pose SQL Injection security risk.
query4 = ("INSERT INTO `testdata`(`Name`, `Location`, `T-Reading`)"
+ " VALUES ('{0}', '{1}', {2})".format (
'No_ID2', 'basement', 30.5
))
#5: All values in SQL statement are constant.
I am trying to use Python to insert into MySQL database, but I have an auto-increment column (TeamID). I am using the lines below and it works like a charm. BUT I would like to not specify the TeamID in my Python script as it is an auto-increment
try:
cur.execute ("INSERT INTO teams values (%d, '%s', %d, '%s')" % (11,"Sevilla", 7, "Jorge Sampaoli"))
db.commit()
this works perfectly
How can I get rid of the first %d and 11 please? I want this value to be added automatically via the script
any help is very much appreciated
EDIT:
#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect(host="localhost", # your host, usually localhost
user="username", # your username
passwd="password", # your password
db="dbname") # name of the data base
cur = db.cursor()
try:
cur.execute ("INSERT INTO teams values ('%s', %d, '%s')" % ("Sevilla", 7, "Jorge Sampaoli"))
db.commit()
except Exception as e:
print("Rolling back")
print(e)
db.rollback()
db.close()
Issue is now resolved
I did specify the column names but didn't notice I need to use %s for all columns including int values. As below:
cur.execute("INSERT INTO teams (TeamName, CountryID, TeamManager) values (%s,%s,%s)", ('Everton', 1, 'Ronald Koeman'))
Try
INSERT INTO teams (name, numb, player) VALUES ('%s', %d, '%s')
I.e.explicitly list columns.
Also PLEASE don't do it like this -- instead of doing '%s' you really need to use prepared statements,I think in Python it is something like:
cursor.execute("INSERT INTO teams (name, numb, player) VALUES (%s, %d, %s)", ['Sevilla', 7, 'John Smith'])
Read up on SQL injections.
import sqlite3
def insert_data(lVideoList, gate_id):
connection = sqlite3.connect('db.sqlite',
detect_types=sqlite3.PARSE_DECLTYPES |
sqlite3.PARSE_COLNAMES)
cursor = connection.cursor()
success = 200
# gateid = 1
default_value = 0
for gate_id in range(no_of_gate):
gate_id = i+1
for videofilename in lVideoList:
print("videofilename: ", videofilename)
insert_query = ("INSERT INTO dailyfootfall(csv_name, video_download, processed, footfall, send_status, "
"male_footfall, send_status_male, female_footfall, send_status_female, gate_id,outsiders, send_status_outsiders) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)")
cursor.execute(insert_query,[videofilename, success, success, default_value, success, default_value,
success, default_value, success, gate_id, default_value, success])
print("Data Inserted Successfully !")
connection.commit()
cursor.close()
connection.close()
if __name__ == "__main__":
lVideoList = getCompleteVideoList("2022_01_24", "10:00", "22:00")
no_of_gate = 3
insert_data (lVideoList, gate_id)
print("default_value inserted!!!!")