Python 3.x 同一列中有多个相似条件

Python 3.x 同一列中有多个相似条件,python-3.x,sqlite,Python 3.x,Sqlite,我试图返回一个查询,当返回与课程代码(例如“CSC”)相关联的所有课程时,将给我一个元组[('CSCA08H3F',),('CSCA20H3F',),('CSCA67H3F',)]…等。我知道我必须使用LIKE子句,但我似乎做得不对,我觉得有一种更简单的方法来做这个lol def create_course_table(db, course_file): '''Courses Table should be ID,Course,Section,Name''' con = sqlite3.con

我试图返回一个查询,当返回与课程代码(例如“CSC”)相关联的所有课程时,将给我一个元组[('CSCA08H3F',),('CSCA20H3F',),('CSCA67H3F',)]…等。我知道我必须使用LIKE子句,但我似乎做得不对,我觉得有一种更简单的方法来做这个lol

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])

如有任何帮助或意见,将不胜感激。

请熟悉适当的

您已将列名放在“LIKE”之后。列名在“LIKE”之前

以下查询将从课程列以给定字符串开头的课程中的所有行返回课程列

SELECT Course FROM Courses WHERE Course LIKE ? || "%";

替代品?具有所需前缀。

因为所有课程列项目都是大写的,所以您可以使用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()函数来强制执行大小写。

部门的值是多少?它与课程前缀有什么关系?@CL说我有一个课程列表,比如('CSCA08H3F','ALL','HW214'),('CSCA08H3F','ALL','HW215')('ANTA01H3F','LEC01','AA112'),('ANTA01H3F','LEC01','SY110')('MATB24H3F','ALL','HW216'),('MATB24H3F','ALL','IC120'),('MATB24H3F','ALL','IC130')。。。。如果我为dept输入MAT,那么我会得到('MATB24H3F',('MATB24H3F'),('MATB24H3F')@Jemi愚蠢的问题,但语法'II'的确切含义是什么?也感谢你帮助我最小化我的工作。@Newbie The | |运算符连接字符串。我重新考虑了我当前的答案。IN()理论上应该是AND…而不是OR…如果存在类似的内容,则ANY()中类似a的内容可能会起作用。我可能会进一步研究它。发现没有合适的表达式或函数。怀疑当前行为是否可以更改。