Python store value from SQL request into a variable - python

this is the code I have in Python:
for y in cursor.fetchone():
# for test - print current product attribute ID
print("Updated at Id_product_attribute: " + str(y))
# I want to select ID stock based on Id_product_attribute
idStockAvailble = "SELECT id_stock_available FROM ps_stock_available WHERE id_product_attribute = " + str(y) + ";"
cursor.execute(idStockAvailble)
# for test - print stock ID
print("Id stock availble: " + str(idStockAvailble))
When I print "idStockAvailble" i get:
Id stock availble: SELECT id_stock_available FROM ps_stock_available WHERE id_product_attribute = 136;
How can I get the value of "id_stock_available "

Related

How can I avoid SQL DB2 issue?

When I run this query by manually it execute correctly without any issue and I can get the store number and item number but when I use it in my framework and connect my scenario step to the Db2 it gives me an error. This is the query which one is I execute:::
cursor.execute("select * from qs36f.DSTHSTP join qs36f.calendar on date_ccyymmd = dhindt where date_iso between(current date - 10 day) and current date and DHCUS# in (" + open_stores + ") and dhqtss>=1 and DHCLSS = " + class_nbr + " and dhsbcl = " + sub_class_nbr + " and ((dhqtss*dhrt5s)*DHPACK) <" + end_range + "")
I don't know what is the issue here. This is error:::
cursor.execute("select * from qs36f.DSTHSTP join qs36f.calendar on date_ccyymmd = dhindt where date_iso between(current date - 10 day) and current date and DHCUS# in (" + open_stores + ") and dhqtss>=1 and DHCLSS = " + class_nbr + " and dhsbcl = " + sub_class_nbr + " and ((dhqtss*dhrt5s)*DHPACK) <" + end_range + "")
pyodbc.ProgrammingError: ('42000', '[42000] [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0104 - Token , was not valid. Valid tokens: FOR USE SKIP WAIT WITH FETCH LIMIT ORDER UNION EXCEPT OFFSET. (-104) (SQLExecDirectW)')
My expectations is I have to retrieve from database store number and item number.
It seems you are facing a syntax error. Having that your statement is:
select *
from qs36f.DSTHSTP
join qs36f.calendar
on date_ccyymmd = dhindt
where date_iso between (current date - 10 day) and current date
and DHCUS# in (" + open_stores + ")
and dhqtss>=1
and DHCLSS = " + class_nbr + "
and dhsbcl = " + sub_class_nbr + "
and ((dhqtss*dhrt5s)*DHPACK) <" + end_range + "
It's possible that you are not building it correctly. In such cases, try to remove one line from the WHERE clause and execute the query in order to find the one that is not correct.

Insert into an unknown mysql column

Am trying to insert values into a mysql table with unknown database columns that are present in the db but can be found - and are passed - from inside a loop, however I am still stuck and I get the error message. I have written sample code that reproduces the error and tries to generate the mysql query dynamically. Is there a simpler way to do this with mysql? Why is my code not running? The final query seems correct
Error
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,%s,%s,%s)' at line 1
The far that I could imagine
from Learn.callmysql import mycursor, db
datte = {}
datte["schoolfee"] = "amount"
datte["student"] = "name"
testlist = ["parent", "bothnames"] #Notte this will be generated dynamically so I have no idea the variables in it, this is a sample
thelist = []
secondlist = ""
for value in testlist:
datte[value] = "Bch" #here "Bch will be a real unknown value"
print("Final dictionary after adding degree is like: " + str(datte))
for value in datte:
thelist.append(value)
breakdown = "("
count = 0
total_count_should_be = len(thelist)
for value in thelist:
if count == total_count_should_be -1:
breakdown = breakdown + value + ")"
else:
breakdown = breakdown + value+","
count = count + 1
first_part_of_query = breakdown
print("First part of the query will be like: " + first_part_of_query)
for index in range(len(thelist)):
if index == 0:
secondlist = secondlist + "(%s,"
elif index == len(thelist)-1:
secondlist = secondlist + "%s)"
else:
secondlist = secondlist + "%s,"
second_part_of_the_query = secondlist
print("Second part of the query will be like: " + second_part_of_the_query)
#Try to join the queries
query = "INSERT INTO testtale " + first_part_of_query + " VALUES " + second_part_of_the_query
print("Query looks like: " + query)
val = datte
mycursor.execute(query, val)
db.commit()
CODE PRINTS
As #Rob Streeting suggested,
I have converted my dictionary into an ordered list then to a tuple like so
print("Dictionary is: " + str(datte))
list = []
for value in datte.values():
list.append(value)
listintotupple = tuple(list)
print(listintotupple)
Then passed it to the query:
#Try to join the queries
query = "INSERT INTO testtale " + first_part_of_query + " VALUES " + second_part_of_the_query
print("Query looks like: " + query)
val = listintotupple
mycursor.execute(query, val)
db.commit()

Dynamically passing the column name as well the values in python mysql query

