Home  >  Article  >  Backend Development  >  How to implement the function of viewing and exporting answer records in online answering questions

How to implement the function of viewing and exporting answer records in online answering questions

王林
王林Original
2023-09-24 12:12:361206browse

How to implement the function of viewing and exporting answer records in online answering questions

The function of viewing and exporting answer records in online answering can be achieved with the help of database and programming technology. Below are the steps and code example to implement this feature.

Step 1: Design database tables
Create a question record table and an answer record table in the database. The question record table is used to store question information, including question number, question content, correct answer, etc. The answer record table is used to store the user's answer records, including user ID, question number, user answer and answer time, etc.

The following is a sample code for the question record table:

CREATE TABLE question (
    id INT PRIMARY KEY,
    content TEXT,
    correct_answer TEXT
);

The following is a sample code for the answer record table:

CREATE TABLE answer (
    id INT PRIMARY KEY,
    user_id INT,
    question_id INT,
    user_answer TEXT,
    answer_time TIMESTAMP
);

Step 2: Enter the question
Provided in the program In the question entry interface, users can enter the question content and correct answers, and store the data in the question record table. The following is a sample code:

def add_question(content, correct_answer):
    # 连接数据库
    conn = mysql.connector.connect(user='username', password='password', host='localhost', database='db_name')
    cursor = conn.cursor()

    # 插入数据
    sql = "INSERT INTO question (content, correct_answer) VALUES (%s, %s)"
    val = (content, correct_answer)
    cursor.execute(sql, val)

    # 提交并关闭连接
    conn.commit()
    cursor.close()
    conn.close()

Step 3: View answer records
The answer record viewing interface is provided in the program. The user can enter the user ID and then query the user's answer record. The following is a sample code:

def view_answer(user_id):
    # 连接数据库
    conn = mysql.connector.connect(user='username', password='password', host='localhost', database='db_name')
    cursor = conn.cursor()

    # 查询数据
    sql = "SELECT * FROM answer WHERE user_id = %s"
    val = (user_id,)
    cursor.execute(sql, val)
    result = cursor.fetchall()

    # 打印结果
    for row in result:
        print("Question ID:", row[2])
        print("User Answer:", row[3])
        print("Answer Time:", row[4])

    # 关闭连接
    cursor.close()
    conn.close()

Step 4: Export answer records
The answer record export function is provided in the program. Users can choose to export all answer records or the answer records of specified users, and export the data to CSV document. The following is sample code:

import csv

def export_answer(user_id=None):
    # 连接数据库
    conn = mysql.connector.connect(user='username', password='password', host='localhost', database='db_name')
    cursor = conn.cursor()

    # 查询数据
    if user_id:
        sql = "SELECT * FROM answer WHERE user_id = %s"
        val = (user_id,)
        cursor.execute(sql, val)
    else:
        sql = "SELECT * FROM answer"
        cursor.execute(sql)
    result = cursor.fetchall()

    # 导出为CSV文件
    with open('answer.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow(["User ID", "Question ID", "User Answer", "Answer Time"])
        writer.writerows(result)

    # 关闭连接
    cursor.close()
    conn.close()

The above are some suggestions and code examples for implementing the answer record viewing and export functions in online answering. Depending on specific needs and development environments, further optimization and modifications can be made.

The above is the detailed content of How to implement the function of viewing and exporting answer records in online answering questions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn