Unit IV Part2
Unit IV Part2
5 Select statement
You can use the select statement to select data from a particular table. If you want to
select all the columns of the data from a table, you can use the asterisk (*). The syntax for this
will be as follows:
In SQLite3, the SELECT statement is executed in the execute method of the cursor object. For
example, select all the columns of the movie table, run the following code:
If you want to select a few columns from a table, then specify the columns like the following:
select column1, column2 from tables_name
For example,
cursorObj.execute( "SELECT title, year FROM movie")
The select statement selects the required data from the database table, and if you want to fetch
the selected data, the fetchall() method of the cursor object is used. We will demonstrate this in
the next section.
To fetch the data from a database, we will execute the SELECT statement and then will
use the fetchall() method of the cursor object to store the values into a variable. After that, we
will loop through the variable and print all values.
The above code will print out the records in our database as follows:
Output:
('Titanic', 1997, 9.5)
('KGF', 2020, 9.5)
('Dil', 1990, 10.0)
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute("SELECT * FROM movie ")
[print(row) for row in cursorObj.fetchall()]
con.commit()
con.close()
If you want to fetch specific data from the database, you can use the WHERE clause.
4.2.6 Delete
In SQLite database we use the following syntax to delete data from a table:
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
#Deleting
cursorObj.execute("Delete from movie where title='Titanic' ")
con.commit()
con.close()
4.2.7 Drop table
You can drop/delete a table using the DROP statement. The syntax of the DROP statement is
as follows:
drop table table_name
To drop a table, the table should exist in the database. Therefore, it is recommended to use “if
exists” with the drop statement as follows:
For example,
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute(" DROP TABLE IF EXISTS MOVIE ")
con.commit()
con.close()
#crete a cursor
cursorObj = con.cursor()
#Updating
cursorObj.execute("UPDATE MOVIE SET SCORE=10 WHERE TITLE='Dil' ")
Output
Initial Data...
('Titanic', 1997, 9.5)
('KGF', 2020, 9.1)
('Dil', 1990, 8.5)
After updating...
('Titanic', 1997, 9.5)
('KGF', 2020, 9.1)
('Dil', 1990, 10.0)
After deleting...
('KGF', 2020, 9.1)
('Dil', 1990, 10.0)
4.3 Data Analysis: NumpPy
4.3.1 NumPy
Data can be defined as a systematic record of a particular quantity. It is the different
values of that quantity represented together in a set. Data is a collection of facts and figures
to be used for a specific purpose such as a survey or analysis. When data is arranged in an
organized form, can be called information.
Purpose of Data:
Data processing cycle as the term suggests a sequence of steps or operations for
processing data, i.e., processing raw data to the usable form. The processing of data can be
done by number of data processing methods.
2. Processing – Once the input is provided the raw data is processed by a suitable or
selected processing method. This is the most important step as it provides the processed data
in the form of output which will be used further.
3. Output – This is the outcome and the raw data provided in the first stage is now
“processed” and the data is useful and provides information and no longer called data.