Note that there are some explanatory texts on larger screens.

plurals
  1. POConnector/python select after insert
    primarykey
    data
    text
    <p>I have a database table with UNIQUE key. If I want to insert some record there are two possible ways. First, the unique item doesn't exist yet, that's OK, just return new id. Second, the item already exists and I need to get the id of this unique record.</p> <p>The problem is, that anything I try, I get always some exception.</p> <p>Here's example of the code:</p> <pre><code>def __init__(self, host, user, password, database): # set basic attributes super().__init__(host, user, password, database) #open connection try: self.__cnx = mysql.connector.connect( database=database, user=user, password=password, host = host) #self.__cursor = self.__cnx.cursor() except ... def insert_domain(self, domain): insertq = "INSERT INTO `sp_domains` (`domain`) VALUES ('{0}')".format(domain) cursor = self.__cnx.cursor() try: cursor.execute(insertq) print("unique") except (mysql.connector.errors.IntegrityError) as err: self.__cnx.commit() print("duplicate") s = "SELECT `domain_id` FROM `sp_domains` WHERE `domain` = '{0}';".format(domain) try: id = cursor.execute(s).fetchone()[0] except AttributeError as err: print("Unable to execute the query:", err, file=sys.stderr) except mysql.connector.errors.ProgrammingError as err: print("Query syntax error:", err, file=sys.stderr) else: self.__cnx.commit() cursor.close() </code></pre> <p>but anyting I try, on the first duplicate record I get either 'MySQL Connection not available', 'Unread result'. The code is just example to demonstrate it.</p> <p>This is my first program using Connector/python, so I don't know all the rules, about fetch the results, commiting queries and so on. </p> <p>Could anyone help me with this issue, please? Or is there any efficient way to such task ('cause this one seems to be not the best solution to me). Thank you for any advice.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload