2

I am having an issue with mysql connector. I did search for quite a while, but have found nothing.

If I execute the first file that just has the query, it works as expected. But if I try to make a db class and put the query in there, it returns None. I have taken out the location variable and it's the same. No matter what I try to query on, it returns None. I even tried to do a "SHOW TABLES", and it returned None.

Also, I have run this in a debugger and looked at the cursor object to be sure, as well as the mysql general log. The query is correct and everything looks as it should to me.

This is my first try with python and I am hoping the solution is some simple newbie mistake.

The query that works: test.py

import mysql.connector

_config = {
    'user': 'user',
    'password': 'password',
    'host': '127.0.0.1',
    'database': 'testdb',
    'raise_on_warnings': True,
    }
cnx = mysql.connector.connect(**_config)

cursor = cnx.cursor()
query = ("SELECT * FROM testtbl WHERE location=%s")
location='HERE'
cursor.execute(query, (location, ))

print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
  print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")

cursor.close()
cnx.close()

The ones that do not work: somedb.py

import mysql.connector

class SomeDB(object):
    def __init__(self):
        _config = {
            'user': 'user',
            'password': 'password',
            'host': '127.0.0.1',
            'database': 'testdb',
            'raise_on_warnings': True,
        }
        self.conn = mysql.connector.connect(**_config)
        self.cur = self.conn.cursor()

    def get_stuff(self):
        query = ("SELECT * FROM testtbl WHERE location=%s")
        location="HERE"
        result = self.cur.execute(query, (location, ))
        return result

    def __del__(self):
        self.conn.close()

Following the advice from Alu, I changed the get_stuff method to this:

def get_nodes(self):
    query = ("SELECT * FROM testtbl WHERE location=%s")
    location="HERE"
    cursor = self.cur.execute(query, (location, ))
    list = []
    for (thing) in cursor:
        list.append(([thing[0],thing[1]]))
    return list

test2.py

import somedb

db = somedb.SomeDB()

cursor = db.get_stuff()
print("--- " + str(cursor) + " ----")
for (stuff) in cursor:
  print("stuff: '" + stuff[0] + "', more stuff: '" + stuff[1] + "'")

UPDATE

Ok, I cannot get this to work. I have gone through this code with a debugger, and class abstraction aside, all else appears to be equal. So let me refine my question: is this possible with any mysql driver? This is what I want to do: https://softwareengineering.stackexchange.com/questions/200522/how-to-deal-with-database-connections-in-a-python-library-module

UPDATE2

I found it!

    result = self.cur.execute(query, (location, ))
    return result

Needs to be, simply:

    self.cur.execute(query, (location, ))
    return self.cur

1 Answer 1

1

As far as i remember you have to commit on the connection after every transaction.

You can try to use this as an example. It's my database class. https://github.com/CatCookie/DomainSearch/blob/master/src/additional/database.py

Sign up to request clarification or add additional context in comments.

7 Comments

I tried that, but it threw an error that the cursor had unread data. The documentation says that commit only applies to operations that change the data: This method sends a COMMIT statement to the MySQL server, committing the current transaction. Since by default Connector/Python does not autocommit, it is important to call this method after every transaction that modifies data for tables that use transactional storage engines.
Jap, you have to read the cursor. The line result = cursor... doesnt work.
So I cannot simply assign the cursor to a var and return it? How can I accomplish that?
This did not work either. When it gets to the loop, it complains that you cannot iterate over Nonetype. So it is still not returning any data
Before you make a commit, read all the results from the cursor (just run cur.featchall(), you don't even have to assign it) and only then commit.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.