Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6012519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:24:58+00:00 2026-05-23T02:24:58+00:00

I tried to do this in the interpreter and I can get it to

  • 0

I tried to do this in the interpreter and I can get it to work but inside my function it doesn’t

What I’m trying to do:

cursor = dbconnect.cursor()
cursor.execute("""SELECT * FROM credits WHERE phone = ?""",(phone,))
data = cursor.fetchone()
firstname = data[1] #the db is set as firstname in position 1 after the id(primekey)

I’m actually extracting all the data using this method just with different variables

The Error I get when I do it inside the function:

firstname = data[1]  
TypeError: 'NoneType' object is not subscriptable

As a note: I put a print statement after the data object to see what it was returning, in the interpreter it returns the tuple i’m searching for, inside the function it’s returning
‘None’

FULL CODE:

def FindByPhone(self,phone): 
    '''Find Credit by phone number ONLY'''    
    dbconnect = sqlite3.connect(self.dbname)  
    cursor = dbconnect.cursor()  
    cursor.execute("""SELECT * FROM credits WHERE phone = ?""",(phone,))  
    data = cursor.fetchone()  
    first = data[1]  
    last = data[2]  
    phone = data[3]  
    credit = data[4]  
    cid = data[0]
    self.SetVariables(first,last,phone,credit,cid)
    cursor.close()
    dbconnect.close()
    return
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:24:59+00:00Added an answer on May 23, 2026 at 2:24 am

    I think the problem is that your function doesn’t check if there was a matching row in the database. you will get this error if no row is being returned:

    #!/usr/bin/python
    try:
        import sqlite3
    except:
        from pysqlite2 import dbapi2 as sqlite3
    
    #prepare testcase    
    db="/tmp/soverflow.sqlite"
    dbconnect = sqlite3.connect(db)
    c = dbconnect.cursor()
    c.execute("""create table credits
    (id int not null primary key, firstname varchar(50), phone varchar(30),amount int not null)""")
    c.execute("""INSERT INTO credits (id,firstname,phone,amount) VALUES (1,'guybrush','123-456',24)""")
    c.execute("""INSERT INTO credits (id,firstname, phone,amount) VALUES (2,'elaine','1337-1337',18)""")
    dbconnect.commit()
    c.close()
    
    
    def print_firstname(phone):
        cursor = dbconnect.cursor()
        cursor.execute("""SELECT * FROM credits WHERE phone = ?""",(phone,))
        data = cursor.fetchone()
        firstname = data[1]
        cursor.close() # cleanup
        print firstname
    
    print "testing existing row"
    print_firstname('1337-1337')
    
    print "testing missing row"
    print_firstname('nothere')
    

    =>

    ./soverflow_sqlite.py 
    testing existing row
    elaine
    testing missing row
    Traceback (most recent call last):
      File "./soverflow_sqlite.py", line 31, in <module>
        print_firstname('not-in-db')
      File "./soverflow_sqlite.py", line 23, in print_firstname
        firstname = data[1]
    TypeError: 'NoneType' object is not subscriptable
    

    Solution:
    Add a check if there was a row returned from your query

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried this here: http://jsfiddle.net/92HXT/1/ but it does not work. It only works if
Having tried this query a million different ways, I can't seem to SELECT 'count_lonely_vehicles'
I tried this, but could not get through:- code behind protected HtmlTableRow trComment; protected
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
I tried this example in Perl. Can someone explain why is it true? if
I can't get the Ruby interpreter to run on either of my Macs (one
So I've got this RSS file that I'm trying to get part of a
I tried this as a simple test: @functions { private MvcHtmlString helloWorld() { return
I tried this: #!/usr/bin/python from wsgiref.simple_server import make_server from cgi import parse_qs, escape import
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.