Search
 
SCRIPT & CODE EXAMPLE
 

SQL

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
Sql :: stop and start mysql 
Sql :: sql server: select column values as comma separated string 
Sql :: how to change the value of a table in sql 
Sql :: how to use like in sql 
Sql :: sql update all rows 
Sql :: oracle index partition 
Sql :: SQL SERVER SELECT BETWEEN DATETIME 
Sql :: sql select between two dates 
Sql :: pgAdmin - Please correct the Binary Path 
Sql :: FirebaseException ([cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.) 
Sql :: purge undo tablespace usage 
Sql :: show column from sql server 
Sql :: t sql get foreign key 
Sql :: add column not null with default value postgres 
Sql :: postgresql drop table 
Sql :: mysql list users 
Sql :: mysql sort descending 
Sql :: create new table from existing table with data in sql server 
Sql :: mysql columns values as comma separated string 
Sql :: psql change table schema 
Sql :: convert date to dd/mm/yyyy sql 
Sql :: how to select first row of database sql 
Sql :: grant all privileges on a db 
Sql :: mysql concatenate null 
Sql :: SQL COUNT() with DISTINCT 
Sql :: how to change column name in sql 
Sql :: how to truncate table with foreign key constraint postgresql 
Sql :: define a variable in mysql from select 
Sql :: install sqlite npm 
Sql :: ms sql print from new line 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =