ass 3 con


Q7 explain SQL
ans
.
Here, we are going to connect SQLite with Python. Python has a native library for SQLite. Let us explain how it works.

  1. To use SQLite, we must import sqlite3.
  2. Then create a connection using connect() method and pass the name of the database you want to access if there is a file with that name, it will open that file. Otherwise, Python will create a file with the given name.
  3. After this, a cursor object is called to be capable to send commands to the SQL. Cursor is a control structure used to traverse and fetch the records of the database. Cursor has a major role in working with Python. All the commands will be executed using cursor object only.
  4. To create a table in the database, create an object and write the SQL command in it with being commented. Example:- sql_comm = ”SQL statement”
  5. And executing the command is very easy. Call the cursor method execute and pass the name of the sql command as a parameter in it. Save a number of commands as the sql_comm and execute them. After you perform all your activities, save the changes in the file by committing those changes and then lose the connection
example 
import sqlite3
  
# connecting to the database 
connection = sqlite3.connect("myTable.db")
  
# cursor 
crsr = connection.cursor()
  
# SQL command to create a table in the database
sql_command = """CREATE TABLE emp ( 
staff_number INTEGER PRIMARY KEY, 
fname VARCHAR(20), 
lname VARCHAR(30), 
gender CHAR(1), 
joining DATE);"""
  
# execute the statement
crsr.execute(sql_command)
  
# SQL command to insert the data in the table
sql_command = """INSERT INTO emp VALUES (23, "Rishabh", "Bansal", "M", "2014-03-28");"""
crsr.execute(sql_command)
  
# another SQL command to insert the data in the table
sql_command = """INSERT INTO emp VALUES (1, "Bill", "Gates", "M", "1980-10-28");"""
crsr.execute(sql_command)
  
# To save the changes in the files. Never skip this. 
# If we skip this, nothing will be saved in the database.
connection.commit()
  
# close the connection
connection.close()


SELECT Operation

cursor = conn.execute("SELECT id, name, 
address, salary from COMPANY")
for row in cursor:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

UPDATE Operation

#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute("UPDATE COMPANY set SALARY = 25000.00 where ID = 1")
conn.commit
print "Total number of rows updated :", conn.total_changes

cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
for row in cursor:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print "Operation done successfully";
conn.close()

DELETE Operation

#!/usr/bin/python

import sqlite3

conn = sqlite3.connect('test.db')
print "Opened database successfully";

conn.execute("DELETE from COMPANY where ID = 2;")
conn.commit()
print "Total number of rows deleted :", conn.total_changes

cursor = conn.execute("SELECT id, name, address, salary from COMPANY")
for row in cursor:
   print "ID = ", row[0]
   print "NAME = ", row[1]
   print "ADDRESS = ", row[2]
   print "SALARY = ", row[3], "\n"

print "Operation done successfully";
conn.close()

Q8. Explain client server with an example
Ans
Python provides a socket class so developers can easily implement socket objects in their source code. To use a socket object in your program, start off by importing the socket library. No need to install it with a package manager, it comes out of the box with Python.
  1. import socket
Now we can create socket objects in our code.
  1. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
This code creates a socket object that we are storing in the “sock” variable. The constructor is provided a family and type parameter respectively.  The family parameter is set to the default value, which is the Address Format Internet.
The type parameter is set to Socket Stream, also the default which enables “sequenced, reliable, two-way, connection-based byte streams” over TCP1.
Once we have an initialized socket object, we can use some methods to open a connectionsend data, receive data, and finally close the connection.
  1. # Connect to an IP with Port, could be a URL
  2. sock.connect(('0.0.0.0', 8080))
  3. ## Send some data, this method can be called multiple times
  4. sock.send("Twenty-five bytes to send")
  5. ## Receive up to 4096 bytes from a peer
  6. sock.recv(4096)
  7. ## Close the socket connection, no more data transmission
  8. sock.close()
server
  1. import socket
  2. serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  3. serv.bind(('0.0.0.0', 8080))
  4. serv.listen(5)
  5. while True:
  6. conn, addr = serv.accept()
  7. from_client = ''
  8. while True:
  9. data = conn.recv(4096)
  10. if not data: break
  11. from_client += data
  12. print from_client
  13. conn.send("I am SERVER\n")
  14. conn.close()
  15. print 'client disconnected'

Popular posts from this blog

18CS45 object oriented concept (OOC) notes, question paper

python application program 15CS664 notes question paper , important question

Operation Research Notes 15CS653