Sqlite - Python - refer to table column in a loop

Asked

Viewed 531 times

0

I’m doing a script in Python accessing Sqlite database. I made a loop for but don’t know how to get the value of a column in the table.

Take the example:

cursor2 = cnx.cursor()
cursor2.execute("select * from despesas")
cursor2.moveToFirst()
cont = 0
lista = []
for (despesa) in cursor2:
    if permanente="S":
    elif:

I need to verify that the value of the "permanent" column of the "expenses" table is "S" or "N". The "permanent" column is varchar(1). Then I’m switching to Boolean. How do I do?

1 answer

0

When you write select * from despesas you are selecting all the columns of the expenses table. To select only a specific column, change the * by the name of the desired column.

So your loop for can stay like this:

//Selecionando apenas a coluna "permanente" da tabela "despesas"
query = cursor2.execute("SELECT permanente FROM despesas")

for row in query:
    if row == 'S':
        código
    else:
        código
  • Thanks. Already helped a lot. Now I want to use the value of other columns to create a record in another table. So I used *. see below: if expense[permanent]="S": Elif: list.append(expense) ydespesa = [Expense] yvencing = [Maturity] yvalor = [Value] cursor3=cnx.cursor() cursor3.execute("INSERT INTO VALUES payments(null,ydespesa,yvencing,yvalor,null,null,null)") cnx.commit()

  • Please edit your question and add this second question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.