-
-
Save mushonnip/8115fc92dda46265c21bb0c2ec2e33cc to your computer and use it in GitHub Desktop.
Simple Python CRUD with MySQL
This file contains 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
#!/usr/bin/python | |
import MySQLdb as mdb | |
import sys | |
try: | |
con = mdb.connect('localhost', 'USERNAME MYSQL', 'PASSWORD MYSQL', 'NAMA DATABASE'); | |
cur = con.cursor() | |
cur.execute("SELECT VERSION()") | |
ver = cur.fetchone() | |
print "Database version : %s" % ver | |
except mdb.Error, e: | |
print "Error %d: %s" % (e.args[0],e.args[1]) | |
sys.exit(1) | |
finally: | |
if con: | |
con.close() |
This file contains 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
#!/usr/bin/python | |
import MySQLdb as mdb | |
import sys | |
con = mdb.connect('localhost', 'USERNAME MYSQL', 'PASSWORD MYSQL', 'DATABASE MYSQL'); | |
cur = con.cursor() | |
sql = "DELETE FROM `user` WHERE `id` = '%s'" % ("6") | |
try: | |
cur.execute(sql) | |
con.commit() | |
print "Hapus Data Berhasil" | |
print cur._last_executed | |
except: | |
con.rollback() | |
print "Hapus Data Gagal" | |
print cur._last_executed | |
if con: | |
con.close() |
This file contains 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
#!/usr/bin/python | |
import MySQLdb as mdb | |
import sys | |
con = mdb.connect('localhost', 'USERNAME MYSQL', 'PASSWORD MYSQL', 'DATABASE MYSQL'); | |
cur = con.cursor() | |
try: | |
sql = "SELECT * FROM `user`" | |
cur.execute(sql) | |
user = cur.fetchall() | |
for val in user: | |
print "Nama : %s" %val[1] | |
except: | |
print "Select Data Gagal" | |
if con: | |
con.close() |
This file contains 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
#!/usr/bin/python | |
import MySQLdb as mdb | |
import sys | |
con = mdb.connect('localhost', 'USERNAME MYSQL', 'PASSWORD MYSQL', 'DATABASE MYSQL'); | |
cur = con.cursor() | |
try: | |
sql = "SELECT * from `user` WHERE `id` = '%s'" % ('1') | |
cur.execute(sql) | |
user = cur.fetchone() | |
print "Nama : %s" %user[1] | |
except: | |
print "Select Data Gagal" | |
if con: | |
con.close() |
This file contains 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
#!/usr/bin/python | |
import MySQLdb as mdb | |
import sys | |
con = mdb.connect('localhost', 'USERNAME MYSQL', 'PASSWORD MYSQL', 'DATABASE MYSQL'); | |
cur = con.cursor() | |
sql = "INSERT INTO `user` (`id`,`nama`) VALUE ('%s', '%s')" % (0, "Marjinal") | |
try: | |
cur.execute(sql) | |
con.commit() | |
print "Input Data Berhasil" | |
print cur._last_executed | |
except: | |
con.rollback() | |
print "Input Data Gagal" | |
print cur._last_executed | |
if con: | |
con.close()< |
This file contains 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
#!/usr/bin/python | |
import MySQLdb as mdb | |
import sys | |
con = mdb.connect('localhost', 'USERNAME MYSQL', 'PASSWORD MYSQL', 'DATABASE MYSQL'); | |
cur = con.cursor() | |
sql = "UPDATE user SET nama = '%s' WHERE `id` = '%s'" % ('Supriadi', '2') | |
try: | |
cur.execute(sql) | |
con.commit() | |
print "Update Data Berhasil" | |
print cur._last_executed | |
except: | |
con.rollback() | |
print "Update Data Gagal" | |
print cur._last_executed | |
if con: | |
con.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment