File size: 9,417 Bytes
e433a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import streamlit as st
import sqlite3
import pandas as pd
def insert_record(table_name):
    """
    Inserts a new record into the specified table in the LGD database.

    Parameters:
    - table_name (str): The name of the table to insert the record into.

    Returns:
    - None
    """
    st.header("Insert Record")
    # Connect to the SQLite database
    conn = sqlite3.connect("lgd_database.db")
    cursor = conn.cursor()

    # Retrieve the first 5 rows from the table
    select_query = f"SELECT * FROM {table_name}"
    cursor.execute(select_query)
    rows = cursor.fetchall()

    # Convert the rows to a DataFrame
    columns = [description[0] for description in cursor.description]
    df = pd.DataFrame(rows, columns=columns)
    df = df.iloc[:, :-1]
    # Display the retrieved rows in a table
    st.subheader("Existing Records")
    st.write(df)

    # Set default values for level_name and level_code based on table_name
    if table_name == "states":
        level_name = "State"
        level_code = 1
    elif table_name == "district":
        level_name = "District"
        level_code = 2
    elif table_name == "sub_district":
        level_name = "Sub-district"
        level_code = 3
    elif table_name == "block":
        level_name = "Block"
        level_code = 4
    elif table_name == "gp":
        level_name = "Gram Panchayats"
        level_code = 5
    else:
        level_name = ""
        level_code = 0

    # Input fields for entityLGDCode and levelCode
    entity_lgd_code = st.number_input("Entity LGD Code", min_value=0, step=1)

    # Input field for entityName
    entity_name = st.text_input("Entity Name")

    # Input field for entityNameVariants
    entity_name_variants = st.text_input("Entity Name Variants")

    # Input field for entityParent
    entity_parent = st.number_input("Entity Parent", min_value=0, step=1)

    if level_name and level_code:
        st.text(f"Level Name: {level_name}")
        st.text(f"Level Code: {level_code}")
    else:
        # Input fields for levelName and levelCode
        level_name = st.text_input("Level Name")
        level_code = st.number_input("Level Code", min_value=0, step=1)

    # Insert button
    if st.button("Insert"):
        # Perform validation checks before inserting the record
        errors = []

        if entity_lgd_code == 0:
            errors.append("Entity LGD Code cannot be zero.")

        if level_code == 0:
            errors.append("Level Code cannot be zero.")

        if not entity_name or not isinstance(entity_name, str):
            errors.append("Entity Name is required and must be a text.")

        if not level_name or not isinstance(level_name, str):
            errors.append("Level Name is required and must be a text.")

        if not entity_name_variants or not isinstance(entity_name_variants, str):
            errors.append("Entity Name Variants is required and must be a text.")

        if not entity_parent or not isinstance(entity_parent, int):
            errors.append("Entity Parent is required and must be an integer.")

        if errors:
            st.error("\n".join(errors))
        else:
            # Connect to the SQLite database
            conn = sqlite3.connect("lgd_database.db")
            cursor = conn.cursor()

            # Check if the entityLGDCode already exists in the table
            select_query = f"SELECT entityLGDCode FROM {table_name} WHERE entityLGDCode = ?"
            cursor.execute(select_query, (entity_lgd_code,))
            existing_code = cursor.fetchone()

            if existing_code:
                st.error("Entity LGD Code already exists in the table.")
            else:
                # Prepare the SQL query for creating the table if it doesn't exist
                create_table_query = f"""
                CREATE TABLE IF NOT EXISTS {table_name} (
                    entityLGDCode INTEGER PRIMARY KEY,
                    entityName TEXT,
                    levelCode INTEGER,
                    levelName TEXT,
                    entityNameVariants TEXT,
                    entityParent INTEGER
                )
                """
                cursor.execute(create_table_query)

                # Prepare the SQL query for inserting the record
                insert_query = f"""
                INSERT INTO {table_name} (entityLGDCode, entityName, levelCode, levelName, entityNameVariants, entityParent)
                VALUES (?, ?, ?, ?, ?, ?)
                """
                values = (entity_lgd_code, entity_name, level_code, level_name, entity_name_variants, entity_parent)

                try:
                    # Execute the SQL query
                    cursor.execute(insert_query, values)
                    conn.commit()
                    st.success("Record inserted successfully!")
                except sqlite3.Error as e:
                    st.error("An error occurred while inserting the record: {}".format(e))
                    conn.rollback()
                finally:
                    # Close the database connection
                    cursor.close()
                    conn.close()

                # Reset the input fields
                entity_lgd_code = 0
                level_code = 0
                entity_name = ""
                level_name = ""
                entity_name_variants = ""
                entity_parent = 0


