用pycharm和mysql建一个查询两种药物信息相互作用的网站
时间: 2024-05-03 19:17:11 浏览: 105
首先,你需要安装MySQL和PyCharm,然后按照以下步骤操作:
1. 创建数据库和表格
打开MySQL,创建一个新的数据库,例如“drug_interactions”,然后创建一个表格来存储药物信息和相互作用信息。
```sql
CREATE DATABASE drug_interactions;
USE drug_interactions;
CREATE TABLE drugs (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description TEXT,
PRIMARY KEY (id)
);
CREATE TABLE interactions (
id INT NOT NULL AUTO_INCREMENT,
drug1_id INT NOT NULL,
drug2_id INT NOT NULL,
description TEXT,
PRIMARY KEY (id),
FOREIGN KEY (drug1_id) REFERENCES drugs(id),
FOREIGN KEY (drug2_id) REFERENCES drugs(id)
);
```
2. 添加数据
使用以下代码将一些药物和相互作用数据添加到数据库中。
```python
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="drug_interactions"
)
# 插入药物信息
mycursor = mydb.cursor()
sql = "INSERT INTO drugs (name, description) VALUES (%s, %s)"
val = ("Aspirin", "Pain reliever and anti-inflammatory drug")
mycursor.execute(sql, val)
val = ("Ibuprofen", "Pain reliever and anti-inflammatory drug")
mycursor.execute(sql, val)
mydb.commit()
# 插入相互作用信息
sql = "INSERT INTO interactions (drug1_id, drug2_id, description) VALUES (%s, %s, %s)"
val = (1, 2, "Taking aspirin and ibuprofen together may increase the risk of stomach bleeding.")
mycursor.execute(sql, val)
mydb.commit()
```
3. 创建网站
在PyCharm中创建一个新的Flask项目,然后创建一个包含两个输入框和一个提交按钮的HTML表单,以便用户可以输入两种药物的名称。当用户提交表单时,使用以下代码查询数据库以获取药物和相互作用信息,并在网站上显示它们。
```python
from flask import Flask, render_template, request
import mysql.connector
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/interactions', methods=['POST'])
def interactions():
drug1 = request.form['drug1']
drug2 = request.form['drug2']
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="drug_interactions"
)
# 查询药物信息
mycursor = mydb.cursor()
sql = "SELECT * FROM drugs WHERE name = %s"
val = (drug1,)
mycursor.execute(sql, val)
drug1_info = mycursor.fetchone()
val = (drug2,)
mycursor.execute(sql, val)
drug2_info = mycursor.fetchone()
# 查询相互作用信息
sql = "SELECT * FROM interactions WHERE drug1_id = %s AND drug2_id = %s"
val = (drug1_info[0], drug2_info[0])
mycursor.execute(sql, val)
interaction_info = mycursor.fetchone()
return render_template('interactions.html', drug1_info=drug1_info, drug2_info=drug2_info, interaction_info=interaction_info)
if __name__ == '__main__':
app.run()
```
4. 创建HTML模板
创建一个包含表单和查询结果的HTML模板,例如“index.html”和“interactions.html”。
index.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Drug Interactions</title>
</head>
<body>
<form action="/interactions" method="post">
<label for="drug1">Drug 1:</label>
<input type="text" name="drug1" id="drug1">
<label for="drug2">Drug 2:</label>
<input type="text" name="drug2" id="drug2">
<input type="submit" value="Submit">
</form>
</body>
</html>
```
interactions.html:
```html
<!DOCTYPE html>
<html>
<head>
<title>Drug Interactions</title>
</head>
<body>
<h1>Drug Interactions</h1>
<h2>{{ drug1_info[1] }}:</h2>
<p>{{ drug1_info[2] }}</p>
<h2>{{ drug2_info[1] }}:</h2>
<p>{{ drug2_info[2] }}</p>
{% if interaction_info %}
<h2>Interaction:</h2>
<p>{{ interaction_info[3] }}</p>
{% else %}
<p>No interaction found.</p>
{% endif %}
</body>
</html>
```
5. 运行网站
在PyCharm中运行项目,并使用浏览器访问http://localhost:5000/,输入两种药物的名称并点击提交按钮,即可查看它们的信息和相互作用信息。
阅读全文