#!/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')
python3生成随机数据,并存入sqlite3
原创
©著作权归作者所有:来自51CTO博客作者kk_Linux的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
python sqlite3 operation
python sqlite3 类重写操作
python sqlite3 -
shell 直接写 sqlite3 sql shell怎么用
本来想是一个很简单的操作,可惜Oracle没有提供简单的一个命令(也许我不知道吧),只好进行一些复杂点的操作了。一般有三种方式实现: 1. 利用DBMS_PIPE包并创建OS上运行的守护进程; 2. 调用c程序来实现 3. 利用java的getRuntime().e
shell 直接写 sqlite3 shell oracle library exception