Anda di halaman 1dari 6

PERINTAH-PERINTAH DASAR

PENGOLAHAN DATABASE MYSQL DENGAN PYTHON


DATABASE DEFINITION LANGUAGE (DDL)

1. Menghubungkan python dengan Server MySQL


a. Cara langsung

import mysql.connector as koneksi


# -- koneksi
konek = koneksi.connect(host='localhost',user='root',passwd='')
if konek:
print("Koneksi ke Database Berhasil..")
else:
print("Koneksi ke Database Gagal..")

konek.close()

b. Cara input

# -- cara koneksi langsung ke database yang akan di olah


from getpass import getpass
from mysql.connector import connect, Error
try:
with connect(
host="localhost",
user=input("Nama User : "),
password=getpass("Password DB : "),
) as konek:
#print(konek)
print('Koneksi ke Database MySQL Sukses!!!')
konek.close()
except Error as e:
print(e)

2. Menampilkan seluruh database yang ada pada MySQL Server


a. Cara langsung

import mysql.connector as koneksi


try:
konek = koneksi.connect(host='localhost',user='root',passwd='')
strSQL = "SHOW DATABASES"
kursor = konek.cursor()
kursor.execute(strSQL)
for data in kursor:
print(data)
konek.close()
except Error as e:
print(e)
b. Cara input

from getpass import getpass


from mysql.connector import connect, Error
try:
with connect(
host="localhost",
user=input("Enter username: "),
password=getpass("Enter password: "),
) as konek:
print('Koneksi ke Database MySQL Sukses!!!')
#-- perintah membuat database
strSQL = "SHOW DATABASES"
with konek.cursor() as kursor:
kursor.execute(strSQL)
for data in kursor:
print(data)
konek.close()
except Error as e:
print(e)
# Keterangan :
# Perintah :
# with konek.cursor() as kursor:
# kursor.execute(strSQL)

# sama dengan
# kursor = konek.cursor()
# kursor.execute(strSQL)

3. Menciptakan database baru pada server MySQL


a. Cara langsung

import mysql.connector as koneksi


# -- koneksi
konek = koneksi.connect(host='localhost',user='root',passwd='')
if konek:
print("Koneksi ke Database Berhasil..")
else:
print("Koneksi ke Database Gagal..")

strSQL = "CREATE DATABASE DBTESTING"


kursor = konek.cursor()
kursor.execute(strSQL)
print('DATABASE DBTESTING BERHASIL DIBUAT')
konek.close()

b. Cara input

from getpass import getpass


from mysql.connector import connect, Error
try:
with connect(
host="localhost",
user=input("Nama User (root) : "),
password=getpass("Password (boleh kosong) : "),
) as konek:
print('Koneksi ke Database MySQL Sukses!!!')
#-- perintah membuat database
strSQL = "CREATE DATABASE DB5SIR"
with konek.cursor() as kursor:
kursor.execute(strSQL)
konek.close()

except Error as e:
print(e)

4. Menghubungkan python dengan Server MySQL langsung Database yang dituju


a. Cara langsung

import mysql.connector as bukaDB


# -- koneksi
konek = bukaDB.connect(host='localhost',user='root',passwd='',
database='db5sir')
if konek:
print("Koneksi ke Database Berhasil..")
else:
print("Koneksi ke Database Gagal..")

konek.close()

b. Cara input

# -----------------------------------------------------------
# -- cara koneksi langsung ke database yang akan di olah
# -- Created by : Dilson, M.Kom
# -- Objek Oriented Programming
# -- Semester Ganjil TA 2021/2022

from getpass import getpass


