Ошибка типа: не все аргументы конвертируются при форматировании строки MySql - PullRequest
0 голосов
/ 27 марта 2020

Редактировать: решил go с другим решением, но не знал, как это закрыть.

Понятия не имею, что не так с моим кодом. Я использую PyCharm (Python3) и MySql. Я хочу иметь возможность добавить «%» в конец значения столбца для процентов. Код не помещается в одну строку, поэтому я разделил его, используя ''. Для строки% я заключил ее в двойные кавычки по сравнению с кавычками разделителя строк.

Я получаю вышеуказанную ошибку субъекта.

Это моя функция:

    # this function displays a bar chart according to age and gender chosen by user
    def bar_chart_ga():
        logging.debug("Executing: bar_chart_ga")
        # prints message to the screen with choices and stores choice in a variables "age" & 
          "gender"
        age = str(input("Enter the age you wish to use in the bar chart: ") + " years")
        gender = pyip.inputMenu(['Males', 'Females'], numbered=True)
        try:
            with mydb.cursor() as cursor:
                # prepared statement to update a record in the database
                select_query = ('SET @total=(SELECT COUNT(*) FROM myTable); '
                                'SELECT student_response, concat(ROUND(100*Count(student_response) / @total), "%")'
                                'AS Percentage FROM mytable WHERE sex=%s and age_group=%s GROUP BY student_response;')

                # executes the query using the user input stored in the age and gender variables
                cursor.execute(select_query, (age, gender))
                x = from_db_cursor(cursor)
                print("Gender  " + gender + ", age  " + age + " years: ")
                print(x)
                # closes the cursor object if still connected to database
                cursor.close()

        except pymysql.DatabaseError as error:
            # if no connection to database
            print("Display bar chart failed to execute {}".format(error))
            # tells the user the input is invalid and goes back thru the bar_chart function
            bar_chart_ga()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...