Repository navigation
Expand file tree
/
Copy pathsql_cmd.py
More file actions
92 lines (77 loc) · 2.57 KB
/
Copy pathsql_cmd.py
File metadata and controls
92 lines (77 loc) · 2.57 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
import mysql.connector
from mysql.connector import Error
def create_connection(host_name, user_name, user_password, db_name):
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password,
database=db_name
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
def connect_V89DB() :
con_sql = ["vtech_cctv", "vtech_cctv", "1^9cm26yEpx", "115.178.63.6", "3306"]
# Replace the values with your actual database credentials
connection = create_connection(con_sql[3], con_sql[0], con_sql[2], con_sql[1])
return connection
def list_tables(connection):
cursor = connection.cursor()
try:
cursor.execute("SHOW TABLES")
tables = cursor.fetchall()
print("Tables in the database:")
for table in tables:
print(table[0])
except Error as e:
print(f"The error '{e}' occurred")
def create_table(connection, create_table_query):
cursor = connection.cursor()
try:
cursor.execute(create_table_query)
connection.commit()
print("Table created successfully")
except Error as e:
print(f"The error '{e}' occurred")
connection.rollback()
def insert_data(connection, query, data):
cursor = connection.cursor()
try:
cursor.execute(query, data)
connection.commit()
print("Data inserted successfully")
except Error as e:
print(f"The error '{e}' occurred")
def select_from_table(connection, select_query):
cursor = connection.cursor()
try:
cursor.execute(select_query)
rows = cursor.fetchall()
for row in rows:
print(row)
except Error as e:
print(f"The error '{e}' occurred")
def update_data(connection, update_query, values):
cursor = connection.cursor()
try:
cursor.execute(update_query, values)
connection.commit()
print(f"{cursor.rowcount} record(s) updated successfully")
except Error as e:
print(f"The error '{e}' occurred")
connection.rollback()
def delete_table(connection, table_name):
drop_query = f"DROP TABLE {table_name}"
cursor = connection.cursor()
try:
cursor.execute(drop_query)
connection.commit()
print(f"Table '{table_name}' deleted successfully.")
except Error as e:
print(f"The error '{e}' occurred")
connection.rollback()
def disconnect(connection) :
connection.close()