String concatenation from variables in the execute method of sqlite3 in python

Asked

Viewed 24 times

0

I am trying to insert data into a table created in Python (module sqlite3) indirectly, that is, through variables that receive the values of inputs and not directly by typing them into the string of the execute method.

See the variable dados.

The code is this:

import sqlite3

continuar = "S"
while continuar == "S":
    nome = input("Digite o seu nome: ")
    idade = input("Digite a sua idade: ")
    conn = sqlite3.connect("banco_de_dados.db")
    #cursor = conn.execute("CREATE TABLE pessoas (nome txt, idade txt)")
    dados = conn.execute("INSERT INTO pessoas VALUES ('"+nome+"', '"+idade+"')")
    conn.commit()
    ver_dados = conn.execute("SELECT * FROM pessoas")
    print(ver_dados.fetchall())
    continuar = input("Voce quer continuar? (S) or (N) ").upper()
    print()

The code worked, but I didn’t understand the meaning of '"+nome+"'. In dados, single quotes indicate a string, which is the type of value expected. But what about double quotes and the two signs of + around the variable? What do they "indicate" to Python?

What I searched is that it has to do with concatenating with the "main" string, the string of the execute method.

  • 2

    See https://answall.com/q/398921/101 and https://i.stack.Imgur.com/zdAbK.jpg. Do not do this. Don’t actually do anything you don’t understand, first understand, then do. That is, don’t try to understand a wrong code.

No answers

Browser other questions tagged

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