You are on page 1of 2

7.

Create table and insert values

Aim

To create a table and insert values using Python MySQL

Algorithm

Step 1: import sqlite3

Step 2: Create the table named salesman using create table (salesman_id n(5), name char(30),
city char(35), commission decimal(7,2));")

Step 3: Get the values of each field

Step 4: Insert the values into the table using INSERT INTO salesman(salesman_id, name, city,
commission)

Step 5: Close the database connection

Program

import sqlite3

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

cursor = conn.cursor ()

#create the salesman table

cursor.execute("CREATE TABLE salesman1(salesman_id n(5), name char(30), city char(35),


commission decimal(7,2));")

s_id = input('Salesman ID:')

s_name = input('Name:')

s_city = input('City:')

s_commision = input('Commission:')

cursor.execute("""

INSERT INTO salesman(salesman_id, name, city, commission)

VALUES (?,?,?,?)

""", (s_id, s_name, s_city, s_commision))


conn.commit ()

print ( 'Data entered successfully.' )

conn . close ()

if (conn):

conn.close()

print("\nThe SQLite connection is closed.")

Output

Salesman ID:5

Name:Santhosh

City:Bagalore

Commission:5

Data entered successfully.

The SQLite connection is closed.

Result

The program for creating a table and inserting values into the table was eexecuted
successfully.

You might also like