利用pycharm在mysql中,创建school数据库,school数据库中再创建student表,teacher表,分别插入三条数据;再创建一个mysql连接,并进行输出查看的代码
时间: 2024-09-11 15:18:52 浏览: 54
首先,确保你已经安装了MySQL服务器,并且PyCharm中配置了Python环境以及相关的数据库驱动(如`mysql-connector-python`或`pymysql`)。
以下是使用Python和PyCharm操作MySQL的步骤:
1. 打开PyCharm,创建一个新的Python项目。
2. 安装MySQL驱动,可以在PyCharm的Terminal中使用pip命令安装,如:`pip install mysql-connector-python`。
3. 创建`school`数据库。
4. 在`school`数据库中创建`student`表和`teacher`表。
5. 向这两个表中各插入三条数据。
6. 使用Python连接到MySQL数据库,执行查询并输出结果。
以下是一个简单的代码示例:
```python
import mysql.connector
from mysql.connector import Error
# 创建数据库连接
def create_server_connection(host_name, user_name, user_password):
connection = None
try:
connection = mysql.connector.connect(
host=host_name,
user=user_name,
passwd=user_password
)
print("MySQL Database connection successful")
except Error as err:
print(f"Error: '{err}'")
return connection
# 创建数据库
def create_database(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
print("Database created successfully")
except Error as err:
print(f"Error: '{err}'")
# 创建表
def create_table(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
print("Table created successfully")
except Error as err:
print(f"Error: '{err}'")
# 插入数据
def insert_data(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
connection.commit()
print("Data inserted successfully")
except Error as err:
print(f"Error: '{err}'")
# 查询数据并打印
def query_data(connection, query):
cursor = connection.cursor()
try:
cursor.execute(query)
records = cursor.fetchall()
for row in records:
print(row)
except Error as err:
print(f"Error: '{err}'")
# 主函数
def main():
host_name = "localhost"
user_name = "root"
user_password = "your_password" # 替换为你的MySQL root用户密码
connection = create_server_connection(host_name, user_name, user_password)
# 创建school数据库
create_database_query = "CREATE DATABASE IF NOT EXISTS school"
create_database(connection, create_database_query)
# 选择school数据库
connection.database = 'school'
# 创建student表
student_table_query = """
CREATE TABLE IF NOT EXISTS student (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
age INT,
class VARCHAR(50)
);
"""
create_table(connection, student_table_query)
# 创建teacher表
teacher_table_query = """
CREATE TABLE IF NOT EXISTS teacher (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
subject VARCHAR(100),
experience INT
);
"""
create_table(connection, teacher_table_query)
# 插入数据到student表
insert_student_query = """
INSERT INTO student (name, age, class) VALUES ('张三', 20, '计算机科学与技术'),
('李四', 19, '信息管理与信息系统'),
('王五', 21, '软件工程');
"""
insert_data(connection, insert_student_query)
# 插入数据到teacher表
insert_teacher_query = """
INSERT INTO teacher (name, subject, experience) VALUES ('赵老师', '数学', 10),
('钱老师', '物理', 8),
('孙老师', '化学', 6);
"""
insert_data(connection, insert_teacher_query)
# 查询并打印student表数据
query_student_table = "SELECT * FROM student"
query_data(connection, query_student_table)
# 查询并打印teacher表数据
query_teacher_table = "SELECT * FROM teacher"
query_data(connection, query_teacher_table)
# 关闭数据库连接
connection.close()
if __name__ == '__main__':
main()
```
请确保替换`your_password`为你自己的MySQL root用户的密码。
阅读全文