Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the Python code for creating a table based on the following SQL statement: CREATE TABLE phone ( phone_id INT, country_code INT NOT NULL, phone_number

  1. Write the Python code for creating a table based on the following SQL statement:
    • CREATE TABLE phone (
    • phone_id INT,
    • country_code INT NOT NULL,
    • phone_number INT NOT NULL,
    • phone_type VARCHAR(12),
    • PRIMARY KEY(phone_id)
    • );
  2. Write the Python code to select rows from the table based on the following SQL statement:
    • SELECT phone_number
    • FROM phone
    • WHERE country_code = 'US'
  3. Write the Python code to update rows in the table based on the following SQL statement:
    • UPDATE phone
    • SET phone_type = 'MOBILE
    • WHERE phone_type = 'CELLULAR'
  4. Write the Python code to delete rows in the table based on the following SQL statement:
    • DELETE FROM phone
    • WHERE country_code = 'XX'
  5. Write the Python code to drop the table based on the following SQL statement:
    • DROP TABLE phone
Screenshot 2023-06-16 at 12.54.26 AM.png 

CACATIU IMDLC. import salite3 # connecting to the database connection = sqlite3.connect("CIS261.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), 1name VARCHAR(30), city VARCHAR (30), gender CHAR(1), joining DATE);""" # execute the statement crsr.execute(sql_command) # close the connection connection.close() SELECTING DATA: #importing the module import sqlite3 # connect with the database. connection = sqlite3.connect("CIS261.db") #cursor object crsr= connection.cursor() # execute the command to select the data from the table emp crsr.execute("SELECT fname, 1name FROM emp Where EmpCity = 'Boston'") UPDATING DATA: import sqlite3 # Connecting to database conn = sqlite3.connect('CIS261.db') # Creating a cursor object using # the cursor() method cursor = conn.cursor() # Updating cursor.execute('''UPDATE emp SET 1name = "Jyoti" WHERE fname="Rishabh"; ''') # Commit your changes in the database conn.commit() # Closing the connection. conn.close() DELETING DATA: # Import module import sqlite3 # Connecting to the database conn = sqlite3.connect ('CIS261.db') # Creating a cursor object using # the cursor() method cursor = conn.cursor() # Updating cursor.execute(' 'DELETE FROM emp WHERE fname="Rishabh"; ''') # Commit your changes in the database conn.commit() # Closing the connection conn.close() DROP TABLE: # Import module import sqlite3 # Connecting to database conn = sqlite3.connect('CIS261.db') # Creating a cursor object using # the cursor() method cursor conn.cursor() # Updating cursor.execute(''DROP TABLE Student;''') # Commit your changes in the database conn.commit() # Closing the connection conn.close()

Step by Step Solution

3.45 Rating (158 Votes )

There are 3 Steps involved in it

Step: 1

You can use Python with a library like SQLAlchemy to interact with a SQL database Heres how you can ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Databases questions

Question

=+c) What are the RRRs? Based on the RRRs, what action is best?

Answered: 1 week ago