Difference between revisions of "User:Alvonruff/Python3"

From ISFDB
Jump to navigation Jump to search
(New page: There are two major steps to move to python3: * Update to the official MySQL connector. There is no python3 support for MySQLdb, which is our current connector. Moving to a new connector...)
 
Line 1: Line 1:
 
 
There are two major steps to move to python3:
 
There are two major steps to move to python3:
  
Line 11: Line 10:
 
* pip install mysql-connector-python
 
* pip install mysql-connector-python
  
I haven't had any luck yet getting the connector to work with python2.7 (it generates TLS errors).
+
I haven't had any luck yet getting the connector to work with python2.7 (it generates TLS errors). The MySQL page states: "Python 2.7 support was removed in Connector/Python 8.0.24"
  
 
===Queries That Return a Single Record===
 
===Queries That Return a Single Record===

Revision as of 08:29, 6 June 2022

There are two major steps to move to python3:

  • Update to the official MySQL connector. There is no python3 support for MySQLdb, which is our current connector. Moving to a new connector is a prerequisite.
  • Update to python3

MySQL Connector Notes

To install on python3:

  • pip install mysql-connector-python

I haven't had any luck yet getting the connector to work with python2.7 (it generates TLS errors). The MySQL page states: "Python 2.7 support was removed in Connector/Python 8.0.24"

Queries That Return a Single Record

The algorithm using MySQLdb to fetch a single record is:

    query = "select author_birthplace from authors where author_id='%d'" % int(authorID)
    db.query(query)
    result = db.store_result()
    record = result.fetch_row()
    return record[0][0]

The method using mysql.connector to fetch a single record would be:

    query = "select author_birthplace from authors where author_id='%d'" % int(authorID)
    cursor = db.cursor()
    cursor.execute(query)
    record = cursor.fetchone()
    return record[0]

Queries That Return Multiple Record

The algorithm using MySQLdb to fetch multiple records is:

    query = "select pubs.* from pubs,pub_authors where pub_authors.author_id=%d and pubs.pub_id=pub_authors.pub_id;" % aurec
    db.query(query)
    result = db.store_result()
    pub = result.fetch_row()
    results = []
    while pub:
        results.append(pub[0])
        pub = result.fetch_row()
    return results

The method using mysql.connector to fetch a single record would be:

    query = "select pubs.* from pubs,pub_authors where pub_authors.author_id=%d and pubs.pub_id=pub_authors.pub_id;" % aurec
    cursor = db.cursor()
    cursor.execute(query)
    results = []
    record = cursor.fetchmany()
    while record:
        results.append(record[0])
        record = cursor.fetchmany()
    return results