psycopg2 takes a value as a column - python

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))

Related

Insert some multiple records to mysql db using python 3

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)

Why is this query not working from python to SQL?

I am new to coding and databases, I can not get the query to work if I write it long hand but I have a lot to carry out and want it in a function but cannot get it to work, it returns a parameters error
import mysql.connector
def connection_check_1(query, value):
mydb = mysql.connector.connect(
host="******",
user="*****",
passwd="*****",
database="****"
)
mycursor = mydb.cursor()
mycursor.execute(query, (value))
myresult = mycursor.fetchall()
mydb.close()
return myresult
value = "sheep"
query = 'select inlicence from licence where animal = %s'
myresult = connection_check_1(query, value)
print(myresult)
Here is the SQL table I have
create table licence
(
animal varchar (20) primary key,
inlicence int (1)
);
This is the error I get
Traceback (most recent call last):
File "*******************", line 20, in
myresult = connection_check_1(query, value)
File "********************", line 13, in connection_check_1
mycursor.execute(query, (value))
File "********************************************88", line 246, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/home/kev/PycharmProjects/test bed/venv/lib/python3.5/site-packages/mysql/connector/connection_cext.py", line 535, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters
I have tried changing the way the query is written, changing it to fetchall().
Wrapping a value with () doesn't turn it in to a tuple. You probably meant to add a comma there:
mycursor.execute(query, (value,))
# Creates a one-element tuple-^

Python MySQL Query Format

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.

MySQL INSERT statement in Python

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!!!!")

getting error while dumping the data from excel sheet to mysql database

I am importing the specific data from excel sheet and dumping that data to mysql database.
But while doing that i am getting the error
$ python dtz_db.py
dtz_db.py:43: Warning: Field 'datetime' doesn't have a default value
cursor.execute(query2, values2)
dtz_db.py:43: Warning: Data truncated for column 'lease_start_date' at row 1
cursor.execute(query2, values2)
dtz_db.py:43: Warning: Data truncated for column 'lease_end_date' at row 1
cursor.execute(query2, values2)
dtz_db.py:43: Warning: Incorrect integer value: '' for column 'lease' at row 1
cursor.execute(query2, values2)
dtz_db.py:43: Warning: Incorrect integer value: '' for column 'leased' at row 1
cursor.execute(query2, values2)
Traceback (most recent call last):
File "dtz_db.py", line 44, in <module>
cursor.execute(query1, values1)
File "c:\Python27\lib\site-packages\MySQLdb\cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "c:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defau
lterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.IntegrityError: (1452, 'Cannot add or update a child row: a fo
reign key constraint fails (`dtz_new`.`property_property`, CONSTRAINT `lease_id_
refs_id_816819bc` FOREIGN KEY (`lease_id`) REFERENCES `property_propertylease` (
`id`))')
my python file is this cod is for selecting the specific data from the excel file and dump that data to mysql database
import xlrd
import MySQLdb
book = xlrd.open_workbook("dtz11.xls")
sheet = book.sheet_by_name("Sheet1")
database = MySQLdb.connect (host="localhost", user="root", passwd="", db="dtz_new")
cursor = database.cursor()
query1 = """INSERT INTO property_property( name, inpection_date) VALUES(%s, %s )"""
query2 = """INSERT INTO property_propertylease( lease_start_date, lease_end_date, lease, leased) VALUES(%s, %s, %s, %s)"""
for r in range(1, sheet.nrows):
gaurav2 = sheet.cell(r,1).value
gaurav3 = sheet.cell(r,2).value
gaurav8 = sheet.cell(r,18).value
gaurav9 = sheet.cell(r,19).value
gaurav10 = sheet.cell(r,20).value
gaurav11 = sheet.cell(r,21).value
values1 = (gaurav2, gaurav3)
values2 = (gaurav8, gaurav9, gaurav10, gaurav11)
cursor.execute(query2, values2)
cursor.execute(query1, values1)
cursor.close()
database.commit()
database.close()
print "dumped successfully"
columns = str(sheet.ncols)
rows = str(sheet.nrows)
print "I just imported "+ columns+ " columns and "+ rows+" rows to MySQL!"
and my db table schema is
Please help me to resolve this problem,,,, Thanks a lot
Your insert statement asserts that there will be 8 variables provided to the DB, but you only give it 7. Start there.
(I know neither python nor excel interaction so I haven't posted any code. I suspect the problem is entirely in that INSERT statement though.)
Edit: So the foreign key constraint error means that according to your schema, property_property's lease_id points to data in another table (property_propertylease). Since you haven't given this second table any data, the insert fails.
Put another way, your insert statement populates a parent table. The parent table is attempting to reference data in a child table that does not exist.

Categories

Resources