zoukankan      html  css  js  c++  java
  • python3与mysql:创建表、插入数据54

    
    
     1 import pymysql
     2 db = pymysql.connect(host='localhost',user='root',passwd='123456',db='jodb1',port=3307,charset='utf8')
     3 # #测试连接开发库成功
     4 # db = pymysql.connect(host='172.31.20.2',user='root',passwd='sjroot',port=3306,charset='utf8')
     5 # print('heh')
     6 #数据中创建表
     7 cursor = db.cursor()
     8 cursor.execute('drop table if exists new_table2')
     9 print('Success!')
    10 
    11 
    12 
    13 sql = '''create table employee3 (
    14 first_name char(20) not null,
    15 last_name char(20),
    16 age int ,
    17 sex char(1),
    18 income float)
    19 '''
    20 # 这样修改语法有错误
    21 # sql = '''create table employee3 if not exists employee3(
    22 # first_name char(20) not null,
    23 # last_name char(20),
    24 # age int ,
    25 # sex char(1),
    26 # income float)
    27 # '''
    28 cursor.execute(sql)
    29 db.close()
    30 print('table create Successully!')
    31 
    32 
    33 
    34 
    35 sql = '''insert into employee
    36           (first_name,last_name,age,sex,income)
    37           values('Jiang','LiGai',20,'F',2000)'''
    38 try:
    39     cursor.execute(sql)
    40     db.commit()
    41 except:
    42     db.rollback()
    43     db.close()
    44 print('data insert successully')
    45 
    46 
    47 #数据没有插入成功,但是也没有出错
    48 # sql2 = 'insert into employee(first_name,last_name,age,sex,income)values({0},{1},{2},{3},{4})'.format('Jiang','PeiRong',21,'F',3000)
    49 # try:
    50 #     cursor.execute(sql2)
    51 #     db.commit()
    52 # except:
    53 #     db.rollback()
    54 # print('data insert successully2')
    
    
    
     
  • 相关阅读:
    题解——[[SHOI2010]最小生成树]
    7.12周总结
    还有5个月就NOIP2019了,我干了什么
    【CQOI2018】破解D-H协议
    【SHOI2006】仙人掌
    【HNOI/AHOI2018】道路
    2019.11纪中集训 宋新波老师和曹天佑学长的勉励
    纪中集训2019.11.05
    【2019.10.25 OI-Killer的模拟赛】3.鸡数
    【华东师附国庆模拟赛】Day2 1.矩阵
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/9257536.html
Copyright © 2011-2022 走看看