-3

My kwarg table in **kwarg is not getting recognizing when I invoke it.

class Database():
    def set_db_setting(self, host, username, passwd, database):
        try:
            self.host = host
            self.username = username
            self.passwd = passwd
            self.database = database
            self.db = pymysql.connect(host=host, user=username, passwd=passwd, db=database)
            print('connected to: {}'.format(database))
            return self.db
        except:
            print('\nerror connecting to database\n')

    def db_select(self, *selected_fields, **kwargs):
        self.selected_fields = selected_fields = list(selected_fields)
        self.table = (kwargs['table']
                      if 'table' in kwargs 
                      else selected_fields.pop())
        try:
            with self.db.cursor() as cursor:
                sql_tld_id_query = Database.query_stmt_list[0]+ ', '.join(selected_fields) + Database.query_stmt_list[4] + table + Database.query_stmt_list[5] + where_field + '=' + 'www.website.com'
                print("sql_tld_id_query is {}".format(sql_tld_id_query))
        except Exception as gatherid_err:
            print("exception was {}".format(gatherid_err))
            self.db.rollback()

I'm invoking it like:

    dbclass = Database()
    dbclass.set_db_setting('localhost', 'root', 'password', 'garbagedb')
    dbclass.db_select('id', 'name', table='tld', where_field='name')

I'm getting an error like:

name 'table' is not defined

FULL TRACEBACK invoked via:

import traceback
traceback.print_stack()

`

  File "dbcrud.py", line 56, in <module>
    dbclass.db_select('id', 'name', table='tld', where_field='name')
  File "dbcrud.py", line 31, in db_select
    traceback.print_stack()
self.selected_fields is ['id', 'name']
exception was name 'table' is not defined

What am I doing wrong here?

Jshee
  • 2,620
  • 6
  • 44
  • 60
  • 1
    If that tiny, tiny snippet of an error message is coming from `kwargs['table']`, then I can't reproduce your error. More details, please. –  Feb 10 '17 at 19:36
  • I agree with @JackManey: Post the entire stack trace. – TemporalWolf Feb 10 '17 at 19:37
  • Post the actual, exact error message, not just something vaguely like it. – user2357112 Feb 10 '17 at 19:42
  • 2
    "name 'table' is not defined" means that `table` it is not found as a variable name, thus the error cannot come from `kwargs['table']` (it would be a KeyError instead). Look where in your code you use a variable named 'table'. – JulienD Feb 10 '17 at 19:42
  • See the traceback above. – Jshee Feb 10 '17 at 19:43
  • I cannot reproduce. For me `db_select` works fine. Show the `set_db_setting` method as well. Sometimes the traceback marks the following line. – JulienD Feb 10 '17 at 19:53
  • @JulienD - please see above – Jshee Feb 10 '17 at 19:54
  • 2
    Why did you use `traceback.print_stack`? It's useless here, and it doesn't show the error message. – vaultah Feb 10 '17 at 19:54
  • Take the **complete, actual stack trace**, copy it, and paste it into your question. Please stop trying to waste our time. –  Feb 10 '17 at 19:57
  • @JackManey - please show me how – Jshee Feb 10 '17 at 19:58
  • 1
    @Jshee If you have no idea how to copy and paste text or what a stack trace is, then I'm not sure we can help you... –  Feb 10 '17 at 19:59
  • @Jshee usually when you run the program and it fails, it prints an error to console by itself. What are you using to run your Python program? (I've never seen this "exception was...") Use `python myscript.py` in a console and copy-paste everything you see after that point. – JulienD Feb 10 '17 at 20:00
  • @JulienD its hitting the `exception` block. updating code above – Jshee Feb 10 '17 at 20:02
  • @Jshee: Here's a hint: `exception was` is **not** a part of any stack trace (unless it's part of a custom error message that's thrown during an exception). –  Feb 10 '17 at 20:02
  • @Jshee: What is an "`exception` block"? Do you mean the `except` block? –  Feb 10 '17 at 20:03
  • @Jshee: **Again**, you have **not** provided the actual stack trace that was provided to you. –  Feb 10 '17 at 20:05
  • @Julien - please see updated. – Jshee Feb 10 '17 at 20:05
  • Can be closed as duplicate of http://stackoverflow.com/q/16586888/2301450. – vaultah Feb 10 '17 at 20:10

1 Answers1

0

I added line continuations to make this fit horizontally...

sql_tld_id_query = Database.query_stmt_list[0]+ ', '.join(selected_fields) + \
Database.query_stmt_list[4] + table + Database.query_stmt_list[5] + \
where_field + '=' + 'www.website.com'

the table in bold should be self.table

you blindly catch all exceptions with you try except Exception block, which probably was hiding the real issue from you. It is better to find out what specific kind of exception you want to catch, and only filter those out. For example if I wanted to have a calculator program that didn't crash when a user tried to divide by zero, I would use try: ... except ZeroDivisionError as e: ...

Aaron
  • 10,133
  • 1
  • 24
  • 40