简体   繁体   English

使用python 2.7函数查询sqlite3 DB

[英]using python 2.7 function to query sqlite3 DB

I'm making a program where people, by giving their info (education, field of study, higher degree achieved etc.) are given a score. 我正在制定一个程序,通过给人们信息(教育,学习领域,取得的更高学位等)给他们打分。 I'm using sqlite3 and Python 2.7 我正在使用sqlite3和Python 2.7

To do so, I have a DB in which each city and province is listed with the percentage of people education's highest degree. 为此,我有一个数据库,其中列出了每个城市和省份中受教育程度最高的人的百分比。 I have made one table for each province: 我为每个省做了一张桌子:

DB: D B:

table: province_1 表格:province_1

cities| no certificate| high school diploma| Apprenticeship| bachelor| etc..
city1 :     5         |        15          |     8         |   20    | ... 
city2 :     15        |        12          |     35        |   10    |  ...  
city3 :     1         |        35          |     3         |   8     |  ...  

And then I have my function. 然后我有我的职能。 In it, what I'd like to do is calculate the percentage of people with a lower and equal education / people with higher education. 在其中,我想做的是计算受教育程度较低和平等的人/受教育程度较高的人的百分比。

Example: if user1 is from city2 and has an apprenticeship degree it would result as 62 / 38 = 1.63 示例:如果user1来自city2并具有学徒学位,则结果为62/38 = 1.63

This is what I have got until now, but as you can see, it is a massive flop: 这是我到目前为止所获得的,但是如您所见,这是一次巨大的失败:

def edu_score(education, fos, province, city):
    edu_lvl = ['No_certificate', 'high_school_diploma', 'Apprenticeship', 'CEGEP', 'Bachelor Diploma', 'Master Diploma', 'Doctorate']
    score = 0 
    crsr.execute("SELECT (SUM([edu_lvl.index(ed):: -1]) / (SUM ([edu_lvl.index(ed)::])) FROM province_1 WHERE cities = city") 
    score = crsr.fetchone()         
    print score

How do I calculate it? 如何计算? and how do I do so that the user's input is not taken as a string, which returns that same string value in the db? 以及如何使用户输入不作为字符串,而该字符串在db中返回相同的字符串值?

Thank you so much, and I hope I was clear enough. 非常感谢,我希望我足够清楚。

First you should merge your tables into one, and add a column called provinces. 首先,您应该将表合并为一个表,并添加一列名为“省”的列。 There is a strong reason for that. 这有很强的理由。 You do not want any user input field being called as a table name since you can't easily tokenize it (avoid sql injection). 您不希望将任何用户输入字段称为表名,因为您不容易将其标记化(避免sql注入)。

Second you are merging python code with a SQL statement, so that will never work. 其次,您正在将python代码与SQL语句合并,因此将永远无法工作。

What you need is something like this: 您需要的是这样的:

def edu_score(education, fos, province, city):
    score = 0
    crsr.execute('SELECT (SUM("no certificate"+"high school diploma"+"Apprenticeship") / (SUM ("Bachelor Diploma"+"Master Diploma"+"Doctorate")) FROM provinces WHERE cities = ? AND province = ?', city, province)
    score = crsr.fetchone()
    return score

UPDATED: To handle the scoring you should do it in python, not SQL. 更新:要处理评分,您应该使用python而不是SQL进行。

def edu_score(education, fos, province, city):
    eds = ['No_certificate', 'high_school_diploma', 'Apprenticeship', 'CEGEP', 'Bachelor Diploma', 'Master Diploma', 'Doctorate']
    ed_index = eds.index(education)
    crsr.execute('SELECT SUM("no certificate"), SUM("high school diploma"), SUM("Apprenticeship"), SUM ("Bachelor Diploma"), SUM("Master Diploma"), SUM("Doctorate") FROM provinces WHERE cities = ? AND province = ?',
        city, province)
    scores = crsr.fetchone()
    score = sum(scores[:ed_index])/sum(scores[ed_index:])
    return score

[MY ANSWER] [我的答案]

So the first problem I had was that I was adding another string into the SELECT statement through edu_score(education), which only returned the string found in the DB. 因此,我遇到的第一个问题是我正在通过edu_score(education)将另一个字符串添加到SELECT语句中,该字符串仅返回在数据库中找到的字符串。 The other problem was that it wasn't summing just the necessary rows, depending on the user's highest level of education. 另一个问题是,它并不能仅根据用户的最高学历来累加必要的行。

The answer was this: segment the SELECT string and do not use the SUM, as it is summing column and not rows. 答案是这样的:将SELECT字符串分段并且不使用SUM,因为它是对列而不是行求和。 You just need to make a string that encompasses all the data you want to add ie : SELECT (A + B + C) FROM table WHERE ?, [city,] . 您只需要创建一个包含要添加的所有数据的字符串,即:从表WHERE?,[city,]中选择(A + B + C)。

phrase = ''

I made this variable because within the function it would return 'None' 我做了这个变量,因为在函数中它将返回“ None”

def city_result(base):   
    global phrase 
    edu_lvl = ['No_certificate', 'high_school_diploma', 'Apprenticeship', 'CEGEP', 'Bachelor', 'Master', 'Doctorate']
    for i in  edu_lvl[edu_lvl.index(base):: -1] :
        if edu_lvl.index(i) == 0:
            phrase = phrase + i
        else: 
            phrase = i + " + " + phrase

city_result is what will be put in the SELECT statement to be counted city_result将被放入SELECT语句中以进行计数

def edu_score(education, fos, province, city):
    global phrase
    score = 0 
    city_result(education)
    crsr.execute("SELECT (" + phrase + ") FROM ? WHERE cities = ?", [province, city,]) 
    score = crsr.fetchone()
    score = score / (100 - score)

The only thing to remember is that SUM is for column, so if you just want certain row, just put the data you want to add within the SELECT statement, and be careful with putting string in the SELECT string (because it returns that same string). 唯一要记住的是SUM是用于列的,因此,如果您只想要特定的行,只需将要添加的数据放入SELECT语句中,并小心将字符串放入SELECT字符串中(因为它会返回相同的字符串) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM