Archive for Maret 2024
Phyton My SQL
Phyton My SQL
Python can be used in database applications.
One of the most popular databases is MySQL.
MySQL Database
To be able to experiment with the code examples in this tutorial, you should have MySQL installed on your computer.
You can download a MySQL database at https://www.mysql.com/downloads/.
Install MySQL Driver
Python needs a MySQL driver to access the MySQL database.
Download and install "MySQL Connector":
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python -m pip install mysql-connector-python
Now you have downloaded and installed a MySQL driver.
Create Connection
Start by creating a connection to the database.
Use the username and password from your MySQL database:
demo_mysql_connection.py:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
print(mydb)
Creating a Database
To create a database in MySQL, use the "CREATE DATABASE" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE DATABASE mydatabase")
Check if Database Exists
You can check if a database exist by listing all databases in your system by using the "SHOW DATABASES" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW DATABASES")
for x in mycursor:
print(x)
Create Table
Creating a Table
To create a table in MySQL, use the "CREATE TABLE" statement.
Make sure you define the name of the database when you create the connection
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
Check if Table Exists
You can check if a table exist by listing all tables in your database with the "SHOW TABLES" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SHOW TABLES")
for x in mycursor:
print(x)
Primary Key
When creating a table, you should also create a column with a unique key for each record.
This can be done by defining a PRIMARY KEY.
We use the statement "INT AUTO_INCREMENT PRIMARY KEY" which will insert a unique number for each record. Starting at 1, and increased by one for each record.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")
Insert Into Table
To fill a table in MySQL, use the "INSERT INTO" statement.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")
Insert Multiple Rows
To insert multiple rows into a table, use the executemany() method.
The second parameter of the executemany() method is a list of tuples, containing the data you want to insert:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
Get Inserted ID
You can get the id of the row you just inserted by asking the cursor object.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("Michelle", "Blue Village")
mycursor.execute(sql, val)
mydb.commit()
print("1 record inserted, ID:", mycursor.lastrowid)
Select From a Table
To select from a table in MySQL, use the "SELECT" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Selecting Columns
To select only some of the columns in a table, use the "SELECT" statement followed by the column name(s):
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT name, address FROM customers")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Select With a Filter
When selecting records from a table, you can filter the selection by using the "WHERE" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers WHERE address ='Park Lane 38'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Wildcard Characters
You can also select the records that starts, includes, or ends with a given letter or phrase.
Use the % to represent wildcard characters:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers WHERE address LIKE '%way%'"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Prevent SQL Injection
When query values are provided by the user, you should escape the values.
This is to prevent SQL injections, which is a common web hacking technique to destroy or misuse your database.
The mysql.connector module has methods to escape query values:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers WHERE address = %s"
adr = ("Yellow Garden 2", )
mycursor.execute(sql, adr)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Sort the Result
Use the ORDER BY statement to sort the result in ascending or descending order.
The ORDER BY keyword sorts the result ascending by default. To sort the result in descending order, use the DESC keyword.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers ORDER BY name"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
ORDER BY DESC
Use the DESC keyword to sort the result in a descending order.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT * FROM customers ORDER BY name DESC"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Delete Record
You can delete records from an existing table by using the "DELETE FROM" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DELETE FROM customers WHERE address = 'Mountain 21'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) deleted")
Delete a Table
You can delete an existing table by using the "DROP TABLE" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE customers"
mycursor.execute(sql)
Drop Only if Exist
If the table you want to delete is already deleted, or for any other reason does not exist, you can use the IF EXISTS keyword to avoid getting an error.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "DROP TABLE IF EXISTS customers"
mycursor.execute(sql)
Update Table
You can update existing records in a table by using the "UPDATE" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'"
mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
Limit the Result
You can limit the number of records returned from the query, by using the "LIMIT" statement:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM customers LIMIT 5")
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Material Review : 30 Days Of Phyton by github Asabeneh ( 4 Hari Pertama )
Ringkasan 30 Days Of Phyton by ASABENEH
Pada materi mata kuliah pengembangan aplikasi berbasis web kemarin kami mempelajari materi phyton dari w3 school dan mencoba menerapkannya seperti projek "30 days of phyton" yang dilakukan oleh pengguna github bernama Asabeneh
FUNGSI PADA PHYTON
Bagian yang kami pelajari adalah 4 hari pertama yang dilakukan pada projek tersebut. Di bagian awal projek itu menjelaskan apasaja fungsi yang tertanam pada bahasa pemograman phyton diantaranya, print(), len(), type(), int(), float(), str(), input(), list(), dict(), min(), max(), sum(), sorted(), open(), file(), help(), and dir(). Fungsi fungsi tersebut berguna untuk mengeksekusi perintah tertentu pada phyton. Tentu saja fungsi ini berbeda dengan variabel.
VARIABEL PADA PHYTON
Pada bagian selanjutnya pada projek tersebut adalah variabel. Pasti kalian sudah tahu bahwa variabel adalah pendeklarasian yang menyimpan data pada memori kompute. Disini penulis github tersebut merekomendasikan Mnemonic variabel. Dikutip dari laman github tersebut Mmeminic variabel merupakan nama variabel yang mudah untuk diingat dan disatukan. Beberapa hal yang tidak dapat dijadikan variabel diantaranya : Penulisan angka di awal, spesial karakter kecuali underscore, dan hyphen. Variable juga bersifat case sensitive dimana Besar kecil nya huruf berpengaruh pada penulisan variabel. Untuk mendeklarasikan variabel lebih dari 1 dapat dilakukan dalam 1 baris dengan memisahkannya menggunakan koma.
TIPE DATA PADA PHYTON
Phyton juga memiliki tipe data serupa dengan bahasa pemograman lainnya. Seperti tipe data string untuk variabel yang mengandung huruf dan kata, integer untuk bilangan bulat, float untuk bilangan desimal dan sebagainya. Angka dapat dideklarasikan sebagai string namun jika angka tersebut digunakan untuk perhitungan maka ubah string tersebut menjadi integer ataupun float, jika tidak maka hasil coding akan eror sebab tipe data yang tidak sesuai.
OPERATOR PADA PHYTON
Selanjutnya adalah operator. Pemograman Phyton mendukung berbagai jenis operator seperti assignment operator, Arithmetic Operator, Comparison Operator, Logical Operator, dsb.
KESIMPULAN
Materi kuliah web minggu kemarin mengajarkan Python melalui proyek “30 days of Python”oleh Pengguna Github bernama Asabeneh, mencakup fungsi dasar, penggunaan variabel yang efektif, tipe data, dan operator. Pentingnya pemilihan nama variabel dan konversi tipe data yang tepat untuk menghindari error juga ditekankan.
Regards,
Zulfikar
[BAHASA INDONESIA] Material Review About Web Server, ASP, and JSP
WEB SERVER, ASP, DAN JSP
MUHAMMAD ZULFIKAR (06202240017)
MUHAMMAD ZULFIKAR (06202240017)
- Web server adalah perangkat lunak dan perangkat keras yang menerima permintaan melalui HTTP atau HTTPS, protokol jaringan yang dibuat untuk mendistribusikan konten web. Web server bertanggung jawab untuk mencari, memproses, dan mengirimkan konten web, seperti halaman web, aplikasi, gambar, dan video, ke browser klien. Web server dapat melayani konten statis atau dinamis, tergantung pada apakah konten tersebut tetap atau berubah sebagai respons terhadap input pengguna.
- Active Server Pages (ASP) adalah bahasa skrip sisi server dan mesin untuk halaman web dinamis yang dikembangkan oleh Microsoft. ASP pertama kali dirilis pada Desember 1996, sebelum digantikan pada Januari 2002 oleh ASP.NET. ASP memungkinkan kode Java tertentu untuk dimasukkan ke dalam halaman HTML menggunakan tag JSP tertentu. Halaman JSP dikompilasi menjadi servlet sebelum dieksekusi, menggabungkan kekuatan Java dengan kemudahan HTML. ASP mendukung banyak model pengembangan, seperti ASP.NET Web Forms, ASP.NET MVC, ASP.NET Web Pages, ASP.NET API, dan ASP.NET Core. ASP juga menyediakan beberapa fitur tambahan seperti Expression Language, Custom Tags, dll.
- Java Server Pages (JSP) adalah teknologi pengembangan web yang mendukung konten dinamis. JSP memungkinkan injeksi konten dinamis ke dalam konten statis menggunakan Java dan Java Servlets. Kita dapat membuat permintaan ke Java Servlet, melakukan logika yang relevan, dan membuat tampilan spesifik di sisi server untuk dikonsumsi di sisi klien.
Regards,
MUHAMMAD ZULFIKAR
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
Beberapa sumber terkait materi yang dapat kamu pelajari.
(1) Active Server Pages – Wikipedia. https://en.wikipedia.org/wiki/Active_Server_Pages.
(2) ASP Tutorial – W3Schools. https://www.w3schools.com/asp/.
(3) What are Active Server Pages (ASP)? – Definition from Techopedia. https://www.techopedia.com/definition/23088/active-server-pages-asp.
(4) Halaman Peladen Aktif – Wikipedia bahasa Indonesia, ensiklopedia bebas. https://id.wikipedia.org/wiki/Halaman_Peladen_Aktif.
(5) What Is ASP? A Comprehensive Guide to Active Server Pages – HostAdvice. https://hostadvice.com/blog/web-hosting/what-is-asp/.
(6) Guide to JavaServer Pages (JSP) | Baeldung. https://www.baeldung.com/jsp.
(7) Learn JSP Tutorial – javatpoint. https://www.javatpoint.com/jsp-tutorial.
(8) Java Server Pages (JSP)Tutorial – Great Learning. https://www.mygreatlearning.com/blog/java-server-pages-jsptutorial/.
(9) Demystifying JSP and Servlets: Building Dynamic Web Applications in Java. https://medium.com/@ramjasprankur/demystifying-jsp-and-servlets-building-dynamic-web-applications-in-java-ad3e69ba310d.
(10) JSP Tutorial. https://www.tutorialspoint.com/jsp/index.htm.
(11) What is a Web Server? – Definition, Features, Uses, And More (2023). https://www.computertechreviews.com/definition/web-server/.
(12) Web server – Wikipedia. https://en.wikipedia.org/wiki/Web_server.
(13) What Is a Web Server? | phoenixNAP IT Glossary. https://phoenixnap.com/glossary/web-server.
(14) What is a Web Server? | A definition – Wix.com. https://www.wix.com/encyclopedia/definition/web-server.
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
Material Review About Python
I will tell you about the material that has been explained by Mr. Hilman Jihadi,S.Kom. ,MMSI. The material learned about programming using python through w3school.
You can open w3school via the following link https://www.w3schools.com/python/.
Python is a computer programming language often used to build websites and software, automate tasks, and analyze data. It was created by Guido van Rossum, and released in 1991.
It is used for:
- web development (server-side),
- software development,
- mathematics,
- system scripting.
Python writing is simpler than other programming languages. For example, to display "Hello World, you just have to type
print("Hello, World!")
× Python can be used on a server to create web applications.
× Python can be used alongside software to create workflows.
× Python can connect to database systems. It can also read and modify files.
× Python can be used to handle big data and perform complex mathematics.
× Python can be used for rapid prototyping, or for production-ready software development.
The most recent major version of Python is Python 3, which we shall be using in this tutorial. However, Python 2, although not being updated with anything other than security updates, is still quite popular.
Before continuing the practice of python programming, you need to know whether your computer already has python or not. If you don't have Python on your computer, you can install it following the following tutorial https://www.w3schools.com/python/python_getstarted.asp
If it is installed, you can continue learning Python programming.
The way to run a python file is like this on the command line:
C:\Users\Your Name>python helloworld.py
Where "helloworld.py" is the name of your python file.
You can write hello world as exemplified earlier Then you can save it and run the program.
The output should read:
Hello, World!
Whenever you are done in the python command line, you can simply type the following to quit the python command line interface:
exit()
That's what was learned from the past material. Hopefully you can understand what I said. If it's not clear you can open my lecturer's youtube video at https://youtu.be/66CBwYcnIf0?si=pywrfo656i82Ee2I
Thank you
Regards,
Muhammad Zulfikar


.png)
