python sqlite3

1)sqlite3工具

SQLite Database Browser可以直接打开sqlite保持的数据库,而且支持导入csv文件,相当方便。 

 

2) python2.7内置对sqlite3的支持。

下面是解析文件,把url存入数据库的例子。  

 1 '''
2 Created on 2011-12-23
3
4 @author: Administrator
5 '''
6
7 import sqlite3
8
9
10 def sanitize_url(url):
11 pass
12
13 def store_url():
14 conn = sqlite3.connect("url.db")
15 cursor = conn.cursor()
16 cursor.execute('''create table if not exists URL(url text unique)''')
17
18 count = 0
19 for line in open("logfile.txt"):
20 url = sanitize_url(line)
21 if url is None:
22 continue
23 try:
24 print url
25 cursor.execute('INSERT INTO URL(url) values (?)', (url,))
26 count += 1
27 if not (count % 1000):
28 conn.commit()
29 except Exception, e:
30 print "store_url:" + str(e)
31 continue
32
33 conn.commit()
34 cursor.close()
35 conn.close()
36
37 if __name__ == '__main__':
38 pass



你可能感兴趣的:(sqlite3)