Python/MySQL- How to properly use %s for counting rows

mysql, python

Solution

query = "SELECT COUNT(*) from `%s`" %table
cursor.execute(query)             #execute query separately
res = cursor.fetchone()
total_rows = res[0]      #total rows

Problem

I have made a function which counts the amount of rows there are in a table using the `cursor.rowcount`function in Python. Now I want to apply that to tables I choose through %s. The problem is that I don't know how to apply it. Here is the sample I am working with. ``` def data_input (table): cursor.execute ("USE database") cursor.execute ("TRUNCATE TABLE table1") cursor.execute ("TRUNCATE TABLE table2") cursor.execute ("LOAD DATA LOCAL INFILE 'table1data' INTO TABLE table1 FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' (field1, field2, field3, field4, field5)") cursor.execute ("LOAD DATA LOCAL INFILE 'table2data' INTO TABLE table2 FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n' (field1, field2, field3)") cursor.execute ("SELECT * FROM %s", table) print cursor.rowcount data_input ("table1") ``` Basically what it already does is input all the data into tables in MySQL from a text file, now I want the function to also print the number of rows for a particular table. It is getting an error message saying wrong MySQL syntax so this code is wrong for the rowcount part.

Original source