# -*- coding:utf-8 -*- import pymysql # mysql_python python2 中使用者个包支持python操做mysql # 1连接数据库 db = pymysql.connect( # 连接的数据库的host主机地址:默认本地数据库使用localhost或者127.0.0.1,若是是远程数据库。须要设置为主机ip地址 host="localhost", #连接数据库的用户名 user="root", # 连接数据库的密码 password="123456", # 端口号3306 mysql默认端口号 #80端口 http协议的默认单开 # 443端口 https协议的默认端口 port=3306, #连接数据库的名字 db="student", # 插入中文 须要配置这两个参数 user_unicode=True, charset="utf8") # 2 获取游标 #同sqlit3 sql语句同样 cursor = db.cursor() # 1 auto_increment 自增量 create_tabl = "CREATE TABLE IF NOT EXISTS stu(name text, age integer)" cursor.execute(create_tabl) # 插入数据 insert_sql = "INSERT INTO stu(name,age)VALUES(0,'张凡',22)" cursor.execute(insert_sql)