python - Error entering data into the database - python

A new member has been registered in the database, but when entering the profile modification page and adding other information, it is registered in a new row and not in the same information as the registered member.
register new member :
def add_users(self):
add_fullname = self.line_fullname.text()
add_username = self.line_username.text()
add_Email = self.line_email.text()
add_password = self.line_password.text()
add_phone = self.line_telephon.text()
add_birthday = self.dateofbirth.text()
add_profession = self.line_profession.text()
add_Country = self.line_country.text()
add_keyone = self.keyone.text()
add_keytwo = self.keytwo.text()
add_newpassword = self.new_pwd.text()
self.cur.execute(''' SELECT add_username FROM users=add_username''')
data = self.cur.fetchall()
self.cur.execute(''' INSERT INTO users(add_fullname,
add_username, add_Email, add_password, add_phone,
add_birthday, add_profession, add_Country, add_keyone, add_keytwo, add_newpassword)
VALUE (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s ) ''',
(add_fullname, add_username, add_Email, add_password, add_phone, add_birthday,
add_profession, add_Country, add_keyone, add_keytwo, add_newpassword))
self.mysql_db.commit()
self.line_fullname.clear()
self.line_username.clear()
self.line_email.clear()
self.line_password.clear()
this code for fill profile :
def save_profil(self):
self.tabWidget.setCurrentIndex(6)
add_fullname = self.line_fullname.text()
add_Email = self.line_email.text()
add_phone = self.line_telephon.text()
add_birthday = self.dateofbirth.text()
add_profession = self.line_profession.text()
add_Country = self.line_country.text()
add_keyone = self.keyone.text()
add_keytwo = self.keytwo.text()
add_newpassword = self.new_pwd.text()
self.cur.execute(''' INSERT INTO users(add_fullname, add_Email, add_phone, add_birthday, add_profession,
add_Country, add_keyone, add_keytwo, add_newpassword)
VALUE (%s, %s, %s, %s, %s, %s, %s, %s, %s ) ''',
(add_fullname, add_Email,
add_phone, add_birthday, add_profession,
add_Country, add_keyone, add_keytwo, add_newpassword))
self.mysql_db.commit()
print("profil edited")

You should use UPDATE instead of INSERT in the "save_profil" function if the profile already exists.
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
You will also need to indicate which user should be updated in
WHERE condition
I would also advise you to read the user ID or another unique key you have defined in your table. So you can update the correct user.
Ex:
def save_profil(self):
self.tabWidget.setCurrentIndex(6)
add_fullname = self.line_fullname.text()
add_Email = self.line_email.text()
add_phone = self.line_telephon.text()
add_birthday = self.dateofbirth.text()
add_profession = self.line_profession.text()
add_Country = self.line_country.text()
add_keyone = self.keyone.text()
add_keytwo = self.keytwo.text()
add_newpassword = self.new_pwd.text()
query = "UPDATE users SET add_fullname=%s, add_Email=%s, add_phone=%s, add_birthday=%s, add_profession=%s, add_Country=%s, add_keyone=%s, add_keytwo=%s, add_newpassword= %s WHERE (UNIQUE_KEY = %s)"
data = (add_fullname, add_Email,add_phone, add_birthday, add_profession,add_Country, add_keyone, add_keytwo, add_newpassword, UNIQUE_KEY_VALUE)
self.cur.execute(query,data)
self.mysql_db.commit()
print("profil edited")
Note that UNIQUE_KEY and UNIQUE_KEY_VALUE are suggestions, you must define these values according to your model.
As I can see in the figure that shows the database, you can use the "id" to know which user should be updated.For this case, UNIQUE_KEY would be replaced by id, and UNIQUE_KEY_VALUE would be the user id that needs to be updated, in your example it would be 14, but you need to get that value inside your function, just like you got the other values.

Related

Python Executing SQL fails over due to non existing field in json data

