· 6 years ago · Aug 20, 2019, 11:10 PM
1# -*- coding: utf-8 -*-
2
3import sqlite3
4import os
5from datetime import datetime
6
7# Connect to database
8conn = sqlite3.connect(
9 "test.sqlite3"
10)
11
12# Create table
13with conn:
14 conn.execute(
15 """CREATE TABLE IF NOT EXISTS Files (
16 ID INTEGER PRIMARY KEY AUTOINCREMENT,
17 state BOOLEAN DEFAULT 0,
18 Filedata BLOB NOT NULL
19 );
20 """
21 )
22
23print("{}: Adding 60MB file to database".format(datetime.now()))
24try:
25 with open("60MB.pdf", 'rb') as fIn:
26 _blob = fIn.read()
27 with conn:
28 conn.execute("INSERT INTO Files VALUES (?, ?, ?);", (None, False, sqlite3.Binary(_blob)))
29 print("{}: Finished".format(datetime.now()))
30except Exception as e:
31 print("Error: {}".format(e))
32
33
34print("{}: Adding 130k file to database".format(datetime.now()))
35try:
36 with open("130k.pdf", 'rb') as fIn:
37 _blob = fIn.read()
38 with conn:
39 conn.execute("INSERT INTO Files VALUES (?, ?, ?);", (None, False, sqlite3.Binary(_blob)))
40 print("{}: Finished".format(datetime.now()))
41except Exception as e:
42 print("Error: {}".format(e))
43
44print("{}: Updating the state column of 60MB file".format(datetime.now()))
45try:
46 with conn:
47 conn.execute("UPDATE Files SET state = 1 WHERE ID = 1;")
48 #conn.execute("UPDATE Files SET state = 0;")
49 print("{}: Finished".format(datetime.now()))
50except Exception as e:
51 print("Error: {}".format(e))
52
53print("{}: Same with 130k file".format(datetime.now()))
54try:
55 with conn:
56 conn.execute("UPDATE Files SET state = 1 WHERE ID = 2;")
57 #conn.execute("UPDATE Files SET state = 0;")
58 print("{}: Finished".format(datetime.now()))
59except Exception as e:
60 print("Error: {}".format(e))
61
62conn.close()
63#os.remove("test.sqlite3")