我正在尝试返回一个查询,当返回与课程代码相关的所有课程时,例如'CSC'会给我一个[('CSCA08H3F'),('CSCA20H3F'),('CSCA67H3F)的元组',)]...等等。我知道我必须使用LIKE子句,但我似乎做错了,感觉就像有一种更简单的方法来做这个大声笑......
def create_course_table(db, course_file):
'''Courses Table should be ID,Course,Section,Name'''
con = sqlite3.connect(db)
cur = con. cursor()
cur.execute('''DROP TABLE IF EXISTS Courses''')
# create the table
cur.execute('''CREATE TABLE Courses( ID TEXT , Course TEXT ,
Sections TEXT , Name TEXT)''')
# Read CSV File
csv_reader = open(course_file, 'r')
csv_reader.readline()
# Insert the rows
for line in csv_reader:
course = line.strip().split(',')
ID = course[0]
Course = course[1]
Section = course[2]
Name = course[3:]
for names in Name:
cur.execute('''INSERT INTO Courses VALUES (?, ?, ?, ?)''',
(ID, Course, Section, names))
# commit and close the cursor and connection
con.commit()
cur.close()
con.close()
db = 'exams.db'
def find_dept_courses(db, dept):
'''Return the courses from the given department. Use the "LIKE"
clause in your SQL query for the course name.'''
return run_query(db, ''' SELECT Course FROM Courses WHERE LIKE Course 'ACT%'
OR LIKE Course 'AFS%' OR LIKE Course 'ANT%' OR LIKE Course 'AST%'
OR LIKE Course 'BIO%'
OR LIKE Course 'CHM%' OR LIKE Course CIT%' OR LIKE Course 'CLA%'
OR LIKE Course 'CRT%' OR LIKE Course 'CSC%' OR LIKE Course 'CTL%'
OR LIKE Course 'ECT%' OR LIKE Course 'EES%' OR LIKE Course 'ENG%'
OR LIKE Course 'EST%' OR LIKE Course 'FRE%' OR LIKE Course 'FST%'
OR LIKE Course 'GAS%'
OR LIKE Course 'GGR%' OR LIKE Course 'HIS%' OR LIKE Course 'HLT%'
OR LIKE Course 'IDS%' OR LIKE Course 'JOU%' OR LIKE Course 'LGG%'
OR LIKE Course 'LIN%' OR LIKE Course 'MAT%' OR LIKE Course 'MDS%'
OR LIKE Course 'MGA%' OR LIKE Course'MGE%' OR LIKE Course 'MGF%'
OR LIKE Course 'MGH%' OR LIKE Course 'MGI%' OR LIKE Course 'MGM%'
OR LIKE Course 'MGO%' OR LIKE Course 'MGS%' OR LIKE Course 'MGT%'
OR LIKE Course 'NRO%' OR LIKE Course 'PHL%' OR LIKE Course 'PHY%'
OR LIKE Course 'PLI%' OR LIKE Course 'POL%' OR LIKE Course 'PPG%'
OR LIKE Course 'PSC%' OR LIKE Course 'PSY%' OR LIKE Course 'RLG%'
OR LIKE Course 'SOC%' OR LIKE Course 'STA%' OR LIKE Course 'VPA%'
OR LIKE Course 'VPD%' OR LIKE Course 'VPM%' OR LIKE Course 'WST%' AND WHERE Course = ? ''', [dept])
任何帮助或意见将不胜感激。
答案 0 :(得分:1)
熟悉正确的syntax of the LIKE clause.
您已将列名称放在" LIKE"之后。列名出现在" LIKE"。
之前以下查询将从课程列以指定字符串开头的课程中的所有行返回“课程”列。
SELECT Course FROM Courses WHERE Course LIKE ? || "%";
代替?带有所需的前缀。
答案 1 :(得分:0)
由于所有Course列项都是大写的,因此您可以使用IN而不是LIKE。
在提示符下进行简单测试,该提示显示课程中使用的printf可以将其修剪为3个字符,因此您可以比较每个项目的3个字符进行搜索。
sqlite> .schema
CREATE TABLE test (one text);
sqlite> select * from test;
abcd
efgh
ijkl
abcd
efgh
ijkl
abcd
efgh
ijkl
sqlite> select one from test where printf('%.3s',one) in ('abc');
abcd
abcd
abcd
sqlite>
因此find_dept_courses()可以使用:
def find_dept_courses(db, dept):
'''Return the courses from the given department. Use the "IN"
clause in your SQL query for the course name.'''
return run_query(db, ''' SELECT Course FROM Courses WHERE printf('%.3',Course)
IN ('ACT','AFS','ANT','AST','BIO','CHM','CIT','CLA','CRT','CSC','CTL',
'ECT','EES','ENG','EST','FRE','FST','GAS','GGR','HIS','HLT','IDS','JOU',
'LGG','LIN','MAT','MDS','MGA','MGE','MGF','MGH','MGI','MGM','MGO','MGS',
'MGT','NRO','PHL','PHY','PLI','POL','PPG','PSC','PSY','RLG','SOC','STA',
'VPA','VPD','VPM','WST') AND WHERE Course = ? ''', [dept])
编辑:如果需要sqlite有一个upper()和lower()函数来强制大小写。 sqlite upper function