-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_db.py
More file actions
54 lines (39 loc) · 1.2 KB
/
test_db.py
File metadata and controls
54 lines (39 loc) · 1.2 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
import mysql.connector
# First, connect to MySQL without selecting a database
conn = mysql.connector.connect(
host="localhost",
user="root",
password="17999nf$xxjw23#$%&" # Your MySQL root password
)
cursor = conn.cursor()
# Create the database if it doesn't exist
cursor.execute("CREATE DATABASE IF NOT EXISTS testkit")
print("Database created successfully!")
cursor.execute("USE testkit;")
print("Using database 'testkit'.")
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
)
""")
print("Table 'users' is ready.")
sql="INSERT IGNORE INTO users (name,email) VALUES (%s, %s)"
val=("johny", "johnycage@gmail.com")
cursor.execute(sql, val)
conn.commit()
sql="INSERT IGNORE INTO users (name,email) VALUES (%s, %s)"
val=("johny", "johnycage@gmail.com")
cursor.execute(sql, val)
conn.commit()
# Close the initial connection
cursor.execute("SELECT * FROM users;")
users = cursor.fetchall()
# Display table contents
print("\nUsers Table Content:")
print("ID | Name | Email")
print("-" * 30)
for user in users:
print(f"{user[0]} | {user[1]} | {user[2]}")
conn.close()