【问题标题】:I created a table in html using python and I'd like to add those input table values into sql database我使用 python 在 html 中创建了一个表,我想将这些输入表值添加到 sql 数据库中
【发布时间】:2016-11-17 04:39:03
【问题描述】:

我正在使用 python 访问 html 来创建一个表,我想要做的是当用户输入值并单击提交按钮时,所有值都将保存在“students”表中。我不确定如何使用提交按钮来保存所有值(姓氏、名字、中期成绩、最终成绩、第一硬件、第二硬件、第三硬件)。 我对 pymysql 不熟悉,我浏览了许多网站以找到解决此问题的方法。我会接受任何建议。

import cgitb
import pymysql
cgitb.enable()
print("Content-type: text/html\n")

print('<form method="" action="">',
          '<fieldset>',
              '<legend>Personal information:</legend>',
              'Last name:'
              '<input type="text" name="lastname" value="lastname"><br>',
              'First name:'
              '<input type="text" name="firstname" value="firstname"><br>',
              'MidtermGrade:'
              '<input type="text" name="midtermgrade" value="midtermgrade"><br>',
              'FinalGrade:'
              '<input type="text" name="finalgrade" value="finalgrade"><br>',
              'FirstHW:'
              '<input type="text" name="firsthw" value="firsthw"><br>',
              'SecondHW:'
              '<input type="text" name="secondhw" value="secondhw"><br>',
              'ThirdHW:'
              '<input type="text" name="thirdhw" value="thirdhw"><br>',
              '<br><br>'
              '<input type="submit" value="Submit">',
          '</fieldset>'
     </form>)

与 RDS 实例上的数据库建立连接。替换为您自己的服务器和用户凭据。

conn = pymysql.connect(host=localhost, port=3306, user='username', passwd='password', db='student')
cur = conn.cursor()

一些SQL返回一些数据

cur.execute("SELECT * from students")

化妆品的空白处

遍历结果集,一次打印一行

关闭连接

cur.close()
conn.close()

【问题讨论】:

    标签: python html mysql sql-server


    【解决方案1】:

    我认为要获得可用的表单,您需要的不仅仅是打印语句。
    您已经制作了一个 HTML 表单,但是将用户数据从它发送到服务器以便您可以获取值需要一个 Web 服务器。

    我建议您查看 Flask(或其他)Web 框架:http://flask.pocoo.org/
    它很容易上手,您可以将表单数据作为 Python 函数中的变量获取;然后您可以调用您的 SQL 函数将它们保存到数据库中。
    让我们知道你的进展!

    【讨论】:

      【解决方案2】:

      这种方法不起作用,因为您尝试在服务器端构建页面,然后期望在客户端输入,然后在服务器端进行更多工作来构建页面。但是(我自己也不是这方面的专家,所以我会很感激更正),一旦您构建页面服务器端并将其发送到客户端,您需要在客户端上使用某种形式的 JavaScript 来获取更多信息从服务器(AJAX 是一种常见的方式)。

      如果您对 submit 按钮将用户带到不同的页面以查看结果表示满意,则不需要 Javascript。我会像这样构建你想要做的事情(请记住,我现在无法测试这个,所以你必须调试它):

      在文件form.html

      <!-- TODO: put the rest of the HTML here -->
      <form method="post" action="get_results.py">
          <fieldset>
              <legend>Personal information:</legend>
              Last name:
              <input type="text" name="lastname" value="lastname"><br>
              First name:
              <input type="text" name="firstname" value="firstname"><br>
              MidtermGrade:
              <input type="text" name="midtermgrade" value="midtermgrade"><br>
              FinalGrade:
              <input type="text" name="finalgrade" value="finalgrade"><br>
              FirstHW:
              <input type="text" name="firsthw" value="firsthw"><br>
              SecondHW:
              <input type="text" name="secondhw" value="secondhw"><br>
              ThirdHW:
              <input type="text" name="thirdhw" value="thirdhw"><br>
              <br><br>
              <input type="submit" value="Submit">
          </fieldset>
      </form>
      

      请注意,我在表单中填写了actionmethod 属性。这是纯 HTML。当表单被提交时,用户将被重定向到get_results.py,它必须读取表单附加到页面请求的POST信息。

      类似:

      在文件get_results.html

      import cgi
      import cgitb
      import pymysql
      
      cgitb.enable()
      
      # This *should* get the fields from the previous form.
      # I can't test it now though...
      form = cgi.FieldStorage()
      
      # this part is almost straight from https://github.com/PyMySQL/PyMySQL
      import pymysql.cursors
      
      # Connect to the database
      connection = pymysql.connect(host='localhost',
                                  user='user',
                                  password='passwd',
                                  db='db',
                                  charset='utf8mb4',
                                  cursorclass=pymysql.cursors.DictCursor)
      
      try:
          with connection.cursor() as cursor:
              student_sql = 'INSERT INTO `students` (lastname, firstname) VALUES (%s, %s)'
              cursor.execute(student_sql,(form['lastname'], form['firstname']) )
              # replacing some filed names with '...'. Fill them in
              grade_sql = 'INSERT INTO `grades` (midtermgrade, finalgrade, ...) VALUES (%d, %d, %d, %d)'
              cursor.execute(grade_sql, form['midtermgrade'], form['finalgrade'], ...)
          connection.commit()
          with connection.cursor() as cursor:
              # Read a single record
              sql = "SELECT * FROM students"
              cursor.execute(sql))
              for result in cursor.fetchall():
                  print(result, '<br>')
      finally:
          connection.close()
      

      这可能是最简单的方法。然而,重要的是要知道,这种方法不会扩展到更大的项目。 首先,您需要:

      • 一种模板语言,因此您可以将原始 HTML 和插入其中的代码分开
      • 网址操作(因此类似 mysite.com//grades 的内容会将用户直接带到他们的成绩)
      • 用户身份验证(我们不希望学生查看其他学生的成绩吗?)
      • 一个 ORM,而不是直接处理您的数据库(我没有在这方面放太多库存,但有些人发誓)

      如果您想制作比这更大的东西,请查看适用于 Python 的 FlaskDjango 之类的框架,或适用于其他语言的任意数量的其他解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-16
        • 1970-01-01
        • 2013-11-29
        • 2023-01-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多