Z
Z
ztx5112021-04-03 20:36:43
Python
ztx511, 2021-04-03 20:36:43

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

and it takes six values ​​as a number, where 0 is the person does not have this machine, and 1 is.
So this is how to check what cars he has, not to check everything in this way -
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

4 answer(s)
O
o5a, 2021-04-04
@ztx511

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

As a result, the query will return say "001010" if the user has a yamaha + lancer. Then compare with the list of machines. For example like this
# допустим получили такую строку флагов из таблицы
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']

--
You can also use field names directly from the query via cursor.description, something like this:
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)

D
d-stream, 2021-04-03
@d-stream

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

J
jerwright, 2021-04-03
@jerwright

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

P
PavelMos, 2021-04-04
@PavelMos

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 question

Ask a Question

731 491 924 answers to any question