from mysql.connector import connect, Error
try:
with connect(
host="localhost",
user=input("Nama User (root) : "),
password=getpass("Password (Boleh Kosong) : "),
database="db5sir",
) as konek:
print('Koneksi ke Database MySQL Sukses!!!')
konek.close()
except Error as e:
print(e)
5. Menciptakan Tabel pada database 5SIB
Studi Kasus : Gaji
Tabel yang akan dirancang :
a. Tbkaryawan
No Field Name Type Width Adjustment
1 Id Int 10 Auto Increment
2 NIK Varchar 10 Nomor Induk Karyawan
3 Nmkry Varchar 40 Nama Karyawan
4 Templ Varchar 30 Tempat Lahir
5 Tgll Date Tanggal Lahir
6 Jenkel Char 1 Jenis Kelamin (L/P)
7 Nohp Varchar 12 Nomor HP
8 Email Varchar 100 Email Karyawan
9 Statkwn char 2 Status Kawin [ K/TK/D/J]
10 Masakerja Int 2 Masa Kerja
11 Gapok Numeric 10 Gaji Pokok
SQL
CREATE TABLE TbKaryawan (id int not null auto_increment
primary key, NIK Varchar(10),nmkry Varchar(40),Templ
Varchar(30),Tgll Date, Jenkel Char(1), Nohp Varchar(12),
Email Varchar(100), Statkwn char(2), Masakerja Int not
null, Gapok Numeric not null);

b. Tbtunjangan
No Field Name Type Width Adjustment
1 Idtunj Int 10 ID Tunjangan
2 Nmtunj Varchar 30 Nama Tunjangan
3 Jmltunj Numeric 10 Jumlah Tunjangan
SQL
create table tbtunjangan (Idtunj Int not null primary
key, Nmtunj Varchar(30), Jmltunj Numeric not null)

c. Tbgaji
No Field Name Type Width Adjustment
1 Idgaji Int 10 ID Gaji
2 Tglgaji Date 30 Nama Tunjangan
3 Id Int 10 ID Karyawan
4 Idtunj Int 10 ID Tunjangan
5 Pot1 Numeric Potongan Kehadiran
6 Pot2 Numeric Potongan Pinjaman
7 Pot3 Numeric Potongan PPh Pasal 21 (10%)
SQL
create table tbGaji (Idgaji Int not null primary key,
Tglgaji Date, Id Int not null,Pot1 Numeric not null,
Pot2 Numeric not null,Pot3 Numeric not null)
a. TbgajiDetail
No Field Name Type Width Adjustment
1 Idgaji Int 10 ID Gaji
2 Idtunj Int 10 ID Tunjangan
create table tbGaji (Idgaji Int not null, Idtunj Int not
null)

Script Python

import mysql.connector as bukaDB


# -- koneksi
konek = bukaDB.connect(host='localhost',user='root',passwd='',
database='db5sib')
if konek:
print("Koneksi ke Database Berhasil..")
else:
print("Koneksi ke Database Gagal..")

# TbKaryawan
strSQL = """
CREATE TABLE TbKaryawan (id int not null auto_increment primary key,
NIK Varchar(10),nmkry Varchar(40),Templ Varchar(30),Tgll Date,
Jenkel Char(1), Nohp Varchar(12),Email Varchar(100), Statkwn
char(2),
Masakerja Int not null, Gapok Numeric not null);
"""

# Tbtunjangan
strSQL2="""
create table tbtunjangan (Idtunj Int not null primary key, Nmtunj
Varchar(30),
Jmltunj Numeric not null)
"""

# TbGaji
strSQL03 ="""
create table tbGaji (Idgaji Int not null primary key,
Tglgaji Date, Id Int not null,Pot1 Numeric not null,
Pot2 Numeric not null,Pot3 Numeric not null)
"""
# TbGajiDetail
strSQL04 ="""
create table tbGajiDetail (Idgaji Int not null, Idtunj Int not null)
"""

kursor = konek.cursor()
# kursor.execute(strSQL)
# kursor.execute(strSQL2)
# kursor.execute(strSQL03)
kursor.execute(strSQL04)
konek.commit()

Anda mungkin juga menyukai