pbojinov
6/10/2014 - 4:57 PM

Create and populate a sqlite database using python sqlite3 adapter

Create and populate a sqlite database using python sqlite3 adapter

# first create a databse, from the command line run 
# > touch hello_world.db

from datetime import datetime, timedelta
import sqlite3
conn = sqlite3.connect('hello_world.db')

c = conn.cursor()

# Create table
c.execute('''
	CREATE TABLE tlds
	(tld text, tag text, vertical text, brand text, cc text, url text)''')

# sample dummy data
tld = 'abcdefghijkk' 
tag = 'abcdefghijk'
vertical = 'abcdefghijk' 
brand = 'abcdefghijk' 
cc = 'us'
url = 'abcdefghijklmnobqrstuvwxyz1234567890' 

query = "INSERT INTO tlds VALUES ('%s','%s','%s','%s','%s','%s')" % (tld, tag, vertical, brand, cc, url)
print query # to make sure we have the right thing

# do 2 million entries
for i in range(0,2000000):
	# Insert a row of data
	c.execute(query)
	print 'insert # %d' % i

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

print 'done'