Bug #87777 Wrong explanation for Row.get_string() and weird usage
Submitted: 15 Sep 2017 14:17 Modified: 18 Jun 2018 21:46
Reporter: Shahriyar Rzayev Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / Python Severity:S2 (Serious)
Version:8.0.4 OS:Any
Assigned to: CPU Architecture:Any

[15 Sep 2017 14:17] Shahriyar Rzayev
Description:
Hi dear all,

Related report:
https://bugs.mysql.com/bug.php?id=87776

According to the DOC:

https://dev.mysql.com/doc/dev/connector-python/mysqlx.Row.html

"
get_string(str_index)

    Returns the value if the index by string.
    Parameters:	str_index (str) – The string index.
"

Returns the value if the index by string. -> could not clearly understand this thing.
Parameters:	str_index (str) – The string index. -> the string index?

According to my tests:

import mysqlx

class RocksBulk:
    # The Class for Rocksdb Bulk Load
    def __init__(self):
        # Connect to a dedicated MySQL server
        self.session = mysqlx.get_session({
            'host': 'localhost',
            'port': 33060,
            'user': 'bakux',
            'password': 'Baku12345',
            'ssl-mode': mysqlx.SSLMode.DISABLED
        })

    def run_select_statement(self, schema_name, table_name):
        try:
            command = "select count(*) from {}.{}"
            sql = self.session.sql(command.format(schema_name, table_name))
            cursor = sql.execute()
            print "Returned from sql.execute()"
            print cursor

            result = cursor.fetch_all()
            print "Returned from cursor.fetch_all()"
            print result
            for i in result:
                print i.get_string('0')

        except Exception as e:
            raise
        else:
            return cursor

k = RocksBulk()
k.run_select_statement('employees', 'salaries2')

The result will be something like:

$ python test.py
Returned from sql.execute()
<mysqlx.result.SqlResult object at 0x2390dd0>
Returned from cursor.fetch_all()
[<mysqlx.result.Row object at 0x2390f90>]
Traceback (most recent call last):
  File "test.py", line 36, in <module>
    k.run_select_statement('employees', 'salaries2')
  File "test.py", line 28, in run_select_statement
    print i.get_string(0)
  File "/usr/lib64/python2.7/site-packages/mysqlx/result.py", line 617, in get_string
    raise ValueError("Column name '{0}' not found".format(str_index))
ValueError: Column name '0' not found

Row.get_string() requires column name as a parameter?

If you use something like:

print i.get_string('count(*)') -> select count(*) query

The result will be OK:

$ python test.py
Returned from sql.execute()
<mysqlx.result.SqlResult object at 0x2468dd0>
Returned from cursor.fetch_all()
[<mysqlx.result.Row object at 0x2468f90>]
2

How to repeat:
See description
[20 Sep 2017 12:14] MySQL Verification Team
Hello Shahriyar,

Thank you for the report and feedback.

Thanks,
Umesh
[20 Sep 2017 14:11] Nuno Mariz
Hi Shahriyar,
The documentation of 'get_string()' is not clear.
The 'mysqlx.Row.get_string()' returns the value of the column name provided.

For example, if you have:

    command = "select count(*) as total from {}.{}"

You can:

    self.session.sql(command.format(schema_name, table_name))
    result = sql.execute()
    for row in result.fetch_all():
        print(row.get_string("total"))
        or
        print(row["total"])

Since you have only on column in the SQL query, you also can do row[0] (by index).
We will update the reference documentation regarding this method.

Thank you.
[18 Jun 2018 21:46] Philip Olson
Posted by developer:
 
Fixed as of the upcoming MySQL Connector/Python 8.0.12 release, and here's the changelog entry:

  Deprecated the Row.get_string() method in favor of __getitem__.

Thank you for the documentation bug report, which ultimately lead to deprecating the method.