Created
October 22, 2016 08:17
-
-
Save sfantree/b0f8ef8fc1c8ff4ea182b711b65d050a to your computer and use it in GitHub Desktop.
Python向Sqlite批量插入数据,测试硬盘性能
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment