2
conn = sqlite3.connect('SADS.db')
cur = conn.cursor()
print " "
choice = raw_input("Does the Customer know their user ID? Y/N : ")
if choice == "N":
        number = raw_input("What is their phone number? : ")
        cur.execute("SELECT * FROM customers WHERE Telephone = (?)", (number,))
        row = cur.fetchone()
        print "Customer ID : " , row[0]

I use the code above in order to retrieve customer details - but i get the following error when i do it :

  File "G:\ICT\SADS.py", line 111, in editdetails
print "Customer ID : " , row[0]
TypeError: 'NoneType' object has no attribute '__getitem__'

and its really getting on my nerves ive tried using while loops or for row in rows yet it does not work - please help :(

4
  • Are you sure the telephone number you entered is in the table? Commented Feb 7, 2013 at 20:38
  • Seems it can't find a customer with the entered phone number. Commented Feb 7, 2013 at 20:38
  • I suggest creating the query using string formatting and printing out the query to verify the query. Also check to see what format the entries in the Telephone column are in (222.222.2222/(222)222-2222/etc) and show that format to the user. Commented Feb 7, 2013 at 20:42
  • @JamesThiele: No! Do not use string formatting when SQL parameters can be used instead. SQL parameters give the database the opportunity to plan the query (and reuse the plan), and give the client library the chance to quote correctly and thus prevent SQL injection attacks. Commented Feb 7, 2013 at 21:14

1 Answer 1

6
row = cur.fetchone()
if row is None:
    print "Telephone number not found"
else:
    print "Customer ID : " , row[0]
Sign up to request clarification or add additional context in comments.

Comments

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.