-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_handler.py
50 lines (37 loc) · 1.27 KB
/
sqlite_handler.py
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sqlite3
from utils import read_sql, DB_DIR
__author__ = "bill.li"
DB_NAME = os.path.join(DB_DIR, "tweet_info.db")
def init_table(num=3):
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.cursor()
i = 0
while i < num:
try:
cursor.execute(read_sql("create_tweet_table.sql"))
break
except sqlite3.OperationalError:
cursor.execute(read_sql("delete_tweet_table.sql"))
continue
def insert(id, title, trans_title, time_str):
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.cursor()
data = [id, title, trans_title, time_str]
cursor.executemany(read_sql("insert_tweet_table.sql"), [data])
conn.commit()
def search(id):
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.cursor()
targets = cursor.execute(read_sql("search_tweet_table.sql").format(id)).fetchall()
return len(targets)
def search_all():
with sqlite3.connect(DB_NAME) as conn:
cursor = conn.cursor()
targets = cursor.execute(read_sql("search_all_tweet_table.sql")).fetchall()
return len(targets)
if __name__ == '__main__':
pass
search_all()