请以通俗易懂的方式解释cursor.execute。另外,如何打印cursor.execute的结果

时间:2018-10-01 08:21:41

标签: mysql python-3.x

 import mysql.connector as conn

 mydb = conn.connect(
 host="localhost",
 user="root",
 passwd="password",
 database="python_mysql"
 )

 mycursor = mydb.cursor()
  

计算数据库“ python_mysql”中的表数

  mycursor.execute("SELECT FOUND_ROWS();") #This contains 3 as result
     

如何通过使用新变量将结果打印为3

2 个答案:

答案 0 :(得分:1)

通过获取和分配,我们可以获得计数

mycursor.execute("SELECT FOUND_ROWS();") 
data = mycursor.fetchall()
print(data[0][0]) # prints 3

答案 1 :(得分:0)

import mysql.connector as conn
mydb = conn.connect(
host="localhost",
user="root",
passwd="password",
database="python_mysql"
)

mycursor = mydb.cursor()

mycursor.execute("select count(*) from information_schema.tables where table_schema = database();") 

data = mycursor.fetchall()

print(data[0][0])