Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
import sqlite3
import os
import csv
# Remove database if exist
if os.path.isfile('car.db'):
    os.remove('car.db')
#Create database and open database
conn=sqlite3.connect('car.db')
db=conn.cursor()
db.execute("PRAGMA foreign_keys = ON")

db.execute("""CREATE TABLE clients
            (ClientID INTEGER PRIMARY KEY autoincrement,
            First_name TEXT,
            Surname TEXT,
            Age INTEGER,
            Address STRING,
            City TEXT,
            Postcode INTEGER,
            Email STRING,
            Phone_number INTEGER)""")
reader = csv.reader(open('clients.txt', 'r'), delimiter=',')
for row in reader:
    to_db = [row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8] ]
    db.execute('''INSERT INTO clients
(ClientID, First_name, Surname, Age, Address, City,Postcode,Email,Phone_number)
      VALUES (?,?,?,?,?,?,?,?,?);''', to_db)
    
db.execute("""CREATE TABLE cars
            (numbID INTEGER PRIMARY KEY autoincrement,
            Make TEXT,
            Model TEXT,
            Colour TEXT,
            Year INTEGER,
            Transmission TEXT,
            Price INTEGER,
            Stock INTEGER)""")
reader = csv.reader(open('cars.txt', 'r'), delimiter=',')
for row in reader:
    to_db = [row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7] ]
    db.execute('''INSERT INTO cars
(numbID, Make, Model, Colour, Year, Transmission, Price, Stock)
      VALUES (?,?,?,?,?,?,?,?);''', to_db)

db.execute("""CREATE TABLE purchase
            (PurchaseID INTEGER PRIMARY KEY autoincrement,
            numbID INTEGER,
            ClientID INTEGER,
            Date_of_Purchase TEXT,
            ETA_of_Arrival STRING
            FOREIGN KEY(clientID)REFERENCES clients(clientID),
            FOREIGN KEY(numbID)REFERENCES cars(numbID))""")
reader = csv.reader(open('purchase.txt', 'r'), delimiter=',')
for row in reader:
    to_db = [row[0], row[1], row[2], row[3], row[4] ]
    db.execute('''INSERT INTO purchase
(PurchaseID, ClientID, numbID, Date_of_Purchase, ETA_of_Arrival)
      VALUES (?,?,?,?,?);''', to_db)
            
conn.commit()
conn.close()

conn.close()

What I have tried:

I have three tables connected using primary and foreign keys but not sure how it works. I am a beginner in this field. Please help me. Below is the error message I'm getting.

OperationalError: near "FOREIGN": syntax error
client.py, line 52
Posted
Updated 23-Aug-22 1:03am
v2

1 solution

Python
db.execute("""CREATE TABLE purchase
            PurchaseID INTEGER PRIMARY KEY autoincrement,

You need an open parenthesis before PurchaseID thus:
Python
db.execute("""CREATE TABLE purchase
            (PurchaseID INTEGER PRIMARY KEY autoincrement,
 
Share this answer
 
Comments
YunusShuaib 23-Aug-22 6:59am    
Thanks a lot. I'm getting another error code though.
OperationalError: near "FOREIGN": syntax error
client.py, line 52
Richard MacCutchan 23-Aug-22 7:11am    
There is a comma missing at the end of
ETA_of_Arrival STRING

Try and do some basic syntax checking when you get error messages.
YunusShuaib 23-Aug-22 7:31am    
Thank you so much. Really appreciate it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900