A text file is the file containing simple text. Python provides inbuilt functions to read, create and write text files. We will discuss how to read a text file in Python.There are three ways to read a text file in Python −read() − This method reads the entire file and returns a single string containing all the contents of the file .readline() − This method reads a single line from the file and returns it as string.readlines() − This method reads all the lines and return them as the list of strings.Read a file in PythonLet there be a text file named ... Read More
The UNION statement is used to combine the results of the two SELECT queries without repeating the duplicate values. If both the SELECT queries return same row, it is listed only once.To perform the UNION on two select statements, The number of columns returned must be sameThe datatypes of the columns must be sameThe columns must be returned in same order by both the select statements.SyntaxSELECT column_name FROM table1 UNION SELECT column_name FROM table2Steps to perform union of two select queries using MySQL in pythonimport MySQL connectorimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() ... Read More
The arithmetic operations as the name suggests are used to perform the operations such as additon, subtraction, division, multiplication or modulus.The arithmetic operations are operated on the numeric data in your table.SyntaxTo perform additionSELECT op1+op2 FROM table_nameHere, the op1 and op2 are the column names or the numeric values. If the op1 and op2 are the numeric values, then FROM clause are not required.The + in the above syntax can be replaced by -, *, %, / to perform other arithmetic operations.Steps to perform arithmetic operations in a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using ... Read More
It may be sometimes required to add a new column in a existing table. Suppose we have a “Students” table with columns such as Name, Age, Roll no. We want to add a new column “Address” into our existing table.This can be done by using the ALTER command. The ALTER command is used to modify, drop or update columns in the database. This can also be used to add a new column into the table using ADD clause.SyntaxALTER TABLE table_name ADD new_column_name column_definition [FIRST | AFTER exisiting_column]Here, table_name refers to the name of the table, new_column_name refers to the name ... Read More
We may at times need to check if a particular record exists in a table or not.This can de done using the EXISTS statement. The EXISTS statement returns true if the following subquery returns one or more records.SyntaxSELECT * FROM table_name WHERE EXISTS(sub_query)The subquery if returns one or more rows, the EXISTS will return true.Steps to check if a record exists in a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() methodcreate a query using the appropriate mysql statementsexecute the SQL query using execute() methodclose the connectionSuppose we have the ... Read More
We may sometimes require to get the list of all the tables present in our database. This can be done by using the SHOW TABLES command.The SHOW TABLES command is used to display the table names in a database as well as the server.SyntaxTo show the tables present in a database −SHOW TABLESThe above statement when executed using the cursor object returns the names of the tables present in our database.To show the tables present in a serverSELECT table_name FROM information_schema.tablesSteps to show all tables present in a database and server using MySQL in pythonimport MySQL connectorestablish connection with the ... Read More
It may be required to count the number of columns present in a SQL table.This is done using count(*) function with information_schema.columns and the WHERE clause. The WHERE clause is used to specify the name of the table whose columns are to be counted.SyntaxSELECT COUNT(*) FROM information_schema.columns WHERE table_name= ‘your_table_name’Steps to count columns in a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() methodcreate a query using the appropriate mysql statementsexecute the SQL query using execute() methodclose the connectionSuppose we have a table named “Students” as below −+----------+---------+-----------+------------+ | ... Read More
A comment is a readable explanation provided about something. The comments in the code are provided to explain the purpose of the code staement used. This enables the outside person to understand the need and use of that particular code.The comments are ignored by the compilers and are not executed. These are just for the explanatory purpose and are of great importance.We can add comments to columns in a sql table. These comments will explain the function or tell something about the column. If some other person is viewing and working on that table, he can get idea about the ... Read More
IF statement is a conditional statement in python. It checks for a particular condition and performs some action accordingly.Here, we will discuss the use of IF statement using python to interact with the sql database.SyntaxIF(condition, value_if_true, value_if_false)The IF statemen tcan be used with the SELECT clause to perform selection based on some criteria .Steps to use IF statement to select data from a table using MySQL in pythonimport MySQL connectorestablish connection with the connector using connect()create the cursor object using cursor() methodcreate a query using the appropriate mysql statementsexecute the SQL query using execute() methodclose the connectionSuppose we the following ... Read More
The data is inserted into the table in Mysql using the INSERT statement. While inserting the data into the table, either the data must be provided in the same order as the column definition in the database or the column names must be provided along with the data while using INSERT statement.To get and print the ID of the last inserted row, lastrowid is used. This is a special keyword which is used to get the ID of the last inserted row. There are certain prerequisites to be taken care of before using this method.The ID column must be the ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP