1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| import pymysql
config = { 'host': 'localhost', 'port': 3306, 'user': 'root', 'password': '123456', 'database': 'reptiledata' } db = pymysql.connect(**config) cursor = db.cursor()
cursor.execute("drop table if exists user")
cursor.execute("""create table if not exists user( id int auto_increment primary key, name varchar(5), sex char(1), id_card char(18), phone varchar(14), address varchar(12), create_time time)""")
cursor.execute('alter table user AUTO_INCREMENT=1000')
cursor.execute('alter table user add plus varchar(8)')
cursor.execute('alter table user drop plus')
cursor.execute("""insert into user(name, sex, id_card, phone, address) values ('李四', '女', '511569845612354879', '10086', '数据不会被添加')""") db.rollback()
sql = """insert into user(name, sex, id_card, phone, address) values (%s, %s, %s, %s, %s)""" sql_data = [['张三', '女', '511569845612354879', '10086', '太古里77号'], ['李四', '男', '511569845612354879', '10086', '太古里77号'], ['王麻子子', '男', '511569845612354879', '10086', '太古里66号'], ['张二', '女', '511569845612354879', '10086', '太古里77号'], ['李五', '男', '511569845612354879', '10086', '太古里88号'], ['王麻子', '女', '511569845612354879', '10086', '太古里99号']] cursor.executemany(sql, sql_data)
cursor.execute("select address,id from user order by id desc") select = cursor.fetchall() print(select)
cursor.execute("select distinct address from user") select = cursor.fetchall() print(select)
cursor.execute("select id, name, sex, id_card from user limit 3,6") select = cursor.fetchall() print(select)
cursor.execute("select * from user where ID in (1001,1003)") select = cursor.fetchall() print(select)
cursor.execute("select * from user where ID between 1001 and 1003") select = cursor.fetchall() print(select)
cursor.execute("select * from user where name like '_麻%'") select = cursor.fetchall() print(select)
cursor.execute("select * from user where sex='女' and address='太古里77号'") select = cursor.fetchall() print(select)
cursor.execute("select * from user where name='王麻子子' or address='太古里77号' ") select = cursor.fetchall() print(select)
cursor.execute("delete from user where name='王麻子子' or address='太古里77号'")
cursor.execute("truncate table user")
cursor.execute("update user set name = '更新' where ID in (1005,1006)")
cursor.close() db.commit() db.close()
|