import pymysql
#参数一:服务器IP
#参数二:端口号
#参数三:用户名
#参数四:用户密码
#参数五:数据库名
conn = pymysql.connect(host="localhost",port=3306,user="root",password="mysql",db="smy_db")
#创建一个cursor对象,由cursor对象执行SQL语句以及获得执行结果
cur = conn.cursor()
sql_1="drop table if exists table_test;"
sql_2="create table table_test(id int auto_increment primary key, name char not null);"
sql_3="show tables;"
cur.execute(sql_1)
cur.execute(sql_2)
cur.execute(sql_3)
data = cur.fetchall() #fetchall()将所有结果赋值给data,fetchone()将第一个结果赋值给data
print(data)
#关闭cursor对象,然后关闭数据库连接
cur.close()
conn.close()
输出结果如下,可以看到已经有了table_test表