Use Jupyter Notebook Q1 Plesae Write New Function Runquery Takes Sql Query Input Returns O Q37205480

Use Jupyter Notebook

”’
Q1. Plesae write a new function “run_query” that takes a SQL Queryas an input;
and returns the ourcome of the query with Pandas “read_sql_query”function.
  
In defining a function, you can import 1) sqlite3 and 2) pandas aspd;
and create connection object ‘conn’ which connects’example.db’
  
You can use the connection object ‘conn’ as an input argument of”read_sql_query”

”’

Example.db


Answer


CODE

import pandas as pd

import sqlite3

def run_query():

conn = sqlite3.connect(“example.db”)

sql = “SELECT * FROM test_table;”

resp = pd.read_sql_query(sql, conn)

conn.close()

return resp

1 import pandas as pd 2 import sqlite3 4 def run_query(): connsqlite3.connect(example.db) sqlSELECT FROM test_table; resp

1 import pandas as pd 2 import sqlite3 4 def

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.