登录
首页 >  数据库 >  MySQL

如何编写Python CGI程序与MySQL交互?

来源:tutorialspoint

时间:2023-09-06 11:20:14 332浏览 收藏

golang学习网今天将给大家带来《如何编写Python CGI程序与MySQL交互?》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习数据库或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

假设您想使用Python CGi脚本登录您的帐户,以下是详细信息

login.html


   
      
email: password:

登录.py

#!C:\Python27\python.exe
import MySQLdb
import cgi
import Cookie

# Open database connection
db = MySQLdb.connect("localhost","root","","student" )

# prepare a cursor object using cursor() method
cursor = db.cursor()
data=cgi.FieldStorage()
a=data.getvalue('e1')
b=data.getvalue('p1')

# Prepare SQL query to fetch a record into the database.
sql = "select id,email,password from user where email='"+a+"' AND password='"+b+"'"
try:
# Execute the SQL command
if(cursor.execute(sql)): 
   # Commit your changes in the database 
   db.commit()
   c=Cookie.SimpleCookie()

   # assign a value
   c['mou']=a

   # set the xpires time
   c['mou']['expires']=24*60*60

   # print the header, starting with the cookie
   print c
   print("Content-type: text/html")
   print('''
      
         Hello Word - First CGI Program
      
      
         

successfully login

''') else: # Commit your changes in the database db.commit() print("Content-type: text/html") print("") print("") print("

fail

") print("") print("") except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()

到这里,我们也就讲完了《如何编写Python CGI程序与MySQL交互?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:tutorialspoint 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>