示例#1
0
def main():
    """Entyr point."""
    db = mysql.connector.connect(option_files="config.conf", use_pure=True)
    cursor = db.cursor()

    # cmd_query_iter()
    converter = MySQLConverter(db.charset, True)

    q1 = """SELECT Name, CountryCode, Population
            FROM world.city
            WHERE CountryCode = 'USA'
            ORDER BY Population DESC
            LIMIT 3"""
    q2 = "DO SLEEP(3)"
    q3 = """SELECT Name, CountryCode, Population
            FROM world.city
            WHERE CountryCode = 'IND'
            ORDER BY Population DESC
            LIMIT 3"""

    queries = [q1, q2, q3]

    result = db.cmd_query_iter(";".join(queries))

    # Iterate throw the results
    count = 0
    # It is one of the SELECT statements
    # as it has coumn definitions
    # print the result.
    for results in result:
        count = count + 1
        time = datetime.now().strftime('%H:%M:%S')
        print(f"query {count} - {time}\n")

        if 'columns' in results:
            print(f"{'City':18s}  {'Code':7s}  {'Popul':3s}")
            city, eof = db.get_row()
            while not eof:
                values = converter.row_to_python(city, results["columns"])
                print(
                    f"{values[0]:18s}  {values[1]:7s}  {values[2]/10000000:3f}"
                )
                city, eof = db.get_row()
        else:
            print("No results.")

        sleep(2)
        print("")

    cursor.close()
    db.close()
示例#2
0
      ORDER BY Population DESC"""
)

# Fetch the rows
(cities, eof) = db.get_rows()

# Initialize the converter
converter = MySQLConverter(
  db.charset, True)

# Print the rows found
print(__file__ + " - Using MySQLConverter:")
print("")
print(
  "{0:15s}   {1:7s}   {2:3s}".format(
    "City", "Country", "Pop"
  )
)
for city in cities:
  values = converter.row_to_python(
    city, result["columns"])
  print(
    "{0:15s}   {1:^7s}   {2:4.1f}".format(
      values[0],
      values[1],
      values[2]/1000000.0
    )
  )

db.close()