📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 284question

DATA MANIPULATION THROUGH SQL · Part 3

Chapter 4: 6 · COMPUTER SCIENCE

STUDENT TABLE CREATED Of course, in most cases, you will not literally insert data into a SQL table. You will rather have a lot of data inside of some Python data type e.g. a dictionary or a list, which has to be used as the input of the insert statement. The following working example, assumes that you have an already existing database Academy.db and a table Student.

We have a list with data of persons which will be used in the INSERT statement: 12th Computer Chapter - - Example . . - import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() = [("BASKAR", "C", "M"," . "," - - "), ("SAJINI", "A", "F"," .

"," - - "), ("VARUN", "B", "M"," . "," - - "), ("PRIYA", "A", "F"," . "," - - "), ("TARUN", "D", "M"," . "," - - ") ] for p in : = """INSERT INTO Student (Rollno, Sname, Grade, gender,Average, ) VALUES (NULL,"{name}", "{gr}", "{gender}","{avg}","{birthdate}");""" = .format(name=p[ ], gr=p[ ], gender=p[ ],avg=p[ ], birthdate = p[ ]) cursor.execute( ) connection.commit() connection.close() print("RECORDS ADDED TO STUDENT TABLE ") OUTPUT RECORDS ADDED TO STUDENT TABLE In the above program {gr} is a place holder (variable) to get the value.

.format() is a function used to format the value to the required datatype. SQL Query Using Python . The time has come now to finally query our “Student” table. Fetching the data from record is as simple as inserting them.

The execute method uses the SQL command to get all the data from the table. . . SELECT Query “Select” is the most commonly used statement in SQL.

The SELECT Statement in SQL is used to retrieve or fetch data from a table in a database. The syntax for using this statement is “Select * from ” and all the table data can be fetched in an object in the form of list of Tuples. 12th Computer Chapter - - Data Manipulation Through SQL If you run the program . .

- , you would get the following result, depending on the actual data: It should be noted that the database file that will be created will be in the same folder as that of the python file. If we wish to change the path

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →