创建数据库
import pymysqlconn = pymysql.connect(host="127.0.0.1", user="root", passwd="root")conn.query("CREATE DATABASE onepiece")
创建表
import pymysql# 连接数据库# conn=pymysql.connect(host="127.0.0.1",user="root",passwd="root",db="onepiece")conn = pymysql.connect("127.0.0.1", "root", "root", "onepiece")# 获取游标cursor = conn.cursor()# SQL语句sql = "create table mytable(id int(10) auto_increment primary key not null,name varchar(30),url varchar(100),hits int(15),comment int(15))"try: cursor.execute(sql) conn.commit()except: conn.rollback()# 关闭数据库连接conn.close()
查询是否创建成功
mysql -uroot -prootshow databases;use onepiece;show tables;desc mytable;