简体   繁体   中英

SQLite Where Clause Syntax Error

I am creating an SQLite database with data from several CSV files using Python. From File A, I have successfully created a table and added entries to that table. I want to add additional columns to the database from File B. The rows in File A and File B do not match, but they need to match in the database.

I apologize for the length of the code - I am selecting specific rows from the CSV file to add to the database. I included all the code here, in case the context is helpful.

This code works:

with open('/Users/Leo/Desktop/accord_key.csv','rU') as accord_csv:
     accord_csv_reader = csv.reader(accord_csv)
    for row in accord_csv_reader:
        MaskID = row[0]
        female = row[1]
        age = row[2]
        arm = row[3]
        cvd_hx_baseline = row[6]
        female = int(female)
        age = float(age)
        arm = int(arm)
        cvd_hx_baseline = int(cvd_hx_baseline)

        if age < 50: continue
        if ((arm == 5) or (arm == 6) or (arm == 7) or (arm == 8)): continue
        female = str(female)
        age = str(age)
        arm = str(arm)
        cvd_hx_baseline = str(cvd_hx_baseline)
        cur.execute('INSERT OR IGNORE INTO accord_baseline (MaskID) VALUES (?)',
                (MaskID,)
                )
        cur.execute('UPDATE OR IGNORE accord_baseline SET female = '+female+' WHERE MaskID=?',
                (MaskID,)
                )
        cur.execute('UPDATE OR IGNORE accord_baseline SET age = '+age+' WHERE MaskID=?',
                (MaskID,)
                )
        cur.execute('UPDATE OR IGNORE accord_baseline SET arm = '+arm+' WHERE MaskID=?',
                (MaskID,)
                )
        cur.execute('UPDATE OR IGNORE accord_baseline SET cvd_hx_baseline = '+cvd_hx_baseline+' WHERE MaskID=?',
                (MaskID,)
                )
        age = float(age)
        if age >= 75:
            cur.execute('UPDATE OR IGNORE accord_baseline SET age_75 = 1 WHERE MaskID=?',
                    (MaskID,)
                    )
    conn.commit()
    accord_csv.close()

This code does not work:

with open('/Users/Leo/Desktop/otherlabs.csv','rU') as labs_csv:
    labs_csv_reader = csv.reader(labs_csv)
    count = 0
    for row in labs_csv_reader:
        MaskID = row[0]
        visit = row[1]
        gfr = row[7]
        if visit == 'BLR':
            cur.execute('UPDATE OR IGNORE accord_baseline SET mdrd = '+gfr+' WHERE MaskID=?',
                    (MaskID,)
                    )
    conn.commit()
    labs_csv.close()

Since I haven't been able to find an answer with my search, I appreciate any help that you might offer!!

This may or may not be the problem in your case - you are doing only half of the escaping correctly, so it is possible that if ' is in your data the SQL queries get corrupted.

Don't do this:

cur.execute('UPDATE OR IGNORE accord_baseline SET mdrd = '+gfr+' WHERE MaskID=?',
    (MaskID,))

Do this instead:

cur.execute('UPDATE OR IGNORE accord_baseline SET mdrd = ? WHERE MaskID=?',
    (gfr, MaskID))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM