Problem when inserting data to sqlite using Web API python

Viewed 20

Basically, I am trying to read the data from the CSV file and insert into the SQLite using Web API. The data has successfully send to the webAPI, but no row is affected.

### Sending HTTP Request
{'\ufeffCode': 'SBK34', 'Brand': 'Calcutta Fan', 'Type': 'JET FAN'}
### Sending HTTP Request
### HTTP Response Received
### ERROR: 500

CSV file

Code,Brand,type
SBK11,AirPro Fan,CENTRIFUGAL FAN
SBK12,Cincinnati Fan,CENTRIFUGAL FAN

Web Api

@app.route('/api/fans/add', methods=['POST'])
def addFans():
    if not request.json:
        abort(404)

    fans_info = (
        request.json['Code'],
        request.json['Brand'],
        request.json['Type'],
    )
    print('fans added')
    db = sqlite3.connect(DB)
    cursor = db.cursor()

    cursor.execute('''
        INSERT INTO fans(Code, Brand, Type)
        VALUES(?,?,?)
    ''', fans_info)

    user_id = cursor.lastrowid
    db.commit()

Python Script

# LYT
import http.client
import json
import csv
HOST = 'localhost'
PORT = 5000
with open('newfans.csv') as file:
    newfans = csv.DictReader(file)
    for member in newfans:
        conn = http.client.HTTPConnection(HOST, PORT)
        member = dict(member)
        print('### Sending HTTP Request')
        conn.request('POST', '/api/fans/add', json.dumps(member), {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        })
    print('### HTTP Response Received')
    response = conn.getresponse()
0 Answers
Related