Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

mysql python

# pip install mysql_connector <-- For Window OS (Comand Prompt/Power Shell)
# conda install mysql_connector <-- For Anaconda Shell
# pip3 install mysql_connector <-- For Mac OS Terminal

import mysql.connector as mc

db = mc.connect(host = "localhost", user = "root", password = "root")
my_cursor = db.cursor()
running = True

while running:
    try:
        my_query = str(input("QUERY: "))
        if my_query == "QUIT":
            running = False
            db.close()
        elif my_query == "SAVE":
            db.commit()
        else:
            my_cursor.execute(my_query)
            for i in my_cursor:
                print(i)
    except mc.Error as error:
        print(error)
    
Comment

python_mysql

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'employeeDB',
        'HOST': 'localhost',
        'USERNAME': 'root',
        'PASSWORD': '',
        'PORT': '3306',
    }
}
Comment

mysql_python

Copied
from __future__ import print_function

import mysql.connector
from mysql.connector import errorcode

DB_NAME = 'employees'

TABLES = {}
TABLES['employees'] = (
    "CREATE TABLE `employees` ("
    "  `emp_no` int(11) NOT NULL AUTO_INCREMENT,"
    "  `birth_date` date NOT NULL,"
    "  `first_name` varchar(14) NOT NULL,"
    "  `last_name` varchar(16) NOT NULL,"
    "  `gender` enum('M','F') NOT NULL,"
    "  `hire_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`)"
    ") ENGINE=InnoDB")

TABLES['departments'] = (
    "CREATE TABLE `departments` ("
    "  `dept_no` char(4) NOT NULL,"
    "  `dept_name` varchar(40) NOT NULL,"
    "  PRIMARY KEY (`dept_no`), UNIQUE KEY `dept_name` (`dept_name`)"
    ") ENGINE=InnoDB")

TABLES['salaries'] = (
    "CREATE TABLE `salaries` ("
    "  `emp_no` int(11) NOT NULL,"
    "  `salary` int(11) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`,`from_date`), KEY `emp_no` (`emp_no`),"
    "  CONSTRAINT `salaries_ibfk_1` FOREIGN KEY (`emp_no`) "
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")

TABLES['dept_emp'] = (
    "CREATE TABLE `dept_emp` ("
    "  `emp_no` int(11) NOT NULL,"
    "  `dept_no` char(4) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`,`dept_no`), KEY `emp_no` (`emp_no`),"
    "  KEY `dept_no` (`dept_no`),"
    "  CONSTRAINT `dept_emp_ibfk_1` FOREIGN KEY (`emp_no`) "
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE,"
    "  CONSTRAINT `dept_emp_ibfk_2` FOREIGN KEY (`dept_no`) "
    "     REFERENCES `departments` (`dept_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")

TABLES['dept_manager'] = (
    "  CREATE TABLE `dept_manager` ("
    "  `emp_no` int(11) NOT NULL,"
    "  `dept_no` char(4) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date NOT NULL,"
    "  PRIMARY KEY (`emp_no`,`dept_no`),"
    "  KEY `emp_no` (`emp_no`),"
    "  KEY `dept_no` (`dept_no`),"
    "  CONSTRAINT `dept_manager_ibfk_1` FOREIGN KEY (`emp_no`) "
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE,"
    "  CONSTRAINT `dept_manager_ibfk_2` FOREIGN KEY (`dept_no`) "
    "     REFERENCES `departments` (`dept_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")

TABLES['titles'] = (
    "CREATE TABLE `titles` ("
    "  `emp_no` int(11) NOT NULL,"
    "  `title` varchar(50) NOT NULL,"
    "  `from_date` date NOT NULL,"
    "  `to_date` date DEFAULT NULL,"
    "  PRIMARY KEY (`emp_no`,`title`,`from_date`), KEY `emp_no` (`emp_no`),"
    "  CONSTRAINT `titles_ibfk_1` FOREIGN KEY (`emp_no`)"
    "     REFERENCES `employees` (`emp_no`) ON DELETE CASCADE"
    ") ENGINE=InnoDB")
Comment

PREVIOUS NEXT
Code Example
Python :: random forest classifier python 
Python :: remove list of value from list python 
Python :: python boolean 
Python :: size of matrix python 
Python :: padding figures in python 
Python :: add element to list python 
Python :: if with && in python 
Python :: select python interpreter vscode 
Python :: TypeError: expected str, bytes or os.PathLike object, not list 
Python :: semaphore in python 
Python :: django add user to group 
Python :: python docstring use 
Python :: python group by 
Python :: python hex 
Python :: pythpn data tyoe 
Python :: how to add items in list in python 
Python :: positive and negative number in python 
Python :: pandas split groupby 
Python :: python replace variable in string 
Python :: python wait 
Python :: how to remove trailing zeros in python 
Python :: python socket get client ip address 
Python :: separate digits with comma 
Python :: ImportError: cannot import name 
Python :: calculate iqr in python dataset example 
Python :: python3 password generator script 
Python :: convert uppercase to lowercase and vice versa in python 
Python :: how to add two strings in python 
Python :: creating dynamic variable in python 
Python :: python schleife 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =