Python & SQLite 填充数据库函数崩溃

Python & SQLite Filling Database Function Crashing

我创建了一个函数来用 API 请求值填充数据库,但是由于我向数据库添加了新列,这些列是通过网页修改的,所以我将 INSERT OR REPLACE 更改为 [=14] =](也试过 UPSERT)但该函数不再 运行。

因为我使用的是 Django 和 VSCode,所以我不知道如何打印东西或看到错误(这个 运行s 在一个单独的线程中所以不会使网页崩溃) .

功能可能有问题,但我看不到什么所以我希望有人能帮助我。

def fill_resources():
    r = pip._vendor.requests.get(BASE_URL+'/api/resources?&maxResults=500',auth=(USER,PASS))
    data0 = json.loads(r.text)

    conn = sqlite3.connect('/app/docaret.sqlite3')
    c = conn.cursor()
    
    for res in data0['data']:
        resId = res['id']
        r1 = pip._vendor.requests.get(BASE_URL+'/api/resources/'+str(resId)+'/administrative?&maxResults=500',auth=(USER,PASS))
        admin = json.loads(r1.text)
        r2 = pip._vendor.requests.get(BASE_URL+'/api/resources/'+str(resId)+'/information?&maxResults=500',auth=(USER,PASS))
        info = json.loads(r2.text)
        r3 = pip._vendor.requests.get(BASE_URL+'/api/resources/'+str(resId)+'/technical-data?&maxResults=500',auth=(USER,PASS))
        tech = json.loads(r3.text)
        if res['relationships']['mainManager']['data'] == None:
            mainManager = 0
        else:
            mainManager = res['relationships']['mainManager']['data']['id']
        if tech['data']['attributes']['diplomas'] != None:
            diplomas = tech['data']['attributes']['diplomas']
        else:
            diplomas = ''
        dipText = ''
        for dip in diplomas:
            newdip = dip + '@µ§'
            dipText += newdip
        collab = {
                "BoondID": resId,
                "lastName": encrypt(res['attributes']['lastName']),
                "firstName": encrypt(res['attributes']['firstName']),
                "dateOfBirth": encrypt(admin['data']['attributes']['dateOfBirth']),
                "placeOfBirth": encrypt(admin['data']['attributes']['placeOfBirth']),
                "address": encrypt(info['data']['attributes']['address']),
                "postcode": encrypt(info['data']['attributes']['postcode']),
                "town": encrypt(info['data']['attributes']['town']),
                "country": encrypt(info['data']['attributes']['country']),
                "email1": encrypt(res['attributes']['email1']),
                "email2": encrypt(info['data']['attributes']['email2']),
                "phone1": encrypt(res['attributes']['phone1']),
                "phone2": encrypt(res['attributes']['phone2']),
                "administrativeComments": encrypt(admin['data']['attributes']['administrativeComments']),
                "title": encrypt(res['attributes']['title']),
                "diplomas": dipText,
                "mainManager": mainManager,
                "agency": res['relationships']['agency']['data']['id'],
                "healthCareNumber": encrypt(admin['data']['attributes']['healthCareNumber']),
                "state": info['data']['attributes']['state']
                }
        values = (collab['BoondID'],collab['lastName'],collab['firstName'],collab['dateOfBirth'],collab['placeOfBirth'],collab['address'],collab['postcode'],collab['town'],collab['country'],collab['email1'],collab['email2'],collab['phone1'],collab['phone2'],collab['administrativeComments'],collab['title'],collab['mainManager'],collab['agency'],collab['healthCareNumber'],collab['diplomas'],collab['state'])
        #collabList.append(collab)
        c.execute("INSERT OR UPDATE INTO RESOURCES (BoondID,lastName,firstName,dateOfBirth,placeOfBirth,address,postcode,town,country,email1,email2,phone1,phone2,administrativeComments,title,mainManager,agency,healthCareNumber,diplomas,state) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", values)
        conn.commit()
    conn.close()

我在 运行独立运行函数时出现以下错误:

Traceback (most recent call last):

  File "<ipython-input-12-87021414c9f9>", line 1, in <module>
    fill_resources()

  File "C:/Users/valen/OneDrive/Bureau/DOCARET/tous les tests/testcontracts.py", line 63, in fill_resources
    c.execute("INSERT OR UPDATE INTO RESOURCES (BoondID,lastName,firstName,dateOfBirth,placeOfBirth,address,postcode,town,country,email1,email2,phone1,phone2,administrativeComments,title,mainManager,agency,healthCareNumber,diplomas,state) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", values)

OperationalError: near "UPDATE": syntax error

SQLite 不支持 INSERT OR UPDATE INTO....

如果你的 SQLite 版本是 3.24.0+,你可以使用 UPSERT

假设 BoondID 是 table 的 PRIMARY KEY 或为其定义了唯一约束:

sql = """
INSERT INTO RESOURCES (
  BoondID, lastName, firstName, dateOfBirth, placeOfBirth, address, 
  postcode, town, country, email1, email2, phone1, phone2, 
  administrativeComments, title, mainManager, agency, healthCareNumber, 
  diplomas, state
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(BoondID) DO UPDATE SET 
(lastName, firstName, dateOfBirth, placeOfBirth, address, postcode, town, 
 country, email1, email2, phone1, phone2, administrativeComments, title, 
 mainManager, agency, healthCareNumber, diplomas, state) =
(EXCLUDED.lastName, EXCLUDED.firstName, EXCLUDED.dateOfBirth,
 EXCLUDED.placeOfBirth, EXCLUDED.address, EXCLUDED.postcode, 
 EXCLUDED.town, EXCLUDED.country, EXCLUDED.email1, EXCLUDED.email2, 
 EXCLUDED.phone1, EXCLUDED.phone2, EXCLUDED.administrativeComments, 
 EXCLUDED.title, EXCLUDED.mainManager, EXCLUDED.agency, 
 EXCLUDED.healthCareNumber, EXCLUDED.diplomas, EXCLUDED.state);
"""
c.execute(sql,  values)