Hi, just joined this website as you guys seemed like the perfect people to ask for help, I'm creating a database system using python for car rental with tables customers,cars,and rentals. The customer table is fine but on my add_cars() function which adds a new car it is showing some error unlike the customer table. I've attached the customer table code which works and the car one which doesn't work :(

def initialise():
    MyConnection = connect ('rockitrally.db')
    MyCursor = MyConnection.cursor()
    MyCursor.execute('''CREATE TABLE Customers
                    (CustomerID INTEGER PRIMARY KEY,
                    Forename TEXT,
                    Surname TEXT,
                    DOB TEXT,
                    Address TEXT,
                    Phone TEXT,
                    Email TEXT)''')
    MyCursor.execute('''CREATE TABLE Cars
                    (CarID INTEGER PRIMARY KEY,
                    Make TEXT,
                    Model TEXT,
                    Miles INTEGER,
                    Price REAL,
                    Number_Rentals TEXT)''')
    MyCursor.execute('''CREATE TABLE Rentals
                    (RentalID INTEGER PRIMARY KEY,
                    CustomerID INTEGER,
                    CarID INTEGER,
                    Forename TEXT,
                    Surname TEXT,
                    Make TEXT,
                    Model TEXT,
                    Price REAL)''')
def add_customers():
    MyConnection = connect('rockitrally.db')
    MyCursor = MyConnection.cursor()
    Forename=input("Enter the customer's forename: ")
    Surname=input("Enter the customer's surname: ")
    DOB=input("Enter the customer's DOB: ")
    Address=input("Enter the customer's address: ")
    Phone=input("Enter the customer's phone no: ")
    Email=input("Enter the customer's email: ")
    MyCursor.execute("INSERT INTO Customers VALUES (NULL,?,?,?,?,?,?)",(Forename,Surname,DOB,Address,Phone,Email))
    MyConnection.commit()
    MyCursor.close()
    MyConnection.close()
def add_cars():
    MyConnection = connect('rockitrally.db')
    MyCursor = MyConnection.cursor()
    Make=input("Enter the car's make: ")
    Model=input("Enter the car's model: ")
    Miles=int(input("Enter the car's mileage: "))
    Price=float(input("Enter the car's rental price: "))
    Number_Rentals=input("Enter how many times the car has been rented: ")
    MyCursor.execute("INSERT INTO Cars VALUES (NULL,?,?,?,?,?)",(Make,Model,Miles,Price,Number_Rentals))
    MyConnection.commit()
    MyCursor.close()
    MyConnection.close()

MyCursor.execute("INSERT INTO Cars VALUES (NULL,?,?,?,?,?)",(Make,Model,Miles,Price,Number_Rentals))
sqlite3.OperationalError: table Cars has 4 columns but 6 values were supplied

The ID for customers and cars is automatically generated

Please help :(

Recommended Answers

All 2 Replies

nvm sorted it out. thanks

First, what SQL db are you using. Assuming SQLite, the following code works. Note that "Urgent Help" does not stimulate us since it usually means that you have waited until the last minute to do some homework. I am guessing that the problem is that you don't close the db in initialise() and then open it again in the function. You should only open the db once and pass the cursor and connection.

import sqlite3 as sqlite

def initialise():
    MyConnection = sqlite.connect ('rockitrally.db')
    MyCursor = MyConnection.cursor()
    MyCursor.execute('''CREATE TABLE Customers
                    (CustomerID INTEGER PRIMARY KEY,
                    Forename TEXT,
                    Surname TEXT,
                    DOB TEXT,
                    Address TEXT,
                    Phone TEXT,
                    Email TEXT)''')
    MyCursor.execute('''CREATE TABLE Cars
                    (CarID INTEGER PRIMARY KEY,
                    Make TEXT,
                    Model TEXT,
                    Miles INTEGER,
                    Price REAL,
                    Number_Rentals TEXT)''')
    MyCursor.execute('''CREATE TABLE Rentals
                    (RentalID INTEGER PRIMARY KEY,
                    CustomerID INTEGER,
                    CarID INTEGER,
                    Forename TEXT,
                    Surname TEXT,
                    Make TEXT,
                    Model TEXT,
                    Price REAL)''')

    return MyCursor, MyConnection

def add_cars(cur, con):
    input = raw_input                     ## for Python 2.X
    Make=input("Enter the car's make: ")
    Model=input("Enter the car's model: ")
    Miles=int(input("Enter the car's mileage: "))
    Price=float(input("Enter the car's rental price: "))
    Number_Rentals=input("Enter how many times the car has been rented: ")
    cur.execute("INSERT INTO Cars VALUES (NULL,?,?,?,?,?)",(Make,Model,Miles,Price,Number_Rentals))
    con.commit()

def print_all_cars(cur):
    cur.execute('SELECT * FROM Cars')
    for row in cur:
        print row

cur, con = initialise()
add_cars(cur, con)
print_all_cars(cur, con)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.