-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.py
More file actions
165 lines (133 loc) · 4.36 KB
/
sql.py
File metadata and controls
165 lines (133 loc) · 4.36 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sqlite3
import os
# Connect to SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('image_tags.db')
c = conn.cursor()
def preflight():
"""
Making sure tables exist.
"""
# Create Images table
c.execute('''
CREATE TABLE IF NOT EXISTS Images (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
path TEXT NOT NULL
)
''')
# Create Tags table
c.execute('''
CREATE TABLE IF NOT EXISTS Tags (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tag TEXT UNIQUE NOT NULL
)
''')
# Create Image_Tag table (to associate images with tags)
c.execute('''
CREATE TABLE IF NOT EXISTS Image_Tag (
image_id INTEGER,
tag_id INTEGER,
FOREIGN KEY (image_id) REFERENCES Images(id),
FOREIGN KEY (tag_id) REFERENCES Tags(id),
PRIMARY KEY (image_id, tag_id)
)
''')
# Commit changes and close connection
conn.commit()
conn.close()
preflight()
def add_image(name, path):
conn = sqlite3.connect('image_tags.db')
c = conn.cursor()
# Insert the image (if it doesn't already exist)
c.execute('''
INSERT OR IGNORE INTO Images (name, path)
VALUES (?, ?)
''', (name, path))
# Get the image ID (since INSERT OR IGNORE won't give it directly)
c.execute('SELECT id FROM Images WHERE name = ? AND path = ?', (name, path))
image_id = c.fetchone()[0]
conn.commit()
conn.close()
return image_id # Return the image ID for future linking
def add_tag(tag):
conn = sqlite3.connect('image_tags.db')
c = conn.cursor()
# Insert the tag (if it doesn't already exist)
c.execute('''
INSERT OR IGNORE INTO Tags (tag)
VALUES (?)
''', (tag,))
# Get the tag ID
c.execute('SELECT id FROM Tags WHERE tag = ?', (tag,))
tag_id = c.fetchone()[0]
conn.commit()
conn.close()
return tag_id # Return the tag ID for future linking
def link_image_tag(image_id, tag_id):
conn = sqlite3.connect('image_tags.db')
c = conn.cursor()
# Insert the link between image and tag
c.execute('''
INSERT OR IGNORE INTO Image_Tag (image_id, tag_id)
VALUES (?, ?)
''', (image_id, tag_id))
conn.commit()
conn.close()
def add_image(name, path):
conn = sqlite3.connect('image_tags.db')
c = conn.cursor()
# Insert the image (if it doesn't already exist)
c.execute('''
INSERT OR IGNORE INTO Images (name, path)
VALUES (?, ?)
''', (name, path))
# Get the image ID (since INSERT OR IGNORE won't give it directly)
c.execute('SELECT id FROM Images WHERE name = ? AND path = ?', (name, path))
image_id = c.fetchone()[0]
conn.commit()
conn.close()
return image_id # Return the image ID for future linking
def add_tag(tag):
conn = sqlite3.connect('image_tags.db')
c = conn.cursor()
# Insert the tag (if it doesn't already exist)
c.execute('''
INSERT OR IGNORE INTO Tags (tag)
VALUES (?)
''', (tag,))
# Get the tag ID
c.execute('SELECT id FROM Tags WHERE tag = ?', (tag,))
tag_id = c.fetchone()[0]
conn.commit()
conn.close()
return tag_id # Return the tag ID for future linking
# Get images from a tag
def get_images_for_tag(tag):
try:
with sqlite3.connect('image_tags.db') as conn:
c = conn.cursor()
c.execute('''
SELECT i.name, i.path
FROM Images i
JOIN Image_Tag it ON i.id = it.image_id
JOIN Tags t ON it.tag_id = t.id
WHERE t.tag = ?
''', (tag,))
images = c.fetchall()
return images
except sqlite3.Error as e:
print(f"An error occurred: {e}")
return []
def add_tags_to_image(image_name, image_path, tags):
# Step 1: Add the image and get its ID
image_id = add_image(image_name, image_path)
# Step 2: Add each tag and link it to the image
for tag in tags:
tag_id = add_tag(tag) # Add the tag to the database (if not already present)
link_image_tag(image_id, tag_id) # Link the image and the tag
# # Example Usage
# image_name = "a (1).jpg"
# image_path = f"{os.getcwd()}\\{image_name}"
# tags = ["sunset", "beach", "vacation"]
# add_tags_to_image(image_name, image_path, tags)