I am getting some JSON data from a third party API. I am trying to add that data into my own database to be used for a website. I loop through each record in the JSON and execute a SQL query to insert that data into my database. However some records in the JSON data doesn't exist, and therefore causes my query to fail. I have set defaults for these fields for this reason however it still falls over.
isNonFoilOnly field will only appear in some of of the records in the JSON data.
models.py
class Set(models.Model):
code = models.CharField(max_length=100, unique=True)
keyrune_code = models.CharField(max_length=100)
name = models.CharField(max_length=100)
type = models.CharField(max_length=100)
release_date = models.DateField()
base_set_size = models.IntegerField()
total_set_size = models.IntegerField()
is_online_only = models.BooleanField(default=False)
is_non_foil_only = models.BooleanField(default=False)
is_foil_only = models.BooleanField(default=False)
sale_status = models.BooleanField(default=False)
def __str__(self):
return self.name
views.py
response = requests.request("GET", "https://mtgjson.com/api/v5/SetList.json")
data = response.json()["data"]
sorted_obj = sorted(data, key=lambda k: k['releaseDate'], reverse=False)
sql = """
INSERT INTO dashboard_set
(code, keyrune_code, name, type, release_date, base_set_size, total_set_size, is_online_only, is_non_foil_only, is_foil_only, sale_status)
VALUES
( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )
ON CONFLICT (code) DO UPDATE
SET keyrune_code = %s,
name = %s,
type = %s,
release_date = %s,
base_set_size = %s,
total_set_size = %s,
is_online_only = %s,
is_non_foil_only = %s,
is_foil_only = %s;
"""
conn = None
try:
params = config()
conn = psycopg2.connect(**params)
cur = conn.cursor()
for entry in sorted_obj:
cur.execute(sql, (
entry["code"],
entry["keyruneCode"],
entry["name"],
entry["type"],
entry["releaseDate"],
entry["baseSetSize"],
entry["totalSetSize"],
entry["isOnlineOnly"],
entry["isNonFoilOnly"],
entry["isFoilOnly"],
False,
entry["keyruneCode"],
entry["name"],
entry["type"],
entry["releaseDate"],
entry["baseSetSize"],
entry["totalSetSize"],
entry["isOnlineOnly"],
entry["isNonFoilOnly"],
entry["isFoilOnly"]
))
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
return redirect('dashboard:sets')
You seem to using Django and not using it at the same time. The Django way to do this is:
from yourapp.models import Set
def yourview(request):
response = requests.request("GET", "https://mtgjson.com/api/v5/SetList.json")
data = response.json()["data"]
# Not caring about sort, because why?
for entry in data:
code = data.pop('code', None)
if not code:
continue # or raise
Set.objects.update_or_create(code=code, defaults=data)
return redirect('dashboard:sets')

Adding Items on 2 tables in MYSQL database inside pipeline using Scrapy

Hi I am trying to add 2 tables in my MYSQL database I successfully added the 2 tables but the separated items like itemA and itemB is not added on Database
If I remove the itemB, ItemA works fine and added on table1, if 2 tables I always got this error
"NameError: name 'items_f21' is not defined"
Any Idea please?
Here's my code
sales_item_spider.py
def parse_1(self, response):
item = GpdealsSpiderItem_hm()
for product_item_hm in response.css('li.product-item'):
hm_title = product_item_hm.css('h3.item-heading a.link::text').extract_first()
hm_regular_price = product_item_hm.css('strong.item-price span.price.regular::text').extract_first()
hm_sale_price = product_item_hm.css('strong.item-price span.price.sale::text').extract_first()
hm_photo_url = product_item_hm.css('.image-container img::attr(data-src)').extract_first()
hm_description_url = product_item_hm.css('h3.item-heading a::attr(href)').extract_first()
item['hm_title'] = hm_title
item['hm_regular_price'] = hm_regular_price
item['hm_sale_price'] = hm_sale_price
item['hm_photo_url'] = hm_photo_url
item['hm_description_url'] = hm_description_url
yield item
def parse_2(self, response):
items_f21 = GpdealsSpiderItem_f21()
for product_item_forever in response.css('div.pi_container'):
f21_title = product_item_forever.css('p.p_name::text').extract_first()
f21_regular_price = product_item_forever.css('span.p_old_price::text').extract_first()
f21_sale_price = product_item_forever.css('span.p_sale.t_pink::text').extract_first()
f21_photo_url = product_item_forever.css('img::attr(data-original)').extract_first()
f21_description_url = product_item_forever.css('a.item_slider.product_link::attr(href)').extract_first()
items_f21['f21_title'] = f21_title
items_f21['f21_regular_price'] = f21_regular_price
items_f21['f21_sale_price'] = f21_sale_price
items_f21['f21_photo_url'] = f21_photo_url
items_f21['f21_description_url'] = f21_description_url
yield items_f21
pipelines.py
def create_table(self):
self.curr.execute("""DROP TABLE IF EXISTS saleitems_hm""")
self.curr.execute("""create table saleitems_hm(
hm_title text,
hm_regular_price text,
hm_sale_price text,
hm_photo_url text,
hm_description_url text
)""")
self.curr.execute("""DROP TABLE IF EXISTS saleitems_f21""")
self.curr.execute("""create table saleitems_f21(
f21_title text,
f21_regular_price text,
f21_sale_price text,
f21_photo_url text,
f21_description_url text
)""")
def process_item(self, item, spider):
self.store_db(item)
return item
def store_db(self, item):
self.curr.execute("""insert into saleitems_hm values (%s, %s, %s, %s, %s)""", (
item['hm_title'],
item['hm_regular_price'],
item['hm_sale_price'],
item['hm_photo_url'],
item['hm_description_url']
))
self.conn.commit()
self.curr.execute("""insert into saleitems_f21 values (%s, %s, %s, %s, %s)""", (
items_f21['f21_title'],
items_f21['f21_regular_price'],
items_f21['f21_sale_price'],
items_f21['f21_photo_url'],
items_f21['f21_description_url']
))
self.conn.commit()
Your store_db uses a items_f21 variable, which is never defined before.
On store_db you should use only the item variable, and use the corresponding INSERT statement depending on the type of item it is:
def store_db(self, item):
if isinstance(item, GpdealsSpiderItem_hm):
self.curr.execute("""insert into saleitems_hm values (%s, %s, %s, %s, %s)""", (
item['hm_title'],
item['hm_regular_price'],
item['hm_sale_price'],
item['hm_photo_url'],
item['hm_description_url']
))
else:
self.curr.execute("""insert into saleitems_f21 values (%s, %s, %s, %s, %s)""", (
item['f21_title'],
item['f21_regular_price'],
item['f21_sale_price'],
item['f21_photo_url'],
item['f21_description_url']
))
self.conn.commit()

