PyMySQL QtDesigner
PyMySQL QtDesigner
Sign up
Sign in
Rizal Saepul M
·
Follow
Published in
Bandung Code
6 min read
Share
Pada pembuatan aplikasi yang terpenting adalah tempat
penyimpanan data yang ada dalam aplikasi tersebut atau yang
sering kita sebut dengan Database. Pada artikel ini saya akan
berbagi pengalaman saya dalam membuat koneksi ke database
dan CRUD di pyhton desktop menggunakan PyQt5. Sebelum
masuk, buatlah database dengan nama dbyhton dan buat tabel
data dengan struktur seperti berikut
Install PyMySQL
import pymysql
import pymysql
Fungsi Koneksi
def koneksi(self):
con = pymysql.connect(db='dbpython', user='root', passwd='',
host='localhost', port=3306, autocommit=True)
cur = con.cursor()
if(cur):
self.messagebox("Koneksi", "Koneksi Berhasil")
else:
self.messagebox("Koneksi", "Koneksi Gagal")
Fungsi Messagebox
def messagebox(self, title, message):
mess = QtWidgets.QMessageBox()
mess.setWindowTitle(title)
mess.setText(message)
mess.setStandardButtons(QtWidgets.QMessageBox.Ok)
mess.exec_()
Fungsi save
def save(self):
firstname = self.FirstName.text()
lastname = self.LastName.text()
address = self.Address.toPlainText()
phone = self.Phone.text()
insert = (firstname, lastname, address, phone)
print(insert)
con = pymysql.connect(db='dbpython', user='root', passwd='',
host='localhost', port=3306, autocommit=True)
cur = con.cursor()
sql = "INSERT INTO data(first_name, last_name, address,
phone)" + \
"VALUES"+str(insert)
data = cur.execute(sql)
if(data):
self.messagebox("SUKSES", "Data Barang Tersimpan")
else:
self.messagebox("GAGAL", "Data Barang Gagal Tersimpan")
run program, isi data kemudian click button save lihat apakah
proses menyimpan data berhasil atau tidak.
Fungsi Tampil
def Tampil(self):
firstname = self.FirstName.text()
db = pymysql.connect(db='dbpython', user='root', passwd='',
host='localhost', port=3306, autocommit=True)
cursor = db.cursor()
cursor.execute("SELECT * FROM data WHERE
first_name='"+str(firstname)+"'")
data = cursor.fetchall()
if (data):
for tp in data:
self.LastName.setText("" + tp[1])
self.Address.setText("" + tp[2])
self.Phone.setText("" + tp[3])
self.messagebox("INFO","Data Ada")
else:
self.messagebox("INFO", "Data belum ada")
Fungsi Update
def update(self):
firstname = self.FirstName.text()
lastname = self.LastName.text()
address = self.Address.toPlainText()
phone = self.Phone.text()
con = pymysql.connect(db='dbpython', user='root', passwd='',
host='localhost', port=3306, autocommit=True)
cur = con.cursor()
sql = "UPDATE data SET last_name=%s, address=%s, phone=%s
WHERE first_name=%s"
data = cur.execute(sql, (lastname, address, phone,
firstname))
if (data):
self.messagebox("SUKSES", "Data Berhasil Di Update")
else:
self.messagebox("GAGAL", "Data Gagal Di Update")
Menghapus Data
Fungsi Hapus
def delete(self):
firstname = self.FirstName.text()
con = pymysql.connect(db='dbpython', user='root', passwd='',
host='localhost', port=3306, autocommit=True)
cur = con.cursor()
sql = "DELETE FROM data where first_name=%s"
data = cur.execute(sql, (firstname))
if (data):
self.messagebox("SUKSES", "Data Berhasil Di HAPUS")
else:
self.messagebox("GAGAL", "Data GAGAL Di HAPUS")
Fungsi clear
def clear(self):
self.FirstName.clear()
self.LastName.clear()
self.Address.clear()
self.Phone.clear()
116
6
Python
Database
Desktop
Sql
Tutorial
116
6
Follow
51 Followers
Follow
Written by Rizal Saepul M
42 Followers
·8 Following
Rizal Saepul M
Author
Indah Yunita
1
1 reply
1
1 reply
See all responses
More from Rizal Saepul M and Bandung Code
In
Bandung Code
by
Rizal Saepul M
Bandung Code
by
Teten Nugraha
Datatable di Reactjs
Hallo sahabat NgulikID selama sore semuanya, oke hari ini saya akan coba
menerangkan bagaimana caranya kita mengimplementasikan jquery…
Bandung Code
by
Teten Nugraha
Belajar Go 1. Pengenalan
Menurut wikipedia, Go Language adalah sebuah sebuah bahasa pemrograman
yang diciptakan Google pada tahun 2009 oleh Robert Griesemer, Rob…
by
Teten Nugraha
Harendra
How I Am Using a Lifetime 100% Free Server
Get a server with 24 GB RAM + 4 CPU + 200 GB Storage + Always Free
Oct 26
In
DataDrivenInvestor
by
Austin Starks
Sep 16
Lists
ChatGPT
21 stories·915 saves
In
The Pythoneers
by
Abhay Parashar
2d ago
Probir Sarkar
Database Performance Benchmark: PostgreSQL 17 vs. MySQL 9
vs. MongoDB 8
Not a member? Read for free
Dec 8
In
BRACKETS
by
Dmitry Bobryshev
Architectures of modern Front-end applications
This post explores the advantages of popular architectures and helps you
choose the best solution to meet your unique requirements.
Nov 5
Jessica Stillman
Jeff Bezos Says the 1-Hour Rule Makes Him Smarter. New
Neuroscience Says He’s Right
Jeff Bezos’s morning routine has long included the one-hour rule. New
neuroscience says yours probably should too.
Oct 30