每天一個python小程序

做爲 Apple Store App 獨立開發者,你要搞限時促銷,爲你的應用生成200個激活碼(或者優惠券),並將生成的激活碼(或者優惠券)保存到 MySQL 關係型數據庫中。

def getrand():
        while True:
                a=random.randint(48,122)
                if a<=57 or (a>=65 and a<=90) or (a>=97 and a<=122):
                        break
        return a
if __name__=='__main__':
        import random
        import pymysql
        array=[]
        for k in range (200):
                rand=''
                for i in range(6):
                        rand=rand+chr(getrand())
                array.append(rand)
        conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='',db='mysql')
        cur=conn.cursor()
        cur.execute('create table if not exists juan (ma varchar(20))')
        cur.executemany("insert into juan values(%s)",array)
        cur.close()
        conn.commit()
        conn.close()

使用的是python3.5版本的。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章