1048, "Column 'execution_period' cannot be null"

Main idea of these work is to parse each page from pagination(it has 152 pages), and put these data to db. while running error occurred, column 'execution_period' cannot be null".
in my DataFrame I did this:
df['Срок исполнения'] = df['Срок исполнения'].fillna(0)
df['Дата принятия решения/дела'] = df['Дата принятия решения/дела'].fillna(0)
don't know how to fix it.
df = pd.DataFrame()
df_list = pd.read_html('sanction.xls')
df = df_list[0]
database = MySQLdb.connect (host="host", user = "root", passwd = "password", db = "db", charset='utf8')
mycursor = database.cursor()
cursor = database.cursor()
query = """INSERT INTO sanction (
id,
organization_type, organization, date,
decision_number, penalty_type, penalty_way,
penalty, violation, execution_period,
article, note, type_npa,
department, uploaded_date)
VALUES (
null,
%s, %s, STR_TO_DATE(%s,"%%d.%%m.%%Y"),
%s, %s, %s,
%s, %s, STR_TO_DATE(%s,"%%d.%%m.%%Y"),
%s, %s, %s,
%s, %s)"""
for r in range(1, sheet.nrows):
organization_type = sheet.cell(r,1).value
organization = sheet.cell(r,2).value
date = (sheet.cell(r,3).value)
decision_number = sheet.cell(r,4).value
penalty_type = sheet.cell(r,5).value
penalty_way = sheet.cell(r,6).value
penalty = sheet.cell(r,7).value
violation = sheet.cell(r,8).value
execution_period = sheet.cell(r,9).value
article =sheet.cell(r,10).value
note =sheet.cell(r,11).value
type_npa =sheet.cell(r,12).value
department =sheet.cell(r,13).value
uploaded_date =datetime.now().strftime("%Y-%m-%d %H:%M")
values = (organization_type, organization, date, decision_number, penalty_type,
penalty_way,penalty, violation, execution_period,article, note, type_npa, department,uploaded_date)
mycursor.execute(query, values)

not enough arguments for format string from excel to mysql

