Lowity
Lowity

Reputation: 91

How can i insert a variable in sqlite3?

im noob in Python and i want to connect a database(sqlite3) with my "account manager", and so i want to insert a variable in read_from_db, instead of a word. Can someone help me?

def read_from_db():
    **c.execute("SELECT * FROM daten WHERE username= AND password= ")**
    # data = c.fetchall()
    for row in c.fetchall():
        name_test = (row[0])
        password_test = (row[1])

Upvotes: 1

Views: 85

Answers (2)

Yoel Nisanov
Yoel Nisanov

Reputation: 1054

A more generic way is to use .format()

def read_from_db(p):
    **c.execute("SELECT * FROM daten WHERE username={0} AND password={1}".format(user_name, password)**
    # data = c.fetchall()
    for row in c.fetchall():
        name_test = (row[0])
        password_test = (row[1])

Upvotes: 0

xibalba1
xibalba1

Reputation: 544

If use python 3.6+, check out f-strings

def read_from_db(un, pw):
    **c.execute(f"SELECT * FROM daten WHERE username={un} AND password={pw} ")**
    # data = c.fetchall()
    for row in c.fetchall():
        name_test = (row[0])
        password_test = (row[1])

Upvotes: 1

Related Questions