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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:10:51+00:00 2026-05-31T14:10:51+00:00

So some code in MainApp.py goes like this, the lines I am concerned about

  • 0

So some code in MainApp.py goes like this, the lines I am concerned about are where DBQuery occurs. DB302.py is in the same folder as MainApp.py and contains a class called DB302 with member methods query shown below. Why does it only work when I use DB302.DB302.query(DBQuery...) where DBQuery is already an instance of DB302.DB302... Is there a way to not have to use firstly, the instance DBQuery = DB302.DB302() instead just DBQuery = DB302 and also to be able to use it then as `DBQuery.query(Instance, var1, var2).

My second question pertains to the use of self.method or variable in DB302.py. Have I used them correctly? I am getting ThrowsU() showing up now, and can’t figure out why. My DB has been set up to work correctly and I can access it through the python terminal. Id is a primary key and I am incrementing it for each test case.

#MainApp.py
import DB302

class MainApp(object):

@cherrypy.expose
def default(self, *args, **kwargs):     
    page = "<b>&quot;args&quot; has %d variables</b><br/>\n" % len(args)
    for x in xrange(0, len(args)):
        if args[x] == "":
            page += "<i>error</i>\n"
        else:
            page += args[x] + "<br/>\n"

    page += "<b>&quot;cherrypy.request.params&quot; has %d variablez</b><br/>\n" % len(cherrypy.request.params)
    for key, value in cherrypy.request.params.items():
        if cherrypy.request.params[key] == "":
            page += "<i>empty</i>\n"
        elif key == "username":
            DBQuery = DB302.DB302()
            data = DB302.DB302.query(DBQuery, 'SELECT * FROM test', 'SELECT')
        else:
            page += key + " = " + value + "<br/>\n"

    page += """
            <form name="form1" method="POST" action="/testURL/part2">
                <input name="username" type="text" maxlength="256" autocomplete="off">
                </input>
            </form>
            """

    return page

#DB302.py
import sqlite3

class DB302(object):

    def __init__(self):
        print "<b>Within DB302</b><br/>\n"

    def throws(self):
        raise RuntimeError('Connection to DB failed, terminating...')

    def throwsQ(self):
        raise RuntimeError('Error in DB query, terminating...')

    def throwsU(self):
        raise RuntimeError('Unknown DB error encountered...')

    def connect(self):
        # Where the DB connection will be made
        CS302="<!-- Private -->" # The datbase file is on my desktop

        try:
            DBCon=sqlite3.connect(CS302)
            cursor=DBCon.cursor()
            return True
        except IOError:
            self.throws()
        except:
            self.throwsU()
        else:
            return False

    def disconnect(self):
        # Used to disconnect...

        try:
            self.cursor.close()
            return True
        except IOError:
            self.throws()
        except:
            self.throwsU()
        else:
            return False

    def query(self, queryString, queryType):
        # Queries will be made here

        if self.connect():
            try:
                self.cursor.execute(queryString)
                if queryType == "SELECT":
                    # return all the rows if there's a select query
                    allRows = self.cursor.fetchall()
                    return allRows
                elif queryType == "DELETE":
                    # return affected rows if there's a deletion
                    rowsAffected = self.cursor.rowcount()
                    self.DBCon.commit()
                    return rowsAffected
                elif queryType == "INSERT":
                    # return true if success
                    self.DBCon.commit()
                    return True
                elif queryType == "CREATE":
                    self.DBCon.commit()
                    return True
                else:
                    return False
            except IOError:
                self.throwsQ()
            except:
                self.throwsU()
        else:
            return False
  • 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-31T14:10:52+00:00Added an answer on May 31, 2026 at 2:10 pm

    To make calling your class method easier you can do:

    from DB302 import DB302 as DBQuery
    ...
    DBQuery.query(Arg1, Arg2)
    

    For your second question:
    You should avoid, when possible, using a so-called “naked” except, i.e. except:.
    This delightful construct has the unfortunate property of making debugging more difficult than it need be.

    So I would recommend instead taking the advice from here: http://wiki.python.org/moin/HandlingExceptions
    And catching all exceptions in such a way that you print out information about the exception:

    import sys
    ...
        except:
            e = sys.exc_info()[1]
            print e
    

    I also notice that your custom-exceptions do not inherit from Exception.
    This question and accompanying answers may provide some insight: Proper way to declare custom exceptions in modern Python?

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

Sidebar

Related Questions

Got some code that is not mine and its producing this warning atm: iehtmlwin.cpp(264)
In some code I've been reading, I've come across this : class Someclass {
Take some code like if (person.IsMale()) { doGuyStuff(); } else { doGirlStuff(); } Should
Some code style tools recommend this and I remember seeing some unix command line
I have this barebones code, I am trying to make it so some items
Some code for context: class WordTable { public: WordTable(); ~WordTable(); List* GetListByAlphaKey(char key); void
Some code for context: class a { } class b { public a a{get;set;}
some code snippets. The java coding doing the jaxb unmarshaling. pretty straightforward, copied out
In some code I've inherited, I see frequent use of size_t with the std
After some code review I removed unnecessary properties which resulted in empty rules. 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.