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

  • Home
  • SEARCH
  • 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 671925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:25:38+00:00 2026-05-14T00:25:38+00:00

I have the cursor with the query statement as follows: cursor.execute(select rowid from components

  • 0

I have the cursor with the query statement as follows:

cursor.execute("select rowid from components where name = ?", (name,))

I want to check for the existence of the components: name and return to a python variable.
How do I do that?

  • 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-14T00:25:38+00:00Added an answer on May 14, 2026 at 12:25 am

    Since the names are unique, I really favor your (the OP’s) method of using fetchone or Alex Martelli’s method of using SELECT count(*) over my initial suggestion of using fetchall.

    fetchall wraps the results (typically multiple rows of data) in a list. Since the names are unique, fetchall returns either a list with just one tuple in the list (e.g. [(rowid,),] or an empty list []. If you desire to know the rowid, then using fetchall requires you to burrow through the list and tuple to get to the rowid.

    Using fetchone is better in this case since you get just one row, (rowid,) or None.
    To get at the rowid (provided there is one) you just have to pick off the first element of the tuple.

    If you don’t care about the particular rowid and you just want to know there is a hit,
    then you could use Alex Martelli’s suggestion, SELECT count(*), which would return either (1,) or (0,).

    Here is some example code:

    First some boiler-plate code to setup a toy sqlite table:

    import sqlite3
    connection = sqlite3.connect(':memory:')
    cursor=connection.cursor()
    cursor.execute('create table components (rowid int,name varchar(50))')    
    cursor.execute('insert into components values(?,?)', (1,'foo',))
    

    Using fetchall:

    for name in ('bar','foo'): 
        cursor.execute("SELECT rowid FROM components WHERE name = ?", (name,))
        data=cursor.fetchall()
        if len(data)==0:
            print('There is no component named %s'%name)
        else:
            print('Component %s found with rowids %s'%(name,','.join(map(str, next(zip(*data))))))
    

    yields:

    There is no component named bar
    Component foo found with rowids 1
    

    Using fetchone:

    for name in ('bar','foo'): 
        cursor.execute("SELECT rowid FROM components WHERE name = ?", (name,))
        data=cursor.fetchone()
        if data is None:
            print('There is no component named %s'%name)
        else:
            print('Component %s found with rowid %s'%(name,data[0]))
    

    yields:

    There is no component named bar
    Component foo found with rowid 1
    

    Using SELECT count(*):

    for name in ('bar','foo'): 
        cursor.execute("SELECT count(*) FROM components WHERE name = ?", (name,))
        data=cursor.fetchone()[0]
        if data==0:
            print('There is no component named %s'%name)
        else:
            print('Component %s found in %s row(s)'%(name,data))
    

    yields:

    There is no component named bar
    Component foo found in 1 row(s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this statement cursor = connection.cursor() query = SELECT * from table cursor.execute(query)
I am using the following query to return a single value: select er.elig_code from
I have a query like queryStr := 'SELECT col1, col2, col3, col4 FROM Table1
I have an SQL query that selects from two inner joined tables. The select
If I have a function which returns a reference cursor for a query, how
I want the EditText in my application to have the cursor by default when
I have a query which uses a cursor to cycle through the results of
I'm using MySQLdb, I use: cursor.execute(QUERY) Where QYERY is: INSERT (A,B,C) VALUES(%s,%s,something);'%(k1, u'W')** w
I have a database and I am doing a query to it using Cursor
I have encountered a problem when trying to select data from a table in

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.