Write a python code
{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Basics (Instructor: Dr. Milad Baghersad)\n", "## Module 5: Using Databases with Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "___\n", "___\n", "___\n", "### Accessing a Database from Python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import mysql\n", "import mysql.connector" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pip install mysql-connector-python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pip install mysql" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import mysql\n", "import mysql.connector" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "database = mysql.connector.connect(host=\"localhost\",user=\"root\",password=\"12345\", database=\"class\")\n", "cursor = database.cursor()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "query = \"select * from class.employee\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cursor.execute(query)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "employee_data = cursor.fetchall()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "employee_data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "employee_table= pd.DataFrame(employee_data)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "employee_table" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "employee_table = pd.DataFrame(employee_data,columns=(\"Emp_ID\",\"Emp_FirstName\", \"Emp_LastName\",\"Dept_Code\",\"Emp_HireDate\",\n", " \"Emp_CreditLimit\",\"Emp_Phone\",\"Emp_MgrID\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "employee_table" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cursor.close()\n", "database.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "___\n", "___\n", "___\n", "___\n", "### Insert values into SQL Server table using Python" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import mysql\n", "import mysql.connector\n", "database = mysql.connector.connect(host=\"localhost\",user=\"root\",password=\"12345\", database=\"class\")\n", "cursor = database.cursor()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#error!\n", "query = \"INSERT INTO class.employee (Emp_ID,Emp_FirstName, Emp_LastName,Dept_Code,Emp_HireDate,Emp_CreditLimit,Emp_Phone,Emp_MgrID) VALUES(11,'Chris',\"Smith\",\"ISM\",\"2018-01-01\",15,4444,22)\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "query = \"INSERT INTO class.employee (Emp_ID,Emp_FirstName, Emp_LastName,Dept_Code,Emp_HireDate,Emp_CreditLimit,Emp_Phone,Emp_MgrID) VALUES (11,'Chris','Smith','ISM','2018-01-01',15,4444,22)\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "query = \"\"\"\n", "INSERT INTO class.employee (Emp_ID,Emp_FirstName, Emp_LastName,Dept_Code,Emp_HireDate,\n", "Emp_CreditLimit,Emp_Phone,Emp_MgrID) VALUES (12,'KKKK','Smith','ISM','2018-01-01',15,4444,22)\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "query" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cursor.execute(query)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "database.commit()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cursor.close()\n", "database.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import mysql\n", "import mysql.connector\n", "import csv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "database = mysql.connector.connect(host=\"localhost\",user=\"root\",password=\"12345\", database=\"class\")\n", "cursor = database.cursor()\n", "query = \"\"\"CREATE TABLE class.grades (ID INT NOT NULL AUTO_INCREMENT,firstname VARCHAR(45) NULL,\n", "lastname VARCHAR(45) NULL,grade INT(11) NULL,PRIMARY KEY (ID));\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cursor.execute(query)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "database.commit()\n", "cursor.close()\n", "database.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "database = mysql.connector.connect(host=\"localhost\",user=\"root\",password=\"12345\", database=\"class\")\n", "cursor = database.cursor()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "updatefile = open(\"Students.csv\",'r')\n", "content=updatefile.readlines()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for line in content:\n", " print(line)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for line in content:\n", " columns = line.split(\",\")\n", " FN = columns[0]\n", " LN = columns[1]\n", " GR = columns[2]\n", " \n", " query= \"INSERT class.grades (firstname, lastname, grade) VALUES (%(1)s, %(2)s, %(3)s)\" %{\"1\": FN, \"2\": LN, \"3\": GR}\n", " print(query)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "database = mysql.connector.connect(host=\"localhost\",user=\"root\",password=\"12345\", database=\"class\")\n", "cursor = database.cursor()\n", "\n", "for line in content:\n", " columns = line.split(\",\")\n", " FN = columns[0]\n", " LN = columns[1]\n", " GR = columns[2]\n", " \n", " query= \"INSERT class.grades (firstname, lastname, grade) VALUES ('%(1)s', '%(2)s', %(3)s)\" %{\"1\": FN, \"2\": LN, \"3\": GR}\n", " #print(query)\n", " cursor.execute(query)\n", " database.commit() \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cursor.close()\n", "database.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }