Home | | Computer Science 12th Std | Data input by User

Data Manipulation Through SQL - Data input by User | 12th Computer Science : Chapter 15 : Integrating Python with MySql and C++ : Data Manipulation Through SQL

Chapter: 12th Computer Science : Chapter 15 : Integrating Python with MySql and C++ : Data Manipulation Through SQL

Data input by User

code for executing query using input data

Data input by User

In this example we are going to accept data using Python input() command during runtime and then going to write in the Table called "Person"


Example

#code for executing query using input data

import sqlite3

#creates a database in RAM

con =sqlite3.connect("Academy.db")

cur =con.cursor()

cur.execute("DROP Table person")

cur.execute("create table person (name, age, id)")

print("Enter 5 students names:")

who =[input() for i in range(5)]

print("Enter their ages respectively:")

age =[int(input()) for i in range(5)]

print("Enter their ids respectively:")

p_id =[int(input())for i in range(5)]

n =len(who)

for i in range(n):

# This is the q-mark style:

cur.execute("insert into person values (?, ?, ?)", (who[i], age[i], p_id[i]))

#And this is the named style:

cur.execute("select * from person")

#Fetches all entries from table

print("Displaying All the Records From Person Table")

print (*cur.fetchall(), sep='\n' )

OUTPUT

Enter 5 students names:

RAM

KEERTHANA

KRISHNA

HARISH

GIRISH

Enter their ages respectively:

28

12

21

18

16

Enter their ids respectively:

1

2

3

4

5

Displaying All the Records From Person Table

('RAM', 28, 1)

('KEERTHANA', 12, 2)

('KRISHNA', 21, 3)

('HARISH', 18, 4)

('GIRISH', 16, 5)

You can even add records to the already existing table like “Student” Using the above coding with appropriate modification in the Field Name. To do so you should comment the create table statement

Note

Execute (sql[, parameters]) :- Executes a single SQL statement. The SQL statement may be parametrized (i. e. placeholders instead of SQL literals). The sqlite3 module supports two kinds of placeholders: question marks? (“qmark style”) and named placeholders :name (“named style”).

 

Tags : Data Manipulation Through SQL , 12th Computer Science : Chapter 15 : Integrating Python with MySql and C++ : Data Manipulation Through SQL
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 15 : Integrating Python with MySql and C++ : Data Manipulation Through SQL : Data input by User | Data Manipulation Through SQL


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.