Python Forum
Creating Dynamic Variable Names
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating Dynamic Variable Names
#1
I was looking around at the MySQLdb extension and noticed that returns are a field number instead of a field name.
I was working on building a function to convert, but I got stuck.
Everything works except the line where I have result= data[x] even when I have it not commented.

Stupid editor killed the post and it won't let me edit.
Edit admin:
Yes can keep formatting when copy,mark all and use "Remove Formatting" button.
import re
import MySQLdb

def fieldconverter( result ):
        statement = re.sub(r'\A(SELECT\s){1}|FROM(.*?)\Z', "", result)
        # Now we have  a list of fields.  Loop through them
        splitstatement = statement.split(", ")
        x = 0
        for a in splitstatement:
            print "Field Number: %s"  % x + " Field Name: %s" % a
            #result[a] = data[x]
            x += 1

        return result

print "Latias flies around at high speed."
# Connect to the database
db = MySQLdb.connect("siteground255.com", "superla9_latios", "R{-q*MNJILE+", "superla9_forums")

cursor = db.cursor()
sql = "SELECT uid, username, email FROM tpc_users WHERE username='Arceus'"
cursor.execute(sql)
data = cursor.fetchone()
result = fieldconverter(sql)
print "Username: %s " % data[1] + " Uid: %i" % data[0]
db.close()
Reply
#2
So result is a dictionary, which means you have to initialize it with an empty dictionary (result={} before the loop...)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
(Oct-21-2016, 09:24 PM)Ofnuts Wrote: So result is a dictionary, which means you have to initialize it with an empty dictionary (result={} before the loop...)

Thanks so much.  The code works perfectly now.  This makes it much easier to handle queries since knowing the position of a field can be tedious.  Now I just need to find a way to resolve queries that use all fields.
Reply
#4
I was able to get it to work for selecting all fields.

Final Code: https://github.com/dragonexpert/Python-M...convert.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  dynamic variable name declaration in OOP style project problem jacksfrustration 3 717 Oct-22-2023, 10:05 PM
Last Post: deanhystad
  Need help with creating dynamic columns with for loops for stock prices PaDat 2 859 Feb-22-2023, 04:34 AM
Last Post: PaDat
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,430 Jul-27-2022, 08:50 PM
Last Post: rob101
  [split] Creating a variable as a function DPaul 23 6,508 Sep-07-2020, 05:20 PM
Last Post: DPaul
  Creating a variable as a function JarredAwesome 4 2,783 Sep-06-2020, 05:08 AM
Last Post: buran
  Alternative to dynamic variable names catosp 13 4,458 Jun-20-2020, 03:45 PM
Last Post: catosp
  Help creating a variable as a result Realen 2 1,742 Jun-18-2020, 04:46 AM
Last Post: Realen
  Use dynamic variable from parallel running python script Sicksym 0 1,823 May-15-2020, 02:52 PM
Last Post: Sicksym
  Can I use iteration to create variable names? Mark17 8 6,703 Oct-17-2019, 06:05 AM
Last Post: perfringo
  creating a variable in a method and using in another method ridgerunnersjw 5 2,932 Oct-04-2019, 04:03 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020