Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,5 @@ ENV/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
.idea/ | ||
*.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import sqlite3 | ||
|
||
db_file = "photo.db" | ||
conn = sqlite3.connect(db_file) | ||
cursor = conn.cursor() | ||
|
||
|
||
def create_table(): | ||
drop_table() | ||
cursor.execute('''CREATE TABLE photo ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
hash VARCHAR(50) NOT NULL, | ||
name TEXT NOT NULL, | ||
path TEXT NOT NULL, | ||
existed INTEGER DEFAULT 1)''') | ||
|
||
|
||
def drop_table(): | ||
cursor.execute('DROP TABLE IF EXISTS photo') | ||
|
||
|
||
def insert_file(hash, name, path): | ||
cursor.execute('INSERT INTO photo (hash,name,path) VALUES (?,?,?)', (hash, name, path)) | ||
|
||
|
||
def is_existed(file_path): | ||
cursor.execute('SELECT COUNT(*) FROM photo WHERE path = ?', (file_path,)) | ||
row = cursor.fetchone() | ||
return row[0] != 0 | ||
|
||
|
||
def mark_deleted(file_path): | ||
cursor.execute('UPDATE photo SET existed =0 WHERE NAME = ?', (file_path,)) | ||
conn.commit() | ||
|
||
|
||
def find_duplicate(): | ||
cursor.execute('''SELECT * FROM photo WHERE hash IN | ||
(SELECT hash FROM photo GROUP BY hash HAVING count(*) > 1)''') | ||
rows = cursor.fetchall() | ||
return rows | ||
|
||
|
||
def close_db(): | ||
if conn is not None: | ||
conn.close() | ||
|
||
|
||
if __name__ == '__main__': | ||
print('create photo.db ...') | ||
create_table() | ||
close_db() | ||
print('done!') |