#连接数据库
conn = MySQLdb.Connect(
host = 'localhost',
port = 3306,
user = 'root',
passwd = '',
db = 'mytest',
charset = 'utf8'
cursor = conn.cursor()
#创建一个表存储数据
sql_create = "create table if not exists account(nameid int, money varchar(100)) "
#插入两条数据
sql_insert1 = "insert into account(nameid,money) values(13,120)"
sql_insert2 = "insert into account(nameid,money) values(14,10)"
#执行上述sql语句
cursor.execute(sql_create)
cursor.execute(sql_insert1)
cursor.execute(sql_insert2)
conn.close()
python操作mysql数据库的完整demo与知识点讲解