• 【python练习册】1.3 将1.2题生成的n个激活码保存到mysql关系型数据库中


    该题涉及到mysql中一些指令,先熟悉一下

    MySQL指令

    参考:https://www.cnblogs.com/zhuyongzhe/p/7686105.html

     1 mysql -u root -p                     以root权限登录,默认密码为空
     2 show databases;                  列出所有数据库
     3 drop table if exists hst;           如果存在表hst则先删除
     4 create database hst;                 创建数据库
     5 use hst;                          进入数据库
     6 create table student(
     7 id int auto_increment primary key,
     8 name varchar(50),
     9 sex varchar(20),
    10 date varchar(50),
    11 content varchar(100)
    12 )default charset=utf8;            创建表
    13 drop table student;                删除表
    14 describe student;               查看表结构(desc student;)
    15 
    16 insert into student values(null,'aa','','1988-10-12','.....');    插入数据
    17 select * from student;
    18 select id,name from student;             查询id,name列
    19 
    20 update student set sex='' where id=4;    修改id为4的行性别为男
    21 
    22 delete from student where id=5;          删除id为5的行
    23 
    24 select * from student where date>'1988-1-2' and date<'1988-12-1';    andor
    25 
    26 select * from student where date between '1988-1-2' and '1988-12-1';    between            

    python中使用MySQL指令

    安装了mysql,python3.0先装好pymysql,在IDLE中测试下import pymysql是否报错,没有报错,完美。

    1 import pymysql
    2 db = pymysql.connect("localhost", "root", "", "test")  连接test数据库
    3 cursor=db.cursor()
    4 cursor.execute(sql语句)
    5 db.commit()
    6 db.close()

    完整代码

     1 #!/user/bin/python
     2 #-*-coding:UTF-8 *-*
     3 
     4 import pymysql
     5 import random,string,uuid
     6 
     7 def generateN(len, count, cursor, db, database):
     8     getChars = lambda: string.ascii_letters+string.digits
     9     generate = lambda len: "".join(random.sample(getChars(), len))
    10     for i in range(count):
    11         sql = "insert into {0} (id,value) values ({1},'{2}')".format(database,"null", generate(len))
    12         cursor.execute(sql)
    13         db.commit()
    14 
    15 #generateN(10, 20)
    16 db = pymysql.connect("localhost", "root", "", "hst")
    17 cursor = db.cursor()
    18 cursor.execute("drop table if exists generateCodes")
    19 sql = "create table generateCodes(id int auto_increment primary key, value varchar(50))"
    20 cursor.execute(sql)
    21 cursor.execute("show tables");
    22 generateN(10, 20, cursor, db, "generateCodes")
    23 db.close()

    期间出了2个问题

    问题1:pymysql.err.InternalError: (1054, "Unknown column '***' in 'field list'")

    参考:https://blog.csdn.net/fk103/article/details/80139284,原因是字符串占位符那里没有写'',将(values ({1},{2})修改为(values ({1},'{2}'))

    问题2:插入第2个数据的时候Duplicate entry for key 'PRIMARY'

    参考:https://blog.csdn.net/m0_37664906/article/details/79612006,主键重复了,将(database,i,**)修改为(database,"null",**)

    执行脚本后,在控制台中查看表内容

    1 select * from generateCodes;

    好,结束。

  • 相关阅读:
    今天,你ak了吗?①
    线段树模板
    DP(关于字符串,数字串的)
    Leedsdiscussion
    高数积分求旋转体体积
    tiny mission
    莫队+数组低级化的 优先队列
    LAB2
    Leedslecturepronouncation
    Eclipse Access Restriction
  • 原文地址:https://www.cnblogs.com/huangsitao/p/10989352.html
Copyright © 2020-2023  润新知