Question

Python Question. I am trying to connect to a database and create a table using Python...

Python Question. I am trying to connect to a database and create a table using Python in this code but I keep getting an error on line 48 saying that "Quotes" is not defined. Could anyone please help me set this up correctly? I am using Azure SQL Database to create this.

import pyodbc
import json
import requests as r
import pprint

connection_string = 'Driver={ODBC Driver 17 for SQL Server};' \
                    'Server=randomquotegenerator.database.windows.net,1433;' \
                    'Database=RandomQuoteGenerator;' \
                    'Uid=carterholliday;' \
                    'Pwd=MIS54002k19;' \
                    'TrustServerCertificate=no;'

conn = pyodbc.connect(connection_string, autocommit=True)

curs = conn.cursor()

curs.execute(
     '''
     if not exists(
        SELECT * 
        FROM INFORMATION_SCHEMA.TABLES 
        WHERE TABLE_NAME = N'Quotes'
        )
     begin
         create table Quotes(
         ID int primary key clustered identity(1,1)
         ,Quote varchar(5000)
         ,QuoteSource varchar(5000)
         )
     end
     else
     begin
        truncate table Quotes
     end

     '''
     )

endpoint_1 = 'https://ron-swanson-quotes.herokuapp.com/v2/quotes/100'
endpoint_2 = 'http://quotesondesign.com/wp-json/posts?filter[orderby]=rand&filter[posts_per_page]=40'

rs_quotes = r.get(endpoint_1)
to_quotes = r.get(endpoint_2)

insert_query = 'insert into Quotes (rs_quotes) values (?)'
insert_query_2 = 'insert into Quotes (to_quotes) values (?)'

for entry in Quotes:
    rows_to_insert.append(entry,)
curs.executemany(insert_query, rows_to_insert)
curs.executemany(insert_query_2, rows_to_insert)

conn.commit()
conn.close()
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The issue here is that,

Quotes variable is not defined, when you are trying to use it. If i understand this piece of code correctly, then you are trying to download 2 kind of quotes from endpoint1 and endpoint2, which are kept in rs_quotes and to_quotes
Then you are trying to insert those quotes in the tables..

For this, instead of below code:

for entry in Quotes:
    rows_to_insert.append(entry,)

use below code:



for entry in rs_quotes :
    rows_to_insert.append(entry,)

Let me know if any issues.. I hope i clarified the issue well.

Add a comment
Know the answer?
Add Answer to:
Python Question. I am trying to connect to a database and create a table using Python...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I'm working on inserting data into a table using MySQL and Python on my Windows...

    Hello, I'm working on inserting data into a table using MySQL and Python on my Windows OS laptop; and I am asked to modify the script so that it executes an insert query in one of my database tables. I am also asked to print the table before and after I execute this query in order to ensure the new information was inserted into the table. I'd like to INSERT INTO my EMPLOYEE table. The columns are employee_id, employee_password, order_id...

  • CREATE TABLE DEPT ( DEPTNO INTEGER NOT NULL, DNAME VARCHAR(14), LOC VARCHAR(13), PRIMARY KEY (DEPTNO)); INSERT...

    CREATE TABLE DEPT ( DEPTNO INTEGER NOT NULL, DNAME VARCHAR(14), LOC VARCHAR(13), PRIMARY KEY (DEPTNO)); INSERT INTO DEPT VALUES (10,'SPORTS','NEW YORK'); INSERT INTO DEPT VALUES (20,'HOME','DALLAS'); INSERT INTO DEPT VALUES (30,'OUTDOOR','CHICAGO'); INSERT INTO DEPT VALUES (40,'CLOTHING','BOSTON'); CREATE TABLE EMP ( EMPNO INTEGER NOT NULL, ENAME VARCHAR(10), JOB VARCHAR(9), MGR INTEGER, SAL FLOAT, COMM FLOAT, DEPTNO INTEGER NOT NULL, FOREIGN KEY (DEPTNO) REFERENCES DEPT (DEPTNO), FOREIGN KEY (MGR) REFERENCES EMP(EMPNO), PRIMARY KEY (EMPNO)); INSERT INTO EMP VALUES (7839,'KING','PRESIDENT',NULL, 5000,NULL,10); INSERT INTO...

  • SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT...

    SQL I have a database CREATE TABLE vendor ( vid CHAR(2) NOT NULL, vname VARCHAR(25) NOT NULL, PRIMARY KEY (vid) ); CREATE TABLE category ( catid CHAR(2) NOT NULL, catname VARCHAR(25) NOT NULL, PRIMARY KEY (catid) ); CREATE TABLE product ( pid CHAR(3) NOT NULL, pname VARCHAR(25) NOT NULL, price NUMERIC (7,2) NOT NULL, vid CHAR(2) NOT NULL, categoryid CHAR(2) NOT NULL, PRIMARY KEY (pid)); CREATE TABLE region ( rid CHAR NOT NULL, rname VARCHAR(25) NOT NULL, PRIMARY KEY (rid)...

  • Part I. Create a library check-out database using Microsoft SQL Server that stores data about books,...

    Part I. Create a library check-out database using Microsoft SQL Server that stores data about books, patrons, and the check-out process. Books (BookID, BookName, Author, YearPublished) Patrons (PatronsID, PatronsName, PatronsAddress, PatronsBirthday) CheckInOut (TransactionID, PatronID, BookID, CheckOutDate, NumDay, ReturnDate, Late, Fees, Paid) - the NumDay field contains the number of days patrons can keep the book, if the return date is over the number of day, then the Late field will have a Y value and a fee of $1.00 per...

  • I am working on multi-table queries for my SQL class using a premade database, which I...

    I am working on multi-table queries for my SQL class using a premade database, which I have included screenshots of. I received assistance and was able to complete a good number of the queries but the bolded ones seem absolutely impossible to do?? How do I write a query for the bolded questions?? I've scoured my textbook and notes and cannot get anything I try to successfully produce results. 1. List all current reservations with the trip ID, customer’s first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT