簡體   English   中英

python 中的數據庫,帶有 sqlite3 和用戶輸入

[英]Database in python with sqlite3 and user input

我正在嘗試在 python 中創建一個小型數據庫,該數據庫最終將包含我感興趣的大學的所有信息。我希望能夠隨時添加一所大學並向其中添加數據或從中刪除數據我已經輸入了一個。我想回答我在代碼中輸入的所有問題,然后將所有這些答案保存在數據庫中。 我現在有一些錯誤,任何幫助將不勝感激。 謝謝!

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)

至少有一個問題是在列名中Visit? . 它包含一個特殊字符( ? ),因此需要像使用雙引號一樣進行轉義,例如。 "Visit?" . 也允許使用反引號 ( ) or square brackets []`。 為列選擇不同的名稱可能還為時不晚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM