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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:40:47+00:00 2026-06-15T11:40:47+00:00

I’ve got an issue trying to connect to a database with python. It compiles

  • 0

I’ve got an issue trying to connect to a database with python. It compiles without error but it doesn’t seem to do anything. I’m not sure if I’m instantiating the class incorrectly or what the issue may be. Could someone point me in the right direction?

import _mysql
import MySQLdb

class Operations:
    def open():
        db=_mysql.connect("127.0.0.1","root","admin","test")
        c=db.cursor()

    #deletes the cursor
    def close(self):
        c.close()

        #verifies the credentials and advances
    def login(self):
        print "Welcome to the online bookstore login!"
        x = raw_input('Please enter your user id. ')
        y = raw_input('Please enter your password. ')

        c.execute("""SELECT userid,password FROM members WHERE userid = %s""", (x,))
        z = c.password

        if y == z:
            member_menu()
        else:
            close()


    def new_user(self):
        print "Welcome to the Online book store new user registration page!"
        print "To begin, please enter your first name: "
        fName = raw_input('Please enter your first name: ')
        lName = raw_input('Please enter your last name: ')
        address = raw_input('Please enter your street address: ')
        city = raw_input('Please enter your city: ')
        state = raw_input('Please enter your state: ')
        zip_code = raw_input('Please enter your zip code: ')
        phone = raw_input('Please enter your phone number: ')
        email = raw_input('Please enter your email: ')
        user_ID = raw_input('Please enter your user id: ')
        password = raw_input('Please enter your password: ')

        c.executemany("""INSERT INTO members(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s,) VALUES (fName, lName, address, city, state, zip_code, phone, email, user_id, password,)""")

        print "Your account has been created. returning to the welcome menu. "
        welcome()

    def welcome(self):
        choice = NONE;

        print "**********************************************************************\n"
        print "***\t\t\t\t\t\t\t\t   ***\n"
        print "***\t\tWelcome to the Online Book Store\t\t   ***\n"
        print "***\t\t\t\t\t\t\t\t   ***\n"
        print "**********************************************************************\n"
        print "1. Member Login\n"
        print "2. New Member Registration\n"
        print "3. Quit\n"
        choice = raw_input('Type in your option: ')

        if choice == 1:
            login()
        elif x == 2:
            new_user()
        else:
            close()


    def member_menu(self):
        x = NONE
        print "**********************************************************************\n"
        print "***\t\t\t\t\t\t\t\t   ***\n"
        print "***\t\t   Welcome to the Online Book Store   \t\t   ***\n"
        print "***\t\t\t    Member Menu   \t\t\t   ***\n"
        print "***\t\t\t\t\t\t\t\t   ***\n"
        print "**********************************************************************\n"
        print "1. Search by Author/Title/Subject\n"
        print "2. View/Edit Shopping Cart\n"
        print "3. Check Order Status\n"
        print "4. One Click Check Out\n"
        print "5. Logout\n"
        print "Type in your option: "
        x = raw_input('Please enter your choice. ')

        if x == 1:
            close_conn(),
        elif  x == 2:
            close_conn(),
        elif  x ==  3:
            close_conn(),
        elif  x ==  4:
            close_conn(),
        else:
            close_conn()

    def main():
        start = Operations()
        print "Opening conenction to database"
        start.welcome

    if __name__ == '__main__':
        main()
  • 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-15T11:40:49+00:00Added an answer on June 15, 2026 at 11:40 am

    Well, there are so many problems with your code, that I’ll probably miss some of them anyway.

    1. Nothing happens, because your main() function and condition are both parts of the class definition, so all the interpreter sees are actually two imports and a class definition.

    2. Let’s say we unindented the main() definition and the condition. All that would happen then is creating an instance of Operations (with no special effects, as you have no custom constructor defined) and printing “Opening connection to database” to the screen, because all the last line in main() does is getting a reference to the welcome() method and ignoring it. You need to call it: start.welcome()

    3. When you do call it, much more problems will appear. NameErrors will probably come first, as you are using identifiers that do not exist in given scopes. It seems you’re new to Python’s object model and probably coming from a language with a different approach, like C++. In Python all non-static and non-class instance methods take a reference to the object they’re operating on as the first parameter, traditionally called ‘self’. If you want to access any of the fields of the object, you need to do this through ‘self’, they are not visible to the interpreter otherwise. E.g.: you open a connection and keep the cursor as c, which you later reuse in other methods:

      def open():
          # ...
          c=db.cursor()
      # ...
      def login(self):
          # ...
          c.execute("...")
      

      That’s incorrect for two reasons:

      • your open() method does not take self as a parameter
      • you’re creating c as a local variable in scope of the open() method and then trying to access it in login(), which essentialy results in a “reference before assignment” error.

      In order to be correct, it should be written like this:

      def open(self):
          # ...
          self.c = db.cursor()
      # ...
      def login(self):
          # ...
          self.c.execute("...")
      

      You’re making the same mistake in many places. You need to call self.login(), self.new_user(), self.close(), etc.

    4. You’re using Python 2, at least according to the question’s tags and there is one thing you need to remember when declaring classes in Python 2. There exist so called old- and new-style classes and what you want to do is use the new-style ones. Therefore your class must inherit from object:

      class Operations(object):
          # ...
      

      They’ve finally decided to drop the old-style classes support in Python 3 and there’s no need to explicitly inherit from object anymore, but while in Python 2, you need to cope with it.

    While there are still some errors or potential errors (what is close_connection()?), I think it’s enough for a good start ;). Good luck.

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:

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.