Answer the question
In order to leave comments, you need to log in
How to check?
Here I have a cars table
cursor.execute("""CREATE TABLE IF NOT EXISTS cars (
id2 TEXT,
honda INT,
spyder INT,
yamaha INT,
ford INT,
lancer INT,
nissan INT)""")
conn.commit()
for row in cursor.execute(f"SELECT * FROM cars WHERE id2 = {ctx.author.id}"):
if #проверка
if #проверка
if #проверка
if #проверка
if #проверка
Answer the question
In order to leave comments, you need to log in
As already noted here, it is better to use a normal structure, where all the necessary machine types are linked by user id (1 for each entry), i.e. table like "user_cars(user_id, car)". Then it will be possible to immediately get just a list of user's cars for a request "SELECT car FROM user_cars WHERE user_id = ..."
And with this structure, you can merge the values \u200b\u200bin one "numeric" string (and at the same time use parameter passing instead of text formatting)
cursor.execute(f"SELECT honda||spyder||yamaha||ford||lancer||nissan car_info FROM cars WHERE id2 = ?", (ctx.author.id, ))
# допустим получили такую строку флагов из таблицы
car_flag = "001010"
# список машин в таком же порядке, как столбцы таблицы
cars = ['honda', 'spyder', 'yamaha', 'ford', 'lancer', 'nissan']
# сопоставляем нашу строку флагов со списком машин
user_cars = [car for car, flag in zip(cars, car_flag) if flag == "1"]
print(user_cars)
# ['yamaha', 'lancer']
data = cursor.execute(f"SELECT * FROM cars WHERE id2 = ?", (ctx.author.id, ))
for value, name in zip(data, map(lambda x:x[0], cursor.description)):
if value = 1:
print(name)
and then a person will have two Hondas ...
it is worth reconsidering the structure because there are people and there are cars, and a car is related to a person
and then - there are brands of cars and there are specific cars of one of the brands ...
Try like this:
cursor.execute(f"SELECT * FROM cars WHERE id2 = {ctx.author.id}")
rows=sql.fetchone()
for i in rows:
try:
if int(rows[i])==1:
print(f"Машина марки {i} есть в базе данных")
except:
pass
In this form, IMHO, no way. If this is the type you need - a separate column for each brand, then you can
1) get a row by user id, the number of elements (1 or 0) in the row is known in advance and it corresponds to the number of brands (marka list) .
2) generate a dictionary d={marka[x]:stroka[x] for x in range (0, len(makra))}
3) then select keys (marks) from the dictionary, where the values will be = 1
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question