0

I have a database with data sets like below how do call the remaining column data from a row I call using the first value.

For example how can I call '12345' and be returned '1a2b3c' 'Anna' '1'.

"12345" "1a2b3c"    "Anna"  "1"

"45678" "123abc"    "Cathy" "2"

"54321" "zaybxc"    "Alan"  "3"

I Googled around but couldn't find the syntax to make it work my solutions keep returning empty.

def get_account_balances(db, number):
    totals = []

    con =  sqlite3.connect(db)
    cur = con.cursor()    

    cur.execute('SELECT Savings, Chequing FROM Accounts')
    cur.fetchone()
Remi Guan
  • 21,506
  • 17
  • 64
  • 87

1 Answers1

1

In your new version, you never used that number variable.

In your old version:

WHERE Number == number

^ SQL will test that your number row is equals Number row or not. So your row called number and you want to check if it's equals your number variable or not, try:

cur.execute('SELECT Savings, Chequing FROM Accounts WHERE Number == (?)', [number])

Your function doesn't return anything because you didn't write return in your function, try:

def get_account_balances(db, number):
    totals = []

    con =  sqlite3.connect(db)
    cur = con.cursor()    

    cur.execute('SELECT Savings, Chequing FROM Accounts WHERE Number == (?)',
                [number])

    return cur.fetchone()
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
  • or maybe something like `cur.execute('SELECT Savings, Chequing FROM Accounts where number=='+number)` would work – Busturdust Dec 04 '15 at 01:35
  • 2
    @Busturdust: Yeah, but use `(?)` can avoid sql injection, see http://stackoverflow.com/questions/13613037/is-this-python-code-vulnerable-to-sql-injection-sqlite3 and http://stackoverflow.com/questions/29528511/python-sqlite3-sql-injection-vulnerable-code. – Remi Guan Dec 04 '15 at 01:37