Friday, September 4, 2020

Python with SQLite Practice

PYTHON with SQLITE Practice
In [1]:
import sqlite3
In [3]:
import sqlite3

conn = sqlite3.connect('JNV.db')

print ("Opened database successfully");
Opened database successfully
In [7]:
import sqlite3

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

conn.execute('''CREATE TABLE JNVS
         (SNO INT PRIMARY KEY     NOT NULL,
         NAME           TEXT    NOT NULL,
         AGE            INT     NOT NULL,
         ADDRESS        CHAR(50),
         SALARY         REAL);''')
print ("Table created successfully")

conn.close()
Opened database successfully
Table created successfully
In [10]:
import sqlite3

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

conn.execute("INSERT INTO JNVS (SNO,NAME,AGE,ADDRESS,SALARY) \
      VALUES (1, 'rakesh', 32, 'California', 20000.00 )");

conn.execute("INSERT INTO JNVS (SNO,NAME,AGE,ADDRESS,SALARY) \
      VALUES (2, 'Ajay', 25, 'Texas', 15000.00 )");

conn.execute("INSERT INTO JNVS (SNO,NAME,AGE,ADDRESS,SALARY) \
      VALUES (3, 'verma ji', 23, 'Norway', 20000.00 )");

conn.execute("INSERT INTO JNVS (SNO,NAME,AGE,ADDRESS,SALARY) \
      VALUES (4, 'ajit sngh', 25, 'Rich-Mond ', 65000.00 )");

conn.commit()
print ("Records created successfully")
conn.close()
Opened database successfully
Records created successfully
In [15]:
import sqlite3

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

cursor = conn.execute("SELECT sno, name, address, salary from JNVS")
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()
Opened database successfully
ID =  1
NAME =  rakesh
ADDRESS =  California
SALARY =  20000.0 

ID =  2
NAME =  Ajay
ADDRESS =  Texas
SALARY =  15000.0 

ID =  3
NAME =  verma ji
ADDRESS =  Norway
SALARY =  20000.0 

ID =  4
NAME =  ajit sngh
ADDRESS =  Rich-Mond 
SALARY =  65000.0 

Operation done successfully
In [21]:
import sqlite3

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

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

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

print ("Operation done successfully")
conn.close()
Opened database successfully
Total number of rows updated : 1
sno =  1
NAME =  rakesh
ADDRESS =  California
SALARY =  25000.0 

sno =  2
NAME =  Ajay
ADDRESS =  Texas
SALARY =  15000.0 

sno =  3
NAME =  verma ji
ADDRESS =  Norway
SALARY =  20000.0 

sno =  4
NAME =  ajit sngh
ADDRESS =  Rich-Mond 
SALARY =  65000.0 

Operation done successfully
In [22]:
import sqlite3

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

conn.execute("DELETE FROM JNVS WHERE SNO=1")
conn.commit()
print ("Total number of rows updated :", conn.total_changes)

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

print ("Operation done successfully")
conn.close()
Opened database successfully
Total number of rows updated : 1
sno =  2
NAME =  Ajay
ADDRESS =  Texas
SALARY =  15000.0 

sno =  3
NAME =  verma ji
ADDRESS =  Norway
SALARY =  20000.0 

sno =  4
NAME =  ajit sngh
ADDRESS =  Rich-Mond 
SALARY =  65000.0 

Operation done successfully
In [ ]: