Category Archives: Library

35.7 Delete From By

Delete Record You can delete records from an existing table by using the “DELETE FROM” statement: Example Delete any record where the address is “Mountain 21”:import mysql.connectormydb = 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”) Output:C:\Users\My Name>python demo_mysql_delete.py1 record(s) deleted Important!: Notice the statement: mydb.commit(). It is required to make the… Read More »

35.6 Order By

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. Example Sort the result alphabetically by name: result:import mysql.connectormydb = mysql.connector.connect(  host=”localhost”,  user=”yourusername“,  password=”yourpassword“,  database=”mydatabase”)mycursor = mydb.cursor()sql = “SELECT * FROM… Read More »

35.5 Where

Select With a Filter When selecting records from a table, you can filter the selection by using the “WHERE” statement: Example Select record(s) where the address is “Park Lane 38”: result:import mysql.connectormydb = 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) Output:C:\Users\My Name>python demo_mysql_where.py(11, ‘Ben’, ‘Park Lane… Read More »

35.4 Select From

Select From a Table To select from a table in MySQL, use the “SELECT” statement: Example Select all records from the “customers” table, and display the result: import mysql.connectormydb = 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) Output: Note: We use the fetchall() method, which fetches all rows from the last executed statement. Selecting Columns… Read More »

35.3 Insert Into Table

Insert Into Table To fill a table in MySQL, use the “INSERT INTO” statement. Example Insert a record in the “customers” table:import mysql.connectormydb = 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.”) Output:C:\Users\My Name>python demo_mysql_insert.py1 record inserted. Important!: Notice the statement: mydb.commit(). It is required to make the… Read More »

35.2 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 Example Create a table named “customers”:import mysql.connectormydb = 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))”)#If this page is executed with no error, you… Read More »

35.1 Create Database

Creating a Database To create a database in MySQL, use the “CREATE DATABASE” statement: Example create a database named “mydatabase”:import mysql.connectormydb = mysql.connector.connect(  host=”localhost”,  user=”yourusername“,  password=”yourpassword“)mycursor = mydb.cursor()mycursor.execute(“CREATE DATABASE mydatabase”)#If this page is executed with no error, you have successfully created a database. Output:C:\Users\My Name>python demo_mysql_create_db.py If the above code was executed with no errors, you have… Read More »

35. MySQL

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 free MySQL database at https://www.mysql.com/downloads/. Install MySQL Driver Python needs a MySQL driver to access… Read More »

34.4 Delete File

Delete a File To delete a file, you must import the OS module, and run its os.remove() function: Example Remove the file “demofile.txt”:import osos.remove(“demofile.txt”) Check if File exist: To avoid getting an error, you might want to check if the file exists before you try to delete it: Example Check if file exists, then delete it:import osif os.path.exists(“demofile.txt”):  os.remove(“demofile.txt”)else:  print(“The file does not… Read More »

34.3 Write/Create File

Write to an Existing File To write to an existing file, you must add a parameter to the open() function: “a” – Append – will append to the end of the file “w” – Write – will overwrite any existing content Example Open the file “demofile2.txt” and append content to the file:f = open(“demofile2.txt”, “a”)f.write(“Now the file has more content!”)f.close() #open… Read More »