Python3 generate random data and deposit to Sqlite3

Source: Internet
Author: User

#!/usr/bin/python#!/usr/bin/env python# -*- coding: utf-8 -*-#  @Time      : 2018/6/15 22:46#  @Author   : Kwan#  @File      : insert_db.py#  @Software: pycharmimport sqlite3import randomimport  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))  &NBsp;      #     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))     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_dictdef 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 ']&NBSp;               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 = 500my_list = []while  n:    my_list.append (Make_date ())     n-=1insert_data (my_list) Print (' OK ')


Python3 generate random data and deposit to Sqlite3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.