简体   繁体   中英

ValueError: invalid literal for int() with base 10: 'SOH'

I received the following error when I try to insert values into sqlite database

Traceback (most recent call last):
  File "C:\SS\Problem13\src\database.py", line 23, in <module>
    sql = """INSERT INTO students (name, student_id, email, school_id, gender, gpa, address) VALUES ('%s','%i','%s','%i','%s','%f','%s')"""%(data[0], int(data[1]),data[2],int(data[3]),data[4],float(data[5]),data[6])
ValueError: invalid literal for int() with base 10: 'SOH'

Below is my code:

import sqlite3
import csv
# Connect to database, if it does not exist, it will create it.
conn = sqlite3.connect("nopoly.db")
# Creating table.
conn.execute("PRAGMA foreign_keys = 1")
conn.execute("CREATE TABLE schools (id INTEGER PRIMARY KEY, name TEXT NOT NULL, description TEXT NOT NULL, director TEXT NOT NULL, email TEXT NOT NULL)")
conn.execute("CREATE TABLE students (id INTEGER PRIMARY KEY, name TEXT NOT NULL, student_id INTEGER NOT NULL, email TEXT NOT NULL, school_id INTEGER NOT NULL REFERENCES schools, gender TEXT NOT NULL, gpa FLOAT NOT NULL, address TEXT NOT NULL)")
# Opening the csv files
schoolreader = csv.reader(open("files/schools.csv"))
studentreader = csv.reader(open("files/studentsp13.csv"))
# Skip the headers
schoolreader.next()
studentreader.next()
# Extract each row and print
for data in schoolreader:
    print data
    sql = """INSERT INTO schools (name, description, director, email) VALUES ('%s','%s','%s','%s')"""%(data[0],data[1],data[2],data[3])
    print "DEBUG", sql
    conn.execute(sql)
for data in studentreader:
    print data
    sql = """INSERT INTO students (name, student_id, email, school_id, gender, gpa, address) VALUES ('%s','%i','%s','%i','%s','%f','%s')"""%(data[0], int(data[1]),data[2],int(data[3]),data[4],float(data[5]),data[6])
    print "DEBUG", sql
    conn.execute(sql)
#
conn.commit()

I understand that in studentsp13.csv, schools are labelled as strings. I wish to know how to map it into integers such that it corresponds to the "schools" table.

I received the following error when I try to insert values into sqlite database

Instead of using string formatting to insert your parameters into your queries, you should pass them as parameters to the conn.execute() method. By doing so, you protect yourself from SQL injection attacks and the data is treated as the proper type by the database. The SQLite python docs provide details and examples on how to do this.

I understand that in studentsp13.csv, schools are labelled as strings. I wish to know how to map it into integers such that it corresponds to the "schools" table.

When you are inserting entries into the schools table, you can record the IDs generated by SQLite into a dictionary and then look them up again when you go to insert your student entries. This would look something like this:

schools = {}
for data in schoolreader:
    print data
    sql = """INSERT INTO schools (name, description, director, email) VALUES ('%s','%s','%s','%s')"""%(data[0],data[1],data[2],data[3])
    conn.execute(sql)
    schools[data[0]] = conn.lastrowid
for data in studentreader:
    school_id = schools[data[3]]
    sql = """INSERT INTO students (name, student_id, email, school_id, gender, gpa, address) VALUES ('%s','%i','%s','%i','%s','%f','%s')"""%(data[0], int(data[1]),data[2],school_id,data[4],float(data[5]),data[6])
    conn.execute(sql)

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