This is the following code
pythonlist = ['Name','Mno']
datalist = ["qwerty",'234']
sql = "SELECT " + ",".join(pythonlist) + " FROM data WHERE name = '"+ "','".join(datalist) + "' INTO OUTFILE filename"
print(sql)
OUTPUT:
SELECT Name,Mno FROM data WHERE Name= 'qwerty','234'
DESIRED OUTPUT:
SELECT Name,Mno FROM data WHERE Name = 'qwerty' and Mno = 234
Do note the removal of quotations marks in 'mno'.
The reason I am doing this is due because the column names, as well as values corresponding it to, will change frequently
Code :
queryparams = {'Name': 'qwerty', 'Mno': '234'}
and_clause = []
[and_clause.append(' %s = %s ') for k,v in queryparams.items()]
and_clause_str = ' and '.join(and_clause)
sql = 'SELECT %s FROM data WHERE ' + and_clause_str
params = [','.join(queryparams.keys())]
for k,v in queryparams.items():
params.append(str(k))
params.append(str(v))
print(sql)
print(params)
cursor.execute(sql, params=tuple(params))
This works if you add 10/20 more items to dictionary .
Aswell as prevents SQL-injection : Using params to pass values instead of string-concatenation .
Try this:
data = {'Name': 'qwerty' , 'Mno' : '234'}
sql = "SELECT " + ", ".join(data.keys()) + " FROM data WHERE " + str(list(data.keys())[0]) + " = '" + \
str(data[list(data.keys())[0]]) + "' and " +\
str(list(data.keys())[1]) + " = " + str(data[list(data.keys())[1]])
print(sql)

]Incorrect syntax near '_283846_2019'. (102) (SQLExecDirectW)")

I have to connect the sql database to python so that I can add new user data via python.
I have tried the int conversion which puts me in further trouble of null types dataset.
i have tried the bracket placement. It doesn't work.
import os
import datetime
import pyodbc
import sqlite3
file_open = open("filenames.txt","r")
path = 'C:\\Users\\Timble\\Desktop\\Face_recognition\\user-id_filenames\\'
flag_loc = 1
flag_proc = 0
flag_vis = 0
file_read_lines = file_open.readlines()
for line in file_read_lines:
for character in line:
if character == "_":
details = line.split("_")
now = datetime.datetime.now()
name = line
print("name:", name) #col-3
print("type of name:", type(name))
user_id = int(details[1])
print("user_id:", details[1]) #col-2
print("type of user_id:", type(user_id))
date = details[2]
print("date on which photo is taken:", details[2]) #col-4
print("type of data:",type(details[2]))
now = now.strftime("%Y-%m-%d %H:%M:%S")
print("Current date and time: ", now) #col-6
print("type of current date:", type(now))
path2 = path + details[1]
if os.path.exists(path2):
print(path2)
else:
os.makedirs(path2)
#break
date = str(date)
print("type of date", type(date))
user_id = str(user_id)
print("type of user_id", type(user_id))
name = str(name)
print("type of name",type(name))
now = str(now)
print("type of now", type(now))
flag_loc = str(flag_loc)
print("type loc flag", type(flag_loc))
flag_proc = str(flag_proc)
print("type proc flag", type(flag_proc))
flag_vis = str(flag_vis)
print("type vis flag", type(flag_vis))
conn = pyodbc.connect(
"DRIVER={SQl Server};"
"server=DESKTOP-3ORBD3I\MSSQL;"
"database=TimbleSecuritySystem;"
"uid=sa;"
"pwd=P#ssword")
cur = conn.cursor()
sqlInsertUser = "Insert Into retraining (date, user_id, image_name,location_flagged, processing_flagged, insert_date, visible)Values( "+ date + " , " + user_id + " , " + name + " , " + flag_loc + " , " + flag_proc + " , " + now + " , " + flag_vis + " )"
print(sqlInsertUser)
cur.execute(sqlInsertUser)
conn.commit()
break
file_open.close()
The actual results tell me that print(sqlInsertUser) prints all the right values.
I am expecting the execute command to work and sql data added there.
This line is the problem:
sqlInsertUser = "Insert Into retraining (date, user_id, image_name,location_flagged, processing_flagged, insert_date, visible)Values( "+ date + " , " + user_id + " , " + name + " , " + flag_loc + " , " + flag_proc + " , " + now + " , " + flag_vis + " )"
For example if name contains some invalid characters e.g. "[" or "]", then the execute call fails because the name string is not properly enclosed. (It should be enclosed in a pair of quote)
You can use the parameter substitution support in pyodbc e.g.
sqlInsertUser = "Insert Into retraining (date, user_id,
image_name, location_flagged, processing_flagged, insert_date,
visible) Values (?,?,?,?,?,?,?)"
then run
cur.execute(sqlInsertUser, date, user_id, name, flag_loc, flag_proc, now, flag_vis)
(My sample code above is untested. You might need to fix some syntax errors)
For more details about the syntax see https://www.python.org/dev/peps/pep-0249/#paramstyle or https://github.com/mkleehammer/pyodbc/wiki/Cursor

INSERT INTO not working. Unique Constraint Failed (Python SQLite)

I am creating a sorting application that picks students out of a table and puts them in form groups. Gender should be equal across the groups. It loops through each table inserting random male students.
I have used print functions to track what is happening. It seems like the loop is working since
print(idMale)
is printing rows out. However there hasn't been an insertion at all in the other table. And the program breaks and outputs this error:
.
Here is my code:
# loops through each table
for x in range(0,groupNumber):
#Adds I to table name to go through each table
nameGroupList.append('I')
print(nameGroupList)
nameGroup = ''.join(nameGroupList)
#loop to select a student from the main table (sessionName) and insert them into another group
#loop number is subject to the number of males that should be in 1 group
for x in range(0, formMaleInt):
selectMaleRow = cur.execute("SELECT * FROM " + sessionName + " WHERE Gender='M' ORDER BY random() Limit 1")
#-----Find ID of student selected so they can be deleted from sessionName
idMale = selectMaleRow.fetchone()
print(idMale)
idSepChar = list(idMale)
idNum = idSepChar[0]
idNumStr = str(idNum)
#-----Find ID of student selected so they can be deleted from sessionName
insertMaleRow = cur.execute("INSERT INTO " + nameGroup + " SELECT * FROM " + sessionName + " WHERE Gender='M' ORDER BY random() Limit 1")
deleteMaleRow = cur.execute("DELETE FROM " + sessionName + " WHERE ID='" + idNumStr + "'")
Here is my sessionName table:
Here is my other table

Categories

Resources