Python Menggunakan Database SQLite3 (CRUD)
Python Menggunakan Database SQLite3 - Dalam post saya kali ini akan membahas sqlite3 pada python, sebelum membahas tentang cara menggunakan sqlite3 pada python, pertama kita harus mengetahui apa itu sqlite, ialah sebuah database engine yang tidak terikat dengan server yang bersifat standalone artinya berdiri sendiri tanpa ada pengaturan seperti database kebanyakan tapi memiliki fungsi yang sama dengan database pada umumnya, perintah dasar seperti sql biasa masih dapat dilakukan di sqlite. Tipe data sqlite ada empat yang mewakili tipe database pada umumnya yaitu :
SQLite | SQL Biasa | |
---|---|---|
Integer | : | Int, Integer, TinyInt, SmallInt, MediumInt, BeginT, Unsign Big Int, Int2, Int8 |
Text | : | Character, Varchar, Varying Character, Native Character, NVarChar, Text, Clob |
Real | : | Real, Double, Double Precision, Float |
Numeric | : | Numeric, Decimal, Boolean, Date, DateTime |
Balik ke SQLite3, pada python sqlite3 adalah salah satu library yang mendukung sistem database sqlite di python. Berikut perintah dasar pada sqlite3
CREATE DATABASE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
# mengkoneksikan database, sekaligus membuat database jika file database tidak ada | |
conn = sqlite3.connect('data.db') | |
# supaya tidak ada lagi akses dari database sehingga tidak proses jalan terus menurus | |
conn.close() |
CREATE TABLE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
conn = sqlite3.connect('data.db') | |
c = conn.cursor() | |
#execute fungsinya mengesukisi perintah sql | |
c.execute('CREATE TABLE IF NOT EXISTS \ | |
karyawan(nik REAL, nama TEXT)') | |
#create table if not exits artinya jika table sudah ada | |
#tidak perlu di buat lagi | |
conn.close() | |
c.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
conn = sqlite3.connect('data.db') | |
c = conn.cursor() | |
#perintah sql untuk insert data | |
c.execute("INSERT INTO karyawan VALUES(14538823,'Aldhi Dinis')") | |
conn.commit() | |
c.close() | |
conn.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
conn = sqlite3.connect('data.db') | |
c = conn.cursor() | |
#perintah sql menampilkan data pada table | |
c.execute('SELECT * FROM karyawan') | |
#perulangan untuk membuat data tampil semua | |
for row in c.fetchall(): | |
print(row) | |
c.close() | |
conn.close() | |
UPDATE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
conn = sqlite3.connect('data.db') | |
c = conn.cursor() | |
#perintah sql untuk mengubah data | |
c.execute('UPDATE karyawan SET nama = 'Iwan Sutandi' WHERE nik = 14538823') | |
conn.commit() | |
c.close() | |
conn.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
conn = sqlite3.connect('data.db') | |
c = conn.cursor() | |
#perintah sql untuk menghapus data | |
c.execute('DELETE FROM karyawan WHERE nik = 14538823') | |
conn.commit() | |
c.close() | |
conn.close() |
Sekian dari Python Menggunakan Database SQLite3 (CRUD), dan......
Selamat Mencoba :)