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 8836895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:35:45+00:00 2026-06-14T09:35:45+00:00

Im creating a python program that connects to mysql. i need to check if

  • 0

Im creating a python program that connects to mysql.

i need to check if a table contains the number 1 to show that it has connected successfully, this is my code thus far:

xcnx.execute('CREATE TABLE settings(status INT(1) NOT NULL)')
  xcnx.execute('INSERT INTO settings(status) VALUES(1)')
  cnx.commit()
  sqlq = "SELECT * FROM settings WHERE status = '1'"
  xcnx.execute(sqlq)
  results = xcnx.fetchall()
  if results =='1':
    print 'yep its connected'
  else:
    print 'nope not connected'

what have i missed? i am an sql noob, thanks guys.

  • 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-06-14T09:35:46+00:00Added an answer on June 14, 2026 at 9:35 am

    I believe the most efficient “does it exist” query is just to do a count:

    sqlq = "SELECT COUNT(1) FROM settings WHERE status = '1'"
    xcnx.execute(sqlq)
    if xcnx.fetchone()[0]:
        # exists
    

    Instead of asking the database to perform any count operations on fields or rows, you are just asking it to return a 1 or 0 if the result produces any matches. This is much more efficient that returning actual records and counting the amount client side because it saves serialization and deserialization on both sides, and the data transfer.

    In [22]: c.execute("select count(1) from settings where status = 1")
    Out[22]: 1L  # rows
    
    In [23]: c.fetchone()[0]
    Out[23]: 1L  # count found a match
    
    In [24]: c.execute("select count(1) from settings where status = 2")
    Out[24]: 1L  # rows
    
    In [25]: c.fetchone()[0]
    Out[25]: 0L  # count did not find a match
    

    count(*) is going to be the same as count(1). In your case because you are creating a new table, it is going to show 1 result. If you have 10,000 matches it would be 10000. But all you care about in your test is whether it is NOT 0, so you can perform a bool truth test.

    Update

    Actually, it is even faster to just use the rowcount, and not even fetch results:

    In [15]: if c.execute("select (1) from settings where status = 1 limit 1"): 
                print True
    True
    
    In [16]: if c.execute("select (1) from settings where status = 10 limit 1"): 
                print True
    
    In [17]: 
    

    This is also how django’s ORM does a queryObject.exists().

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

Sidebar

Related Questions

I am creating a simple python program that needs to search a somewhat large
So I'm creating a very basic python game and I need some help with
I am having problems creating a table in python. Basically I want to build
I am in the process of creating a Java program that goes on the
I'am writing a multi-threaded program in C. Before creating the threads, a global python
So I am creating a program to generate pascal's triangle, which I know has
I am creating a python gtk program which uploads videos to youtube in ubuntu
Im creating a program that prints out the Ip adress of the User. So
All discussion I've seen on SO_REUSEADDR assumes that it's the same program creating and
I have a small program in python that consists in one .py file plus

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.