import pymysql
db = pymysql.connect(host='localhost',user='root',passwd='',db='jodb1',port=3307,charset='utf8')
# #测试连接开发库成功
# db = pymysql.connect(host='172.31.20.2',user='root',passwd='sjroot',port=3306,charset='utf8')
# print('heh')
#数据中创建表
cursor = db.cursor()
cursor.execute('drop table if exists new_table2')
print('Success!')
sql = '''create table employee3 (
first_name char(20) not null,
last_name char(20),
age int ,
sex char(1),
income float)
'''
# 这样修改语法有错误
# sql = '''create table employee3 if not exists employee3(
# first_name char(20) not null,
# last_name char(20),
# age int ,
# sex char(1),
# income float)
# '''
cursor.execute(sql)
db.close()
print('table create Successully!')
sql = '''insert into employee
(first_name,last_name,age,sex,income)
values('Jiang','LiGai',20,'F',2000)'''
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
print('data insert successully')
#数据没有插入成功,但是也没有出错
# sql2 = 'insert into employee(first_name,last_name,age,sex,income)values({0},{1},{2},{3},{4})'.format('Jiang','PeiRong',21,'F',3000)
# try:
# cursor.execute(sql2)
# db.commit()
# except:
# db.rollback()
# print('data insert successully2')