简体   繁体   English

使用用户输入创建python sparql查询

[英]python sparql query creating with user input

I am trying to build sparql query using python and take some data from user我正在尝试使用 python 构建 sparql 查询并从用户那里获取一些数据

this is my code :这是我的代码:

@app.route('/api/v1/getCourseByCode', methods=['GET'])
def api_getCourseByCode():
    if 'courseId' in request.args:
        courseId = request.args['courseId']
    else:
        return "Error: No course id provided. Please specify an course id."

    onto = get_ontology("https://website/lumiere8.owl")
    onto.load()
    graph = onto.world.as_rdflib_graph()

    query = """PREFIX lumiere: <http://www.smacrs.com/lumiere.owl#>
            SELECT ?individual
            WHERE { ?individual a lumiere:Course ;
                    lumiere:Code ?propertyValue .
            FILTER(STR(?propertyValue) = {fname})}""".format(fname=courseId)

    print("---------------------------------------")
    print(query)

    course = list(graph.query_owlready(query))
    print(course)
    return course

and I got the following error :我收到以下错误:

在此处输入图片说明

The query is working like this :查询是这样工作的:

course = list(graph.query_owlready("""PREFIX lumiere: <http://www.smacrs.com/lumiere.owl#>
    SELECT ?individual
    WHERE { ?individual a lumiere:Course ;
                    lumiere:Code ?propertyValue .
    FILTER(STR(?propertyValue) = "CS101")}
    """))

but the problem is that I want to take user input from the api method但问题是我想从 api 方法中获取用户输入

This is what works with me using %s in the query这是我使用的作品%S查询

course=list(graph.query_owlready("""PREFIX lumiere: <http://www.smacrs.com/lumiere.owl#>
    SELECT ?individual
    WHERE { ?individual a lumiere:Course ;
                    lumiere:Code ?propertyValue .
    FILTER(STR(?propertyValue) = "%s")}
    """ %courseId))

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

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