def update_record(table_name):
    """
    Update a record in a SQLite database table.

    :param table_name: The name of the table to update the record in.
    :type table_name: str

    :return: None
    :rtype: None
    """
    st.header("Update Record")

    # Input field for entityLGDCode
    entity_lgd_code = st.number_input("Entity LGD Code", min_value=0, step=1)

    # Input field for entityName
    entity_name = st.text_input("Entity Name")

    # Input field for entityNameVariants
    entity_name_variants = st.text_input("Entity Name Variants")

    # Input field for entityParent
    entity_parent = st.number_input("Entity Parent", min_value=0, step=1)

    # Update button
    if st.button("Update"):
        # Perform validation checks before updating the record
        errors = []

        if entity_lgd_code == 0:
            errors.append("Entity LGD Code cannot be zero.")

        if not entity_name or not isinstance(entity_name, str):
            errors.append("Entity Name is required and must be a text.")

        if not entity_name_variants or not isinstance(entity_name_variants, str):
            errors.append("Entity Name Variants is required and must be a text.")

        if not entity_parent or not isinstance(entity_parent, int):
            errors.append("Entity Parent is required and must be an integer.")

        if errors:
            st.error("\n".join(errors))
        else:
            # Connect to the SQLite database
            conn = sqlite3.connect("lgd_database.db")
            cursor = conn.cursor()

            # Prepare the SQL query for updating the record
            update_query = f"""
            UPDATE {table_name}
            SET entityName = ?, entityNameVariants = ?, entityParent = ?
            WHERE entityLGDCode = ?
            """
            values = (entity_name, entity_name_variants, entity_parent, entity_lgd_code)

            try:
                # Execute the SQL query
                cursor.execute(update_query, values)
                conn.commit()
                st.success("Record updated successfully!")
            except sqlite3.Error as e:
                st.error("An error occurred while updating the record: {}".format(e))
                conn.rollback()
            finally:
                # Close the database connection
                cursor.close()
                conn.close()

            # Reset the input fields
            entity_lgd_code = 0
            entity_name = ""
            entity_name_variants = ""
            entity_parent = 0

def delete_record(table_name):
    """
    Deletes a record from the specified table in an SQLite database based on the entityLGDCode.
    
    :param table_name: Name of the table to delete the record from.
    :type table_name: str
    
    :return: None
    :rtype: None
    """
    st.header("Delete Record")

    # Input field for entityLGDCode
    entity_lgd_code = st.number_input("Entity LGD Code", min_value=0, step=1)

    # Delete button
    if st.button("Delete"):
        # Connect to the SQLite database
        conn = sqlite3.connect("lgd_database.db")
        cursor = conn.cursor()

        # Prepare the SQL query for deleting the record
        delete_query = f"""
        DELETE FROM {table_name}
        WHERE entityLGDCode = ?
        """
        values = (entity_lgd_code,)

        try:
            # Execute the SQL query
            cursor.execute(delete_query, values)
            conn.commit()
            st.success("Record deleted successfully!")
        except sqlite3.Error as e:
            st.error("An error occurred while deleting the record: {}".format(e))
            conn.rollback()
        finally:
            # Close the database connection
            cursor.close()
            conn.close()

        # Reset the input fields
        entity_lgd_code = 0

""" if __name__ == "__main__":
    table_name = "block"  # Provide the table name here

    # Usage examples
    update_record(table_name)
    delete_record(table) """