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

DATA MANIPULATION THROUGH SQL · Part 2

Chapter 4: 6 · COMPUTER SCIENCE

a database. So it's used for the fetching of the results. Cursor is used for performing all SQL commands. Note The cursor object is created by calling the cursor() method of connection.

The cursor is used to traverse the records from the result set. You can define a SQL command with a triple quoted string in Python. The reason behind the triple quotes is sometime the values in the table might contain single or double quotes. Example .

. = """ CREATE TABLE Student ( Rollno INTEGER PRIMARY KEY , Sname VARCHAR( ), Grade CHAR( ), gender CHAR( ), Average DECIMAL( , ), DATE);""" In the above example the Rollno field as "INTEGER PRIMARY KEY" A column which is labeled like this will be automatically auto-incremented in SQLite3. To put it in other words: If a column of a table is declared to be an INTEGER PRIMARY KEY, then whenever a NULL will be used as an input for this column, the NULL will be automatically converted into an integer which will one larger than the highest value so far used in that column. If the table is empty, the value will be used.

. . Adding Records To populate (add record) the table "INSERT" command is passed to SQLite. “execute” method executes the SQL command to perform some action.

The following example . . is a complete working example. To run the program you should uncomment the "DROP TABLE" line in the SQL command, if the program has been executed already.

12th Computer Chapter - - Data Manipulation Through SQL Example . . - import sqlite3 connection = sqlite3.connect ("Academy.db") cursor = connection.cursor() = """ CREATE TABLE Student ( Rollno INTEGER PRIMARY KEY , Sname VARCHAR( ), Grade CHAR( ), gender CHAR( ), Average DECIMAL ( , ), DATE);""" cursor.execute( ) = """INSERT INTO Student (Rollno, Sname, Grade, gender, Average, ) VALUES (NULL, "Akshay", "B", "M"," . ", " - - ");""" cursor.execute( ) = """INSERT INTO Student (Rollno, Sname, Grade, gender, Average, ) VALUES (NULL, "Aravind", "A", "M"," .

"," - - ");""" cursor.execute( ) # never forget this, if you want the changes to be saved: connection.commit() connection.close() print("STUDENT TABLE CREATED") OUTPUT

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 →