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()
3
  • 2
    you are checking if Number == number... That will never happen.. assuming you have a variable number try Number =='+number Commented Dec 4, 2015 at 1:28
  • the number was a parameter from the function call...sorry about that. Commented Dec 4, 2015 at 1:31
  • keep returning empty: Did you write return in your function? Commented Dec 4, 2015 at 1:35

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

or maybe something like cur.execute('SELECT Savings, Chequing FROM Accounts where number=='+number) would work
@Busturdust: Yeah, but use (?) can avoid sql injection, see stackoverflow.com/questions/13613037/… and stackoverflow.com/questions/29528511/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.