Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views5 pages

Unit IV Part2

The document explains the use of the SELECT statement in SQLite for retrieving data from tables, including syntax for selecting all or specific columns and fetching results. It also covers DELETE and DROP statements for removing data and tables, respectively, along with a demonstration of database operations such as creating, inserting, updating, and deleting records. Additionally, it introduces NumPy, a Python package for scientific calculations, detailing its purpose and the data processing cycle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Unit IV Part2

The document explains the use of the SELECT statement in SQLite for retrieving data from tables, including syntax for selecting all or specific columns and fetching results. It also covers DELETE and DROP statements for removing data and tables, respectively, along with a demonstration of database operations such as creating, inserting, updating, and deleting records. Additionally, it introduces NumPy, a Python package for scientific calculations, detailing its purpose and the data processing cycle.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4.2.

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:

select * from table_name

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:

cursorObj.execute("SELECT * FROM movie ")

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.

Fetch all data

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 code will be like this:


import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute("SELECT * FROM movie ")
records=cursorObj.fetchall()
for row in records:
print(row)
con.commit()
con.close()

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)

You can also use the fetchall() in one line as follows:

[print(row) for row in cursorObj.fetchall()]

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:

DELETE FROM table_name [WHERE Clause]


 Import the required module.
 Create a connection object with the database using to connect().
 Create a Cursor object by calling the cursor().
 Finally, use execute() method by passing a DELETE statement as a parameter to it.

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:

drop table if exists table_name

For example,
import sqlite3
con = sqlite3.connect('mydatabase.db')
cursorObj = con.cursor()
cursorObj.execute(" DROP TABLE IF EXISTS MOVIE ")
con.commit()
con.close()

#Program to demonstrate a database operations- create, insert, select, update, delete


and drop
#importing the module
import sqlite3

#create connection object


con = sqlite3.connect('mydatabase.db')

#crete a cursor
cursorObj = con.cursor()

#creating the table


cursorObj.execute('''CREATE TABLE movie(title text, year int, score real)''')

#inserting the data


cursorObj.execute('''INSERT INTO movie VALUES ("Titanic",1997, 9.5)''')
cursorObj.execute('''INSERT INTO movie VALUES ("KGF",2020, 9.1)''')
cursorObj.execute('''INSERT INTO movie VALUES ("Dil",1990, 8.5)''')

#Print the Initial Data


print("Initial Data...")
cursorObj.execute("SELECT * FROM movie ")
[print(row) for row in cursorObj.fetchall()]

#Updating
cursorObj.execute("UPDATE MOVIE SET SCORE=10 WHERE TITLE='Dil' ")

#Print the after updating


print("After updating...")
cursorObj.execute("SELECT * FROM movie ")
[print(row) for row in cursorObj.fetchall()]
#Deleting
cursorObj.execute("Delete from movie where title='Titanic' ")

#Print the after Deleting


print("After deleting...")
cursorObj.execute("SELECT * FROM movie ")
[print(row) for row in cursorObj.fetchall()]

#Drop the table


cursorObj.execute(" DROP TABLE IF EXISTS MOVIE ")

#commit changes in the database


con.commit()

#close the connection


con.close()

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:

1. Improve Business Performance


2. Improve Decision Making
3. To find solution of problems
4. To get desired results
5. To perform various calculation
6. For analyzing various activity

Structured or Unstructured Data


We can easily find structured data in our database system in the form of fields such
as names, dates, addresses etc., as the time goes by, people think how to handle unstructured
data like text, image, video, audio, etc. that might give you something useful to make
decision in your business.

What is a Data Processing Cycle?

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.

Stages of data processing:


1. Input – The raw data after collection needs to be fed in the cycle for processing. This
is considered the first step and called input.

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.

Numpy stands for “Numeric Python” or “Numerical python”.Numpy is a package that


contains several classes, functions, variables etc. to deal with scientific calculations in
Python.
Numpy is useful to create and process single and multi-dimensional arrays. In
addition, numpy contains a large library of mathematics like linear algebra functions and
Fourier transformations.

You might also like