用pycharm和mysql建一个查询两种药物信息相互作用的网站

时间: 2024-05-03 20:17:11 浏览: 10
首先,你需要安装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/,输入两种药物的名称并点击提交按钮,即可查看它们的信息和相互作用信息。

相关推荐

最新推荐

recommend-type

图文详解Django使用Pycharm连接MySQL数据库

主要介绍了Django使用Pycharm连接MySQL数据库的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

pycharm中连接mysql数据库的步骤详解

在进行Python研发的时候,pycharm是一个很好的IDE,下面这篇文章主要给大家介绍了pycharm中连接mysql数据库的步骤,文中通过图文介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
recommend-type

PyCharm MySQL可视化Database配置过程图解

主要介绍了PyCharm MySQL可视化Database配置过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

详解pycharm连接不上mysql数据库的解决办法

主要介绍了详解pycharm连接不上mysql数据库的解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

pycharm+django创建一个搜索网页实例代码

主要介绍了pycharm+django创建一个搜索网页实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。