python3生成随机数据,并存入sqlite3-创新互联
#!/usr/bin/python #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/6/15 22:46 # @Author : Kwan # @File : insert_db.py # @Software: PyCharm import sqlite3 import random import datetime # conn = sqlite3.connect('local.db') # # c = conn.cursor() # # c.execute("insert into system_cfg values(?,?,?)",(2,'test2',1)) # # conn.commit() # # conn.close() def make_date(): # def make_card_number(bits): # counter = bits # number_list = [] # while counter: # number_list.append(str(random.randrange(0, 10))) # counter -= 1 # return number_list number_list = [str(x) for x in range(0, 10)] card_number = '' card_number = card_number.join(tuple(random.choices(number_list,k=10))) # card_number = '' # card_number = card_number.join(tuple(make_card_number(10))) car_type = random.choice(('m','p')) localtime = datetime.date.isoformat(datetime.datetime.now()) data_title = ['card_number','car_type','localtime'] data_dict = dict.fromkeys(data_title) data_dict['card_number'] = card_number data_dict['car_type'] = car_type data_dict['localtime'] = localtime return data_dict def insert_data(list): conn = sqlite3.connect('test.db') c = conn.cursor() try: id = max(c.execute("select max(id) from monthly_card").fetchall()) max_id = int(id[0]) for data in list: card_number = data['card_number'] db_car_number = c.execute("select card_number from monthly_card").fetchall() if card_number in db_car_number: break else: car_type = data['car_type'] localtime = data['localtime'] max_id += 1 c.execute("insert into monthly_card values (?,?,?,?)", (max_id, card_number, car_type, localtime)) except sqlite3.OperationalError: sql = '''create table if not exists monthly_card (id int primary key not null, card_number text not null, car_type text not null, valid text not null);''' c.execute(sql) id = 0 for data in list: try: card_number = data['card_number'] db_car_number = c.execute("select card_number from monthly_card").fetchall() if card_number in db_car_number: break else: car_type = data['car_type'] localtime = data['localtime'] id += 1 c.execute("insert into monthly_card values (?,?,?,?)", (id, card_number, car_type, localtime)) except sqlite3.OperationalError: card_number = data['car_number'] car_type = data['car_type'] localtime = data['localtime'] id += 1 c.execute("insert into monthly_card values (?,?,?,?)", (id, card_number, car_type, localtime)) # print(type(id_1)) # id = id_1.fetchall() conn.commit() conn.close() # def select_data(): # conn = sqlite3.connect('test.db') # c = conn.cursor() # db_car_number = c.execute("select card_number") # test1 = make_date() # my_list = [] # my_list.append(test1) # insert_data(my_list) # print('OK') n = 500 my_list = [] while n: my_list.append(make_date()) n-=1 insert_data(my_list) print('OK')
创新互联建站是一家专业的成都网站建设公司,我们专注网站制作、网站建设、网络营销、企业网站建设,买友情链接,一元广告为企业客户提供一站式建站解决方案,能带给客户新的互联网理念。从网站结构的规划UI设计到用户体验提高,创新互联力求做到尽善尽美。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前文章:python3生成随机数据,并存入sqlite3-创新互联
网页路径:http://azwzsj.com/article/iohoo.html