I'm trying to put all information from excel to mysql, while processing it have these problem.
struggling to solve it!
counted all %s, seems like didn't miss any of them.
query = """INSERT INTO sanction (id, organization_type, organization, date, decision_number, penalty_type, penalty_way
penalty, violation, execution_period, article, note, type_npa, department, uploaded_date)
VALUES(null, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
for r in range(1, sheet.nrows):
organization_type = sheet.cell(r,1).value
organization = sheet.cell(r,2).value
date = sheet.cell(r,3).value
decision_number = sheet.cell(r,4).value
penalty_type = sheet.cell(r,5).value
penalty_way = sheet.cell(r,6).value
penalty = sheet.cell(r,7).value
violation = sheet.cell(r,8).value
execution_period = sheet.cell(r,9).value
article =sheet.cell(r,10).value
note =sheet.cell(r,11).value
type_npa =sheet.cell(r,12).value
department =sheet.cell(r,13).value
uploaded_date =datetime.now().strftime("%Y-%m-%d %H:%M")
values = (organization_type, organization, date, decision_number, penalty_type,
penalty_way,penalty, violation, execution_period,article, note, type_npa, department,uploaded_date)
mycursor.execute(query, [values])
I notice 2 things that could cause this error:
Your variable values is a tuple already, so you dont need to wrap it inside a new list.
That means, change this line
mycursor.execute(query, [values])
to
mycursor.execute(query, values)
You are also missing a comma in your query in the part where you list the target column names, between penalty_way and penalty.
In case of this many arguments, I would suggest to restructure your code so that you can more easily see if you missed anything.
For example, here is a version that groups the 15 parameters in a 1-3-3-3-3-2 formation in 3 parts: the first part of the query, the second part of the query and also when building the values tuple.
query = """
INSERT INTO sanction (
id,
organization_type, organization, date,
decision_number, penalty_type, penalty_way,
penalty, violation, execution_period,
article, note, type_npa,
department, uploaded_date)
VALUES (
null,
%s, %s, %s,
%s, %s, %s,
%s, %s, %s,
%s, %s, %s,
%s, %s)
"""
for r in range(1, sheet.nrows):
organization_type = sheet.cell(r, 1).value
organization = sheet.cell(r, 2).value
date = sheet.cell(r, 3).value
decision_number = sheet.cell(r, 4).value
penalty_type = sheet.cell(r, 5).value
penalty_way = sheet.cell(r, 6).value
penalty = sheet.cell(r, 7).value
violation = sheet.cell(r, 8).value
execution_period = sheet.cell(r, 9).value
article = sheet.cell(r, 10).value
note = sheet.cell(r, 11).value
type_npa = sheet.cell(r, 12).value
department = sheet.cell(r, 13).value
uploaded_date = datetime.now().strftime("%Y-%m-%d %H:%M")
values = (
# the first value of the INSERT statement will be NULL
organization_type, organization, date, # 3 elements
decision_number, penalty_type, penalty_way, # 3 elements
penalty, violation, execution_period, # 3 elements
article, note, type_npa, # 3 elements
department, uploaded_date, # 2 elements
)
mycursor.execute(query, values)

Syntax error in SQL IF statement

I'm using python Flask and can't get my head around why i'm getting error:
ProgrammingError: syntax error at or near "IF"
LINE 1: IF SELECT count(*) FROM ProfilePicture WHERE userid =
Here is my code:
> def updateProfilePicture(filename, image, userid):
> cursor = getCursor()
> binary = psycopg2.Binary(image)
> data = (userid, filename, binary, userid, filename, binary, userid)
> #SQL = """INSERT INTO ProfilePicture(id, image, userid)
> # VALUES (%s, %s, %s)"""
> SQL = """IF SELECT count(*) FROM ProfilePicture WHERE userid = %s > 0
> THEN
> UPDATE ProfilePicture SET id = %s, image = %s WHERE userid = %s
> ELSE
> INSERT INTO ProfilePicture(id, image, userid) VALUES (%s, %s, %s)
> END IF"""
> print cursor.mogrify(SQL, data)
> cursor.execute(SQL, data)
> cursor.connection.commit()
> cursor.close()
> return
A simple insert works well but not the if statement.
Appreciate your help!
Since "ON CONFLICT" syntax is introduced in PostgreSQL 9.5, you have to test the existence of the row in python.
If you have a unique constraint on the userid you can use exceptions:
def updateProfilePicture(filename, image, userid):
cursor = getCursor()
binary = psycopg2.Binary(image)
data = (userid, filename, binary, userid, filename, binary, userid)
SQL = """INSERT INTO ProfilePicture(id, image, userid) VALUES (%s, %s, %s)"""
try:
cursor.execute(SQL, data)
except:
cursor.rollback()
SQL = """UPDATE ProfilePicture SET id = %s, image = %s WHERE userid = %s"""
cursor.execute(SQL, data)
cursor.connection.commit()
cursor.close()
That's not SQL syntax, it's the PL/pgSQL procedural language. It's primarily used to write functions. You can use it for a one-off command, but you need to put it in a DO block:
DO $$
BEGIN
IF (SELECT count(*) FROM ProfilePicture WHERE userid = %s) > 0 THEN
UPDATE ProfilePicture SET id = %s, image = %s WHERE userid = %s;
ELSE
INSERT INTO ProfilePicture(id, image, userid) VALUES (%s, %s, %s);
END IF;
END
$$
Note, however, that your logic will not work if someone else is inserting into/deleting from ProfilePicture at the same time; you risk either losing the update, or inserting multiple records for the same userid. Avoiding this is less than straightforward.

Categories

Resources