flash
flash

Reputation: 3

NameError: name 'db' is not defined,can anyone pls explain this to me,i m new to this field and need guidelines

dbc_in_file = sqlite3.connect('park') #create db in file if not exists
dbc_in_file.close()
dbc_in_file = sqlite3.connect('park1') #connect to db
db_cursor = dbc_in_file.cursor() 

################################################
#TABLE FOR CUSTOMER INFO
db_cursor.execute('''CREATE TABLE PARK_INFO
(CUSTOMER_ID INT PRIMARY KEY NOT NULL,
 SLOTS_RESERVED  INT ,
 SLOTS_OCCUPIED INT);''')
tup1=(100,3,1)
tup2=(101,2,0)
tup3=(102,15,8)
data=[tup1,tup2,tup3]
db_cursor.executemany("insert into PARK_INFO values (?, ?, ?)",data)
dbc_in_file.commit()
db_cursor.execute("SELECT CUSTOMER_ID, SLOTS_RESERVED, SLOTS_OCCUPIED from PARK_INFO")
print(db_cursor.fetchall())
db_cursor.execute("drop table PARK_INFO") 

#################################################
#TABLE FOR VECHILE INFO OF CUSTOMERS
db_cursor.execute('''CREATE TABLE VECHILEEe_INFO
            (CUSTOMER_ID INT PRIMARY KEY NOT NULL,
            VECHILE_TYPE TEXT NOT NULL,
            TIME_REQUIRED_HRS INT);''' )
tup1=(101,'car',4)
tup2=(102,'truck',10)
tup3=(103,'bike',2)
data=[tup1,tup2,tup3]
db_cursor.executemany("insert into VECHILEEe_INFO values (?, ?, ?)",data)
dbc_in_file.commit()
db.cursor.execute("SELECT CUSTOMER_ID, VECHILE_TYPE, TIME_REQUIRED_HRS from VECHILEEe_INFO")
print(db_cursor.fetchall())
db_cursor.execute("drop table VECHILEEe_INFO")

and sometimes also it's telling that the table vechileee_info already exits,how to solve this too?

And is there any other mistakes present in the code?

Upvotes: 1

Views: 139

Answers (1)

Jackson
Jackson

Reputation: 1223

Drop table if it already exist with

DROP TABLE IF EXISTS vechileee_info;

Also shouldn't

db.cursor.execute("SELECT CUSTOMER_ID, VECHILE_TYPE, TIME_REQUIRED_HRS from VECHILEEe_INFO")

be

db_cursor.execute("SELECT CUSTOMER_ID, VECHILE_TYPE, TIME_REQUIRED_HRS from VECHILEEe_INFO")

Probably what cause db not defined error

Upvotes: 1

Related Questions