· 8 years ago · Feb 19, 2018, 08:38 PM
1import pymysql
2
3# Connect to mysql user
4db = pymysql.connect("localhost","danu","nightcore","PyMySQL")
5
6# Cursor Function untuk men-select pymysql / cursor object
7cursor=db.cursor()
8
9# Command 'execute' untuk mengeksekusi perintah dari MYSQL
10cursor.execute('DROP TABLE IF EXISTS Karyawan')
11database = cursor.execute("SHOW DATABASES")
12
13
14
15# Command For Create Tables
16sql = """Create Table Karyawan (
17 Nama_Depan Char (10) NOT NULL,
18 Nama_Belakang Char (10) NOT NULL,
19 Umur INT NOT NULL,
20 Kelamin Char (1) NOT NULL,
21 SKS Float NOT NULL
22 ) """
23
24# Command 'execute' untuk mengeksekusi perintah dari MYSQL (2)
25cursor.execute(sql)
26
27
28# Making Output
29print ('Database : {}'.format(database))
30print ('If was any Value in tables, there will be deleted')
31
32# Disconect
33db.close()