python 使用python sqlite3 adapter创建并填充sqlite数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用python sqlite3 adapter创建并填充sqlite数据库相关的知识,希望对你有一定的参考价值。

# 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'

以上是关于python 使用python sqlite3 adapter创建并填充sqlite数据库的主要内容,如果未能解决你的问题,请参考以下文章

Python 之 Sqlite3数据库

Python 之 Sqlite3数据库

FreeBSD中Python3使用pip,scrapy,sqlite3问题

python使用上下文管理器实现sqlite3事务机制

Python3.5 使用Sqlite3

python内置的sqlite3模块,使用其内置数据库