1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import json
import os
import sqlite3
PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))
DATABASE = os.path.join(PROJECT_ROOT, 'bookmark.chromeExtension')
def initDB():
conn = None
try:
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute(
'CREATE TABLE bookmark (id INT PRIMARY KEY , parentId INT, indexId INT,url VARCHAR (20),title VARCHAR(20))')
cursor.close()
conn.commit()
except BaseException as e:
print e
finally:
if conn:
conn.close()
def saveBookMark(key, data):
bookMarks = json.loads(data)
data = bookMarks.get('data')
conn = None
count = 0
error = 'null'
try:
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
sql = "DELETE FROM bookmark"
cursor.execute(sql)
for bookMark in data:
id = bookMark.get('id')
pId = bookMark.get('parentId')
iId = bookMark.get('index')
url = bookMark.get('url').encode('utf-8')
title = bookMark.get('title').encode('utf-8').replace("'", " ")
sql = "INSERT INTO bookmark (id,parentId,indexId,url,title) VALUES ('{}','{}','{}','{}','{}')" \
.format(id, pId, iId, url, title)
try:
cursor.execute(sql)
count = count + cursor.rowcount
except Exception as e:
print e
error = e.message
cursor.close()
conn.commit()
except Exception as e:
error = e.message
finally:
if conn:
conn.close()
if count == 0:
return '{"code":%d,"count":%d,"error":"%s"}' % (-1, count, error)
else:
return '{"code":%d,"count":%d,"error":"%s"}' % (0, count, error)
|