Skip to content

Commit deb8b71

Browse files
committed
Created using Colaboratory
1 parent 2a20e9e commit deb8b71

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

Python_MySQL_P1.ipynb

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": [],
7+
"authorship_tag": "ABX9TyOR0tI3L4pBZ+841BZ1AGyL",
8+
"include_colab_link": true
9+
},
10+
"kernelspec": {
11+
"name": "python3",
12+
"display_name": "Python 3"
13+
},
14+
"language_info": {
15+
"name": "python"
16+
}
17+
},
18+
"cells": [
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {
22+
"id": "view-in-github",
23+
"colab_type": "text"
24+
},
25+
"source": [
26+
"<a href=\"https://colab.research.google.com/github/Animeshcoder/MySQL-Python/blob/main/Python_MySQL_P1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
27+
]
28+
},
29+
{
30+
"cell_type": "markdown",
31+
"source": [
32+
"### **Introduction**\n",
33+
"\n",
34+
"This project is a Python script that uses PyMySQL to connect to a MySQL server, create a new database named medicine, and create a new table named medicines within that database. The medicines table has several columns, including Name, Description, Expiry_date, Number_of_tablets, and Price_per_tablet, which can be used to store information about different medicines. This script demonstrates how to use PyMySQL to interact with a MySQL server and execute SQL statements to create a new database and table."
35+
],
36+
"metadata": {
37+
"id": "hio0hIqGIsbE"
38+
}
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"source": [
43+
"### **Steps Involved**\n",
44+
"\n",
45+
"**Connect to MySQL server**: The code starts by importing the pymysql module and using its connect function to create a connection to a MySQL server running on the local machine (localhost). The connect function takes several arguments, including the host, user, and password of the MySQL server. The resulting connection object represents the connection to the MySQL server.\n",
46+
"\n",
47+
"**Create cursor:** Next, the code creates a cursor object by calling the cursor method of the connection object. A cursor is used to execute SQL statements on the MySQL server.\n",
48+
"\n",
49+
"**Create database:** The code then uses the cursor to execute an SQL statement that creates a new database named medicine on the MySQL server. This is done by calling the execute method of the cursor object with an SQL CREATE DATABASE statement as its argument.\n",
50+
"\n",
51+
"**Select database:** After creating the new database, the code uses the cursor to execute another SQL statement that selects the medicine database as the default database for subsequent SQL statements. This is done by calling the execute method of the cursor object with an SQL USE statement as its argument.\n",
52+
"\n",
53+
"**Create table:** The code then uses the cursor to execute an SQL statement that creates a new table named medicines within the selected medicine database. This table has several columns, including Name, Description, Expiry_date, Number_of_tablets, and Price_per_tablet. This is done by calling the execute method of the cursor object with an SQL CREATE TABLE statement as its argument.\n",
54+
"\n",
55+
"**Close cursor and connection:** Finally, after creating the new table, the code closes the cursor and connection objects by calling their respective close methods. This releases any resources held by these objects and terminates the connection to the MySQL server."
56+
],
57+
"metadata": {
58+
"id": "QGtZruZmJTm8"
59+
}
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": null,
64+
"metadata": {
65+
"id": "DwfzHLVOIDGi"
66+
},
67+
"outputs": [],
68+
"source": [
69+
"import pymysql\n",
70+
"\n",
71+
"# create a connection to the MySQL server\n",
72+
"connection = pymysql.connect(host=\"yourhost\", user=\"youruser\", password=\"yourpassword\")\n",
73+
"\n",
74+
"# create a cursor object to execute SQL statements\n",
75+
"cursor = connection.cursor()\n",
76+
"\n",
77+
"# create a new database\n",
78+
"cursor.execute(\"CREATE DATABASE medicine\")\n",
79+
"\n",
80+
"# select the new database\n",
81+
"cursor.execute(\"USE medicine\")\n",
82+
"\n",
83+
"# create a new table with columns\n",
84+
"cursor.execute(\"\"\"\n",
85+
" CREATE TABLE medicines(\n",
86+
" Name VARCHAR(2000),\n",
87+
" Description VARCHAR(2550),\n",
88+
" Expiry_date VARCHAR(255),\n",
89+
" Number_of_tablets VARCHAR(25),\n",
90+
" Price_per_tablet VARCHAR(255)\n",
91+
" )\n",
92+
"\"\"\")\n",
93+
"\n",
94+
"# close the cursor and connection\n",
95+
"cursor.close()\n",
96+
"connection.close()\n"
97+
]
98+
}
99+
]
100+
}

0 commit comments

Comments
 (0)