python - 使用通过python连接的表单在数据库中插入数据

标签 python mysql forms

我在html文档中创建了一个表单,从用户那里获取数据,并在mysql中创建了一个数据库。我使用python将数据库连接到表单,以便用户输入填充数据库。但它不起作用。在提交值之后,我得到了如下的代码,在浏览器页面上打印出来,并且没有值从表单插入到数据库中。你能指出代码中的错误/任何修改吗?

import cgitb;cgitb.enable()
import MySQLdb
import cgi
import sys

form=cgi.FieldStorage()
Text0=form.getValue('Text0')
Text1=form.getValue('Text1')
Text2=form.getValue('Text2')
Text3=form.getValue('Text3')
Text4=form.getValue('Text4')

# Open database connection
dbc = MySQLdb.connect("127.0.0.1","root","sam143","project" )

# prepare a cursor object using cursor() method
cursor = dbc.cursor()

# Prepare SQL query to INSERT a record into the database.

sql = """INSERT INTO entries(slno, \
       dat1, dat2, dat3, dat4, dat5) \
       VALUES ('%d', '%d', '%d', '%d', '%d', '%d' )""" % \
       (l[0], l[1], l[2], l[3], l[4])

try:
   # Execute the SQL command
   cursor.execute(sql)
   sql = "SELECT * FROM entries"
   cursor.execute(sql)
   print cursor.fetchall()
   # Commit your changes in the database
   dbc.commit()

except:
   # Rollback in case there is any error
   dbc.rollback()

# disconnect from server
dbc.close()

最佳答案

>>> form=cgi.FieldStorage()
>>> dir(form)
['FieldStorageClass', '_FieldStorage__write', '__contains__', '__doc__', '__getattr__', '__getitem__', '__init__', '__iter__', '__len__', '__module__', '__nonzero__', '__repr__', 'bufsize', 'disposition', 'disposition_options', 'done', 'fi
le', 'filename', 'fp', 'getfirst', 'getlist', 'getvalue', 'has_key', 'headers', 'innerboundary', 'keep_blank_values', 'keys', 'length', 'list', 'make_file', 'name', 'outerboundary', 'qs_on_post', 'read_binary', 'read_lines', 'read_lines_to
_eof', 'read_lines_to_outerboundary', 'read_multi', 'read_single', 'read_urlencoded', 'skip_lines', 'strict_parsing', 'type', 'type_options']
>>> 

form.getvalue不是form.getvalue

关于python - 使用通过python连接的表单在数据库中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118635/

相关文章:

python - 删除 Django admin 中的默认删除操作

mysql - 从库存中获取可用设备的 SQL 语句

php - 从多个表中按计数排序的最佳方法是什么?

java - 全局形式:errors field sequence

RTL 形式的 HTML 标签

python - Ipython 具有与 python 不同的 sys.path 并且不使用 PYTHONPATH?

python - 查找与给定向量相似的所有向量的快速方法

html - HTML 中的清除表单按钮……我们真的需要这个吗?

python - 无法在 Windows 10 上使用 selenium chromedriver 打开网络浏览器

mysql - 如何从一台 MySQL 服务器连接到另一台 MySQL 服务器?