简体   繁体   中英

Database in python with sqlite3 and user input

I am trying to make a small database in python that will eventually house all of the information for the colleges that I am interested in. I want to be able to add a college in at any time and add data to it or take data away from one that I already put in. I want to answer all of the questions that I put in the code and then have all of those answers saved in the database. I am having some errors right now, any help would be greatly appreciated. Thanks!

import sqlite3

connection = sqlite3.connect("CollegeDB.db")

cursor = connection.cursor()

cursor.execute("CREATE TABLE Colleges (Name TEXT, Enrollment INTEGER, Location TEXT, Acceptance TEXT, Matriculation INTEGER, Testing TEXT, ACT INTEGER, SAT INTEGER, Tuition INTEGER, Scholarships TEXT, ApplicationDeadline BLOB, Visit? TEXT, Interview TEXT, Major TEXT, GreekLife INTEGER, ReligiousAffiliation TEXT)")

collegename1 = input("What is the name of the college?")
enrollment1 = input("What is the total number of undergrads?")
location1 = input("Where is it located?")
acceptance1 = float(input("What is the acceptance rate?"))
matriculation1 = int(input("What is the matriculation?"))
testing1 = input("Is testing optional?")
act1 = int(input("What is the minimum ACT score?"))
sat1 = int(input("What is the minimum SAT score?"))
tuition1 = int(input("What is the cost of tuition?"))
scholarship1 = input("Do they offer scholarships?")
applicationdeadline1 = input("What is the application deadline?")
visit1 = input("Have you visited?")
interview1 = input("Are interviews offered?")
major1 = input("What major interests you from this university?")
greeklife1 = int(input("What is the greek life percentage?"))
religiousaffiliation1 = input("Is there a religious affiliation?")

cursor.execute("INSERT INTO Colleges VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (
    collegename1, enrollment1, location1, acceptance1, matriculation1, testing1, act1, sat1, tuition1, scholarship1,
    applicationdeadline1, visit1, interview1, major1, greeklife1, religiousaffiliation1))

rows = cursor.execute("SELECT Name, Enrollment, Location, Acceptance, Matriculation, Testing, ACT, SAT, Tuition, "
                      "Scholarships, ApplicationDeadline, Visit?, Interview, Major, GreekLife, ReligiousAffiliation")
print(rows)

At least one problem is in the column name Visit? . It contains a special character ( ? ), and as such needs to be escaped as with double-quotes eg. "Visit?" . Backticks ( ) or square brackets []` are also allowed. It's probably not too late to choose a different name for the column.

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