-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_class_file.py
135 lines (111 loc) · 4.32 KB
/
database_class_file.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
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
database_file_location = "C:/Program Files/messagetyper/save_msg.db"
folder_path = "C:/Program Files/messagetyper"
import os
import sqlite3
class database_editing_class(sqlite3.Connection):
def __init__(self ):
try:
os.makedirs(folder_path)
except FileExistsError:
...
# Call the constructor of the parent class (sqlite3.Connection)
super().__init__( database_file_location )
self.cursor = self.cursor()
try:
# Create the save_msg_table with id (auto-increment) and msgs (text)
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS save_msg_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
msgs TEXT
)
''')
self.commit()
except:
...
def create_table(self):
# Create the save_msg_table with id (auto-increment) and msgs (text)
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS save_msg_table (
id INTEGER PRIMARY KEY AUTOINCREMENT,
msgs TEXT
)
''')
self.commit()
def add_msg(self, message : str ):
# Add a new message to the msgs column
self.cursor.execute("INSERT INTO save_msg_table (msgs) VALUES (?)", (message,))
self.commit()
def update_msg(self, old_message : str , new_message : str ):
# Update messages without knowing the ID
self.cursor.execute("UPDATE save_msg_table SET msgs = ? WHERE msgs = ?", (new_message, old_message))
self.commit()
def delete_msg(self, old_message : str ):
try:
# Use the DELETE FROM statement to delete rows from the table
self.cursor.execute("DELETE FROM save_msg_table WHERE msgs = ?", (old_message,))
self.commit()
return (f"Deleted message: {old_message}")
except Exception as e:
return (f"Error deleting message: {e}")
def fetch_all_data(self):
# Fetch all data from the table
self.cursor.execute("SELECT * FROM save_msg_table")
return self.cursor.fetchall()
def clear_database(self):
# Delete all data from the database
self.cursor.execute("DELETE FROM save_msg_table")
self.commit()
def close_connection(self):
# Close the database connection
self.close()
# if __name__ == '__main__' :
# # Usage example:
# db = database_editing_class('save_msg.db')
# # db.create_table()
# for i in range(50):
# db.add_msg(i)
# # db.add_msg("Hello, world!")
# # db.update_msg("ALI", "Updated message")
# # data = db.fetch_all_data()
# # data = db.clear_database()
# # print("All data:")
# # for row in data:
# # print(row)
# # # db.clear_database()
# db.close_connection()
# class database_editing_class(sqlite3.Connection):
# def __init__(self, db_file_name):
# # Call the constructor of the parent class (sqlite3.Connection)
# super().__init__(db_file_name)
# self.cursor = self.cursor()
# #
# def add_msg(self , message:str ):
# """This function add new data mean new message in database"""
# self.cursor.execute("INSERT INTO save_msg_table (msgs) VALUES (?)", (message))
# # Commit the changes and close the connection
# self.commit()
# def fetch_all_messages(self):
# # Retrieve all messages from the table
# self.cursor.execute("SELECT msgs FROM save_msg_table")
# return [row[0] for row in self.cursor.fetchall()]
# def close_connection(self):
# # Close the database connection
# self.close()
# import sqlite3
# # Connect to the SQLite database (or create it if it doesn't exist)
# conn = sqlite3.connect('save_msg.db')
# # Create a cursor object to interact with the database
# cursor = conn.cursor()
# message = "d"
# cursor.execute("INSERT INTO save_msg_table (msgs) VALUES (?)", (message))
# # Commit the changes and close the connection
# conn.commit()
# conn.close()
# # Create the save_msg_table with id (auto-increment) and msgs (text)
# cursor.execute('''
# CREATE TABLE IF NOT EXISTS save_msg_table (
# id INTEGER PRIMARY KEY AUTOINCREMENT,
# msgs TEXT
# )
# ''')
# print("Table 'save_msg_table' has been created in 'save_msg.db'")