Example #1
0
def query_process_file():
    correct = list()
    wrong = list()
    flag_open = 0
    error_string = ""
    global database_name, sql_query, sql_output, username, password, ip, db, natural_lang_query, query_type
    file_path = request.form['input_file']

    output_path = os.path.abspath(os.path.join(file_path,os.pardir))+"\output.txt"
    op_file = open(output_path, "w")

    db = database.Database(ip, username, password, database_name)
    db.connect()

    overall_detail = overall_details.OverallDetails(db)
    overall_detail.collect_details()

    count = 1
    try:
        open(file_path)
        flag_open = 1
    except FileNotFoundError:
        error_string = "File not found at the location specified."

    if flag_open == 1:
        with open(file_path) as fp:
            for line in fp:
                natural_lang_query = line.strip('\n')
                print("%d %s" % (count, natural_lang_query))

                try:
                    sql_query_details_obj = sql_query_details.SQLQueryDetails(db, overall_detail)

                    clauses = sql_query_details_obj.collect_query_details(natural_lang_query)

                    [sql_query, query_type] = clauses.create_query()
                    write_log_file()
                    print("\n-----------")
                    print("Final query: ", sql_query)
                    print("-----------\n")
                    op_file.write(sql_query + "\n")
                    correct.append(count)
                except Exception:
                    sql_query = "Query cannot be formed. Please check the input."
                    write_log_file()
                    print("\n-----------")
                    print("Final query: ", sql_query)
                    print("-----------\n")
                    op_file.write(sql_query + "\n")
                    wrong.append(count)
                count += 1

        op_file.close()
        output_path = output_path.replace('\\', '/')
        return render_template('loggedin.html', ip=ip, nlq=file_path, database=database_name, db=True,
                               username=username, result=result, generate=True, sql_query=sql_query, exec_flag=False,
                               opt_type2=True,output_path=output_path, error=False, correct=correct, wrong = wrong)
    return render_template('loggedin.html', ip=ip, nlq=file_path, database=database_name, db=True,
                           username=username, error_string=error_string, generate=False, sql_query=sql_query, exec_flag=False,
                           opt_type2=True, output_path=output_path, error=True, result=result)
Example #2
0
def query_process():
    error_flag = 0
    error_string = ""
    column_names = ""
    print("query process1:", request.form['query_button'])
    global database_name, sql_query, sql_output, username, password, ip, db, natural_lang_query, query_type
    db = database.Database(ip, username, password, database_name)
    db.connect()
    try:
        overall_detail = overall_details.OverallDetails(db)
        overall_detail.collect_details()
        sql_query_details_obj = sql_query_details.SQLQueryDetails(db, overall_detail)
        natural_lang_query = request.form['input']
        print("query process:", request.form['query_button'])
        if request.form['query_button'] == "gener_bt":
            clauses = sql_query_details_obj.collect_query_details(natural_lang_query)
            [sql_query, query_type] = clauses.create_query()
            if query_type == "S":
                try:
                    db.execute_query(sql_query)
                except Exception:
                    error_flag = 1
                    error_string = "System failed to generate SQL query for the given input."

            if error_flag == 0:
                write_log_file()
                return render_template('loggedin.html', ip=ip,nlq=natural_lang_query,database=database_name,db=True,
                    username=username,result=result, generate=True, sql_query=sql_query,opt_type=True,error=False)
            return render_template('loggedin.html', ip=ip, nlq=natural_lang_query, database=database_name, db=True,
                    username=username, result=result, generate=False, sql_query=sql_query,
                                   opt_type=True, error=True, error_string=error_string)

        elif request.form['query_button'] == "exec_bt":
            # natural_lang_query=request.form['nlq']
            if query_type == "S":
                sql_output = db.execute_query(sql_query, "1")
                query_result=sql_output[0]
                column_names=sql_output[1]
                print(sql_output[0], sql_output[1])
            else:
                sql_output = db.execute_query(sql_query)
                query_result=sql_output

            return render_template('loggedin.html', ip=ip,nlq=natural_lang_query,database=database_name,db=True,
                                   username=username, result=result,generate=True, sql_query=sql_query, exec_flag=True,
                                   query_result=query_result,column_names=column_names,opt_type=True)
    except Exception:
        error_string = "System failed to generate SQL query for the given input."
        return render_template('loggedin.html', ip=ip, nlq=natural_lang_query, database=database_name, db=True,
                               username=username, result=result, generate=False, sql_query=sql_query,
                               opt_type=True, error=True, error_string=error_string)
Example #3
0
op_file = open("output.txt", "w")

db = database.Database("localhost", "nandans16", "1234", "bank")
db.connect()

overall_details = overall_details.OverallDetails(db)
overall_details.collect_details()

count = 1
with open('ip1.txt') as fp:
    for line in fp:
        natural_lang_query = line.strip('\n')
        print("%d %s" % (count, natural_lang_query))

        sql_query_details_obj = sql_query_details.SQLQueryDetails(
            db, overall_details)

        clauses = sql_query_details_obj.collect_query_details(
            natural_lang_query)

        [query, type_query] = clauses.create_query()
        # use the type variable wherever you want
        print("\n-----------")
        print("Final query: ", query)
        print("-----------\n")
        op_file.write(query + "\n")
        count += 1

op_file.close()

# compare()