Python Crud with MySQL

Let’s do the same thing with MySql

pip install MySQL-python
import MySQLdb

class mydbconn():
    def __init__(self):
        self.con = MySQLdb.connect('x', 'x', 'x', 'x')

    def get_columns(self,table):
        cuc = self.con.cursor()
        cursData = cuc.execute("SHOW columns FROM " + table)
        HEADERS = cuc.fetchall()
        keyName = HEADERS[0][0]
        keyLen = self.lenFunction(HEADERS[0][1])
        return keyName, keyLen, HEADERS

    def lenFunction(self,varCharString):
        x = varCharString.find("(")
        varCharString = varCharString[x+1:]
        x = varCharString.find(")")
        varCharString = varCharString[:x]
        return int(varCharString)

if __name__=="__main__":
    con = mydbconn()
    keyName, keyLen, h = con.get_columns("customer")
    for i in h:
        print i

On the fifth line of the program, use your MySql credentials. Sometimes the first parameter is localhost, the second x is the username for the database, the password is third and the name of the database is fourth.

You also have to change the parameter in line 2 of the __main__ section to the name of the table you are using. This routine also has been used with databases that had a key other than named id for the first field, and it was important to have the name of the key and its length to proceed with scaffolding

In the Sqlite3 sqlalchemy example, we got a list of field names. In this example, we get a tuple of tuples. At this point, tuples might be a preferred format, they are immutable and hashable, and a utility crud function would not need to add and remove things from the tuple. However, in a more sophisticated scaffolding environment, you would add things like a field for password verification or especially remove things, like the ability to change certain fields.

0 thoughts on “Python Crud with MySQL”

  1. My auto approve lists can be used with ScrapeBox. I work hard to build the best lists possible.
    I hope that you’re able to put them to good use.
    Thank you for visiting my site. Feel free to
    contact me if you have any questions.

  2. Whats up are using WordPress for your site platform? I am new to the blog
    world but I am trying to get started and create my own. Do you need any coding knowledge
    to make your own blog? Any help would be greatly appreciated!

  3. Just wish to say your article is as amazing.
    The clearness in your post is simply great and i can assume you are an expert on this subject.
    Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming post.
    Thanks a million and please continue the rewarding work.

  4. Great beat ! I would like to apprentice while you amend your website, how can i subscribe for a blog website?
    The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast provided
    bright clear concept

  5. It is the best time to make some plans for the future and it
    is time to be happy. I have read this submit
    and if I could I desire to counsel you few interesting issues or
    suggestions. Perhaps you can write subsequent articles regarding this article.

    I desire to read more issues about it!

Leave a Reply

Your email address will not be published. Required fields are marked *