Home | 12 ஆம் வகுப்பு | 12வது கணினி அறிவியல் | பயனரால் உள்ளிடைப்படும் தரவு

SQL மூலம் தரவுகளைக் கையாளுதல் - பயனரால் உள்ளிடைப்படும் தரவு | 12th Computer Science : Chapter 15 : Integrating Python with MySql and C++ : Data Manipulation Through SQL

12 வது கணினி அறிவியல் : அலகு 15 : MySql மற்றும் C++ உடன் பைத்தானை ஒருங்கிணைத்தல் : SQL மூலம் தரவுகளைக் கையாளுதல்

பயனரால் உள்ளிடைப்படும் தரவு

code for executing query using input data

பயனரால் உள்ளிடைப்படும் தரவு

பின்வரும் எடுத்துக்காட்டில் இயங்கு நேரத்தில் பைதான் input() கட்டளையைப் பயன்படுத்தி தரவுகளைப் பெற்று அதை "Person" என்ற அட்டவணையில் எழுதுதல்

எடுத்தக்காட்டு 15.10 -1

# 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') 

வெளியீடு

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)

ஏற்கனவே உள்ள "student” என்ற அட்டவணையில் Name புலத்தில் சில மாற்றங்களைச் செய்து பதிவுகளைச் சேர்க்கலாம். இதை செய்ய Create table கூற்றைக் குறிப்புரையில் (Comment) எழுதவும்.

குறிப்பு

Execute (sql[, parameters]) :- இது ஒற்றை SQL கூற்றை இயக்குகிறது. SQL கூற்று அளபுருக்களை கொண்டிருக்கலாம் (அதாவது நிலையுருக்களுக்குப் பதிலாக இடநிரப்பிகள்). sqlite3 இரண்டு வகையான இடநிரப்பிகளை கொண்டுள்ளது. அவை, கேள்விக் குறிகள் (?) மற்றும் பெயரிடப்பட்ட இடநிரப்பிகள் (:name). எ.கா who= 'who ,age=:age

12 வது கணினி அறிவியல் : அலகு 15 : MySql மற்றும் C++ உடன் பைத்தானை ஒருங்கிணைத்தல் : SQL மூலம் தரவுகளைக் கையாளுதல்