#!/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')
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前名稱:python3生成隨機(jī)數(shù)據(jù),并存入sqlite3-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://muchs.cn/article0/iohoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、自適應(yīng)網(wǎng)站、域名注冊(cè)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容