Pertemuan XV Database Python
Pertemuan XV Database Python
Pertemuan XV
Materi
• Install PyMySql
• Database Connection
• Create Table
• Insert
huripan Kediri
2
Akses Database Python
• Standar Interface Python untuk database adalah Python DB-API. Kebanyakan
Interface database Python mematuhi standar ini.
• Database yang didukung API Database Python :
• GadFly
• mSQL
• MySQL
• PostgreSQL
huripan Kediri
• Sybase
• SQLite
• Mulai Python 3 Modul MySQLdb tidak kompatibel dengan. Sebagai gantinya, kita
akan menggunakan modul PyMySQL.
3
Apa itu PyMySQL ?
• PyMySQL adalah sebuah antarmuka untuk menghubungkan ke server
database MySQL dari Python. Ini mengimplementasikan API Database
Python v2.0 dan berisi perpustakaan klien MySQL murni-Python. Tujuan
PyMySQL adalah penggantian drop-in untuk MySQLdb. Anda dapat melihat
dokumentasi lengkap penggunaan PyMySQL di
https://pymysql.readthedocs.io/en/latest/.
huripan Kediri
https://www.youtube.com/watch?v=n_8aA4r6V-o
5
Database Connection
• Sebelum terhubung ke database MySQL, pastikan beberapa hal dibawah ini :
• Anda telah membuat database TESTDB.
• Anda telah membuat tabel EMPLOYEE di TESTDB.
• Tabel ini memiliki bidang FIRST_NAME, LAST_NAME, AGE, SEX, dan INCOME.
• User ID “testuser” dan password “test123” diatur untuk mengakses TESTDB.
• Python modul PyMySQL terinstal dengan benar pada mesin Anda.
• Anda telah melalui tutorial MySQL untuk memahami Dasar-Dasar MySQL
• Berikut ini adalah contoh koneksi dengan database MySQL “TESTDB”
huripan Kediri
pymysql.connect("localhost","testuser","test method.
123","TESTDB" ) data = cursor.fetchone()
# prepare a cursor object using cursor() print ("Database version : %s " % data) 6
method
cursor = db.cursor() # disconnect from server
db.close()
Membuat Tabel Database
import pymysql.cursors
huripan Kediri
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""
7
cursor.execute(sql)
huripan Kediri
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error 8
db.rollback()
huripan Kediri
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error 9
db.rollback()
huripan Kediri
• Rowcount - Ini adalah atribut read-only dan mengembalikan jumlah baris
yang dipengaruhi oleh metode execute ().
• Prosedur berikut menanyakan semua catatan dari tabel EMPLOYEE yang
memiliki gaji lebih dari 1000 10
import pymysql.cursors
huripan Kediri
sex = row[3]
income = row[4]
# Now print fetched result
print ("fname = %s,lname = %s,age = %d,sex = %s,income = %d" % \
(fname, lname, age, sex, income ))
except: 11
print ("Error: unable to fetch data")
import pymysql.cursors
huripan Kediri
# Execute the SQL command
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error 12
db.rollback()
huripan Kediri
cursor.execute(sql)
# Commit your changes in the database
db.commit()
except:
# Rollback in case there is any error 13
db.rollback()
huripan Kediri
14
Terimakasih
huripan Kediri