|
| 1 | +#!/usr/bin/env python |
| 2 | +#-*-coding:utf-8-*- |
| 3 | + |
| 4 | +""" |
| 5 | +第 0002 题:将 0001 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中。 |
| 6 | +""" |
| 7 | +import MySQLdb |
| 8 | +import string |
| 9 | +import random |
| 10 | + |
| 11 | +KEY_LEN = 20 |
| 12 | +KEY_ALL = 200 |
| 13 | + |
| 14 | +def base_str(): |
| 15 | + return (string.letters+string.digits) |
| 16 | + |
| 17 | +def key_gen(): |
| 18 | + keylist = [random.choice(base_str()) for i in range(KEY_LEN)] |
| 19 | + return ("".join(keylist)) |
| 20 | + |
| 21 | +def key_num(num,result=None): |
| 22 | + if result is None: |
| 23 | + result = [] |
| 24 | + for i in range(num): |
| 25 | + result.append(str(key_gen())) |
| 26 | + return result |
| 27 | + |
| 28 | +class mysql_init(object): |
| 29 | + |
| 30 | + def __init__(self,conn): |
| 31 | + self.conn = None |
| 32 | + |
| 33 | + #connect to mysql |
| 34 | + def connect(self): |
| 35 | + self.conn = MySQLdb.connect( |
| 36 | + host = "localhost", |
| 37 | + port = 3306, |
| 38 | + user = "root", |
| 39 | + passwd = "123456", |
| 40 | + db = "test", |
| 41 | + charset = "utf8" |
| 42 | + ) |
| 43 | + |
| 44 | + def cursor(self): |
| 45 | + try: |
| 46 | + return self.conn.cursor() |
| 47 | + except (AttributeError,MySQLdb.OperationalError): |
| 48 | + self.connect() |
| 49 | + return self.conn.cursor() |
| 50 | + |
| 51 | + def commit(self): |
| 52 | + return self.conn.commit() |
| 53 | + |
| 54 | + def close(self): |
| 55 | + return self.conn.close() |
| 56 | + |
| 57 | +def process(): |
| 58 | + dbconn.connect() |
| 59 | + conn=dbconn.cursor() |
| 60 | + DropTable(conn) |
| 61 | + CreateTable(conn) |
| 62 | + InsertDatas(conn) |
| 63 | + QueryData(conn) |
| 64 | + dbconn.close() |
| 65 | + |
| 66 | +#def execute(sql): |
| 67 | +# '''执行sql''' |
| 68 | +# conn=dbconn.cursor() |
| 69 | +# conn.execute(sql) |
| 70 | + |
| 71 | +#def executemany(sql, tmp): |
| 72 | +# '''插入多条数据''' |
| 73 | +# conn=dbconn.cursor() |
| 74 | +# conn.executemany(sql,tmp) |
| 75 | + |
| 76 | +def query(sql,conn): |
| 77 | + '''查询sql''' |
| 78 | + #conn=dbconn.cursor() |
| 79 | + conn.execute(sql) |
| 80 | + rows = conn.fetchall() |
| 81 | + return rows |
| 82 | + |
| 83 | +def DropTable(conn): |
| 84 | + #conn=dbconn.cursor() |
| 85 | + conn.execute("DROP TABLE IF EXISTS `user_key`") |
| 86 | + |
| 87 | +def CreateTable(conn): |
| 88 | + #conn=dbconn.cursor() |
| 89 | + sql_create =''' CREATE TABLE `user_key` (`key` varchar(50) NOT NULL)''' |
| 90 | + conn.execute(sql_create) |
| 91 | + |
| 92 | +def InsertDatas(conn): |
| 93 | + #conn=dbconn.cursor() |
| 94 | + #insert_sql = "insert into user_key values(%s)" |
| 95 | + insert_sql = "INSERT INTO user_key VALUES (%(value)s)" |
| 96 | + key_list = key_num(KEY_ALL) |
| 97 | + #print len(key_list) |
| 98 | + #conn.executemany(insert_sql,str(key_listi)) |
| 99 | + #conn.executemany("INSERT INTO user_key VALUES (%(value)s)",[dict(value=v) for v in key_list]) |
| 100 | + conn.executemany(insert_sql,[dict(value=v) for v in key_list]) |
| 101 | + |
| 102 | +def DeleteData(): |
| 103 | + del_sql = "delete from user_key where id=2" |
| 104 | + execute(del_sql) |
| 105 | + |
| 106 | +def QueryData(conn): |
| 107 | + sql = "select * from user_key" |
| 108 | + rows = query(sql,conn) |
| 109 | + printResult(rows) |
| 110 | + |
| 111 | +def printResult(rows): |
| 112 | + if rows is None: |
| 113 | + print "rows None" |
| 114 | + for row in rows: |
| 115 | + print row |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + dbconn = mysql_init(None) |
| 119 | + process() |
0 commit comments