Sqlite3 error in Project, HELP!

Hello! I am currently working on the hotel database project in the advance python3 class.

https://www.codecademy.com/courses/learn-advanced-python/projects/analyzing-hotel-databases-with-python

I’m running this code and I get an error that states:

Traceback (most recent call last):
File “script.py”, line 40, in
curs.executemany(’’‘INSERT INTO bra_customers VALUES (?,?,?,?,?,?,?,?,?,?,?,?)’’’, bra)
sqlite3.ProgrammingError: Recursive use of cursors not allowed.

Import module

import sqlite3

Task 1: Create connection object

con = sqlite3.connect(‘hotel_booking.db’)

Task 2: Create cursor object

curs = con.cursor()

Task 3: View first row of booking_summary

#print(curs.execute(’’‘SELECT * FROM booking_summary’’’).fetchone())

Task 4: View first ten rows of booking_summary

#print(curs.execute(’’‘SELECT * FROM booking_summary’’’).fetchmany(10))

Task 5: Create object bra and print first 5 rows to view data

bra = (curs.execute(’’‘SELECT * FROM booking_summary WHERE country = ‘BRA’;’’’))

print(bra.fetchmany(5))

Task 6: Create new table called bra_customers

curs.execute(’’‘CREATE TABLE IF NOT EXISTS
bra_customers(
num INTEGER,
hotel TEXT,
is_cancelled INTEGER,
lead_time INTEGER,
arrival_date_year INTEGER,
arrival_date_month TEXT,
arrival_date_day_of_month INTEGER,
adults INTEGER,
children INTEGER,
country TEXT,
adr REAL,
special_requests INTEGER
)
‘’’)

Task 7: Insert the object bra into the table bra_customers

curs.executemany(’’‘INSERT INTO bra_customers VALUES (?,?,?,?,?,?,?,?,?,?,?,?)’’’, bra)

EDIT:
Nevermind! I figured it out.

bra_list = bra.fetchall()

I added this line, I guess you needed to have a fetch method to get the actual tuple of values to add into the new table.

Revisiting this topic again, the above edit i stated did not fix the issue, it seems the DB entry I put doesn’t get saved at all because printing out bra_customers yields an empty list.

Went back to the section lessons that covered this, doesn’t seem to have an answer as none of the materials mentioned this issue. The code I used was what they are telling me to use in the hint section well!

How do I get rid of the recursive use error?

At the risk of necroing a somewhat old thread, I also ran into the same issue. Since I could not find any other references to it, I wanted to share a solution to the insert issue. As the OP stated, they solved the recursion error with .fetchall(), for which I came to same conclusion.

I found that if I executed step 5 again just before the .executemany() in step 7, then it would successfully insert the rows. That suggested to me that in doing the create table, it was perhaps resetting the cursor.

Reading the sqlite3 online documentation, it stated:
“The INSERT statement implicitly opens a transaction, which needs to be committed before changes are saved in the database (see Transaction control for details).”

By adding the commit it solved the issue for me, and I was able to fetch the rows:
con.commit()

Alternatively, I found that by creating a second cursor, the original curs, and a write_curs, I used the write_curs to do the inserts and table creation separate from the last select for bra, and that also seemed to work. I feel the commit is probably the better solution. It is certainly the cleaner one and should be preferred. Even better would be to use a context manager, which I realize goes beyond the examples in the lesson, however is covered in the Intermediate Python course and also pointed out in the sqlite3 docs.

# Successful, con.commit() is called automatically afterwards
with con:
    con.executemany('''INSERT INTO bra_customers VALUES (?,?,?,?,?,?,?,?,?,?,?,?)''', bra.fetchall())