Python向Sqlite批量插入数据,测试硬盘性能

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sqlite3
from faker import Faker

conn = sqlite3.connect('test.db')
print "Opened database successfully";

cur = conn.cursor()
cur.execute("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'person'")
if not cur.fetchone():
    conn.execute('''CREATE TABLE person
    (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT,
    country TEXT,
    state TEXT,
    city TEXT,
    address TEXT,
    postcode TEXT,
    latitude REAL,
    longitude REAL,
    phoneNumber TEXT,
    birthday DATE,
    email TEXT
    );''')
    print "Table created successfully"

fake = Faker()

i = 0
while i < 10000:
    sql = 'INSERT INTO person (name, country, state, city, address, postcode, latitude, longitude, phoneNumber, birthday, email) VALUES (?,?,?,?,?,?,?,?,?,?,?);'
    data = []
    j = 0
    while j < 500:
        # sql = sql + '(?,?,?,?,?,?,?,?,?,?,?),'
        data.append((
            fake.name(), fake.country(), fake.state(), fake.city(), fake.address(), fake.postcode(),
            float(fake.latitude()),
            float(fake.longitude()), fake.phone_number(), fake.date(), fake.email()))
        j = j + 1
    sql = sql[:-1] + ';'
    cur.executemany(sql, data)
    i = i + 1

conn.commit()
cur.execute('SELECT  * FROM person')
print cur.fetchall()
conn.close()

    原文作者:丑矬穷
    原文地址: https://www.jianshu.com/p/6f78b58535fb
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