Difference between revisions of "User:Alvonruff/Python3"

From ISFDB
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
There are two major steps to move to python3:
 
There are two major steps to move to python3:
  
 +
* Fix the inconsistent tabs/spaces problem in numerous files (pretty much every file attempted so far). It appears that 8-spaces is far more popular than tabs.
 
* 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 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
+
* Update to python3. This can leverage the futurize work, by removing the "from __future__" lines.
 
+
* Change the default charset in MySQL
==MySQL Connector Notes==
+
* Repair strings which have URL encodings in MySQL
 
 
To install on python3:
 
 
 
* pip install mysql-connector-python
 
 
 
The connector generates TLS errors with python2.7, as "Python 2.7 support was removed in Connector/Python 8.0.24".
 
 
 
===Instantiating a Connection===
 
 
 
The algorithm using MySQLdb to connect to the database is:
 
 
 
<pre>
 
    db = MySQLdb.connect(DBASEHOST, USERNAME, PASSWORD, conv=_IsfdbConvSetup())
 
    db.select_db(DBASE)
 
    db.set_character_set("latin1")
 
</pre>
 
 
 
The method using mysql.connector to connect is:
 
 
 
<pre>
 
    db = mysql.connector.connect(user=USERNAME, password=PASSWORD, host=DBASEHOST, database=DBASE)
 
</pre>
 
 
 
===Queries That Return a Single Record===
 
 
 
The algorithm using MySQLdb to fetch a single record is:
 
 
 
<pre>
 
    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]
 
</pre>
 
 
 
The method using mysql.connector to fetch a single record would be:
 
 
 
<pre>
 
    query = "select author_birthplace from authors where author_id='%d'" % int(authorID)
 
    cursor = db.cursor()
 
    cursor.execute(query)
 
    record = cursor.fetchone()
 
    return record[0]
 
</pre>
 
 
 
===Queries That Return Multiple Record===
 
 
 
The algorithm using MySQLdb to fetch multiple records is:
 
 
 
<pre>
 
    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
 
</pre>
 
 
 
The method using mysql.connector to fetch a single record would be:
 
 
 
<pre>
 
    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
 
</pre>
 

Latest revision as of 19:31, 11 April 2023

There are two major steps to move to python3:

  • Fix the inconsistent tabs/spaces problem in numerous files (pretty much every file attempted so far). It appears that 8-spaces is far more popular than tabs.
  • 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. This can leverage the futurize work, by removing the "from __future__" lines.
  • Change the default charset in MySQL
  • Repair strings which have URL encodings in MySQL