Simple way to connect MySQL with Python's bottle framework

Source: Internet
Author: User
Python about MySQL connection plug-ins, bottle is also someone specifically developed plug-ins: Bottle-mysql specific use method see official, total feel its usage limit too much, its use inconvenient, the most suitable of course is, The common official driver of the MySQL website for Python is handy: Mysql-connector the following:

#-*-Coding:utf-8-*-#!/usr/bin/python# filename:login_admin.py# codedtime:2014-9-7 11:26:11import bottleimport MySQL . Connector # import MySQL database connector def check_userinfo (): A_list = [] # Create an empty list username = bottle.request.GET.get (' LoginName ', ' ). Strip () # user name password = bottle.request.GET.get (' password ', '). Strip () # password if username is not None or password is no         T none:try: # CONNECT Database conn = Mysql.connector.connect (user= ' root ', password= ' 123456 ', database= ' MyBlog ') cursor = Conn.cursor () # Create data cursor # Execute query = ("Select username, password from mb_users" "W Here username=%s and password=%s ") Cursor.execute (query, (username, password)) A_list = Cursor.fetchall () # FETC Hone get a tuple #count = Int (cursor.rowcount) # Gets the number of tuples return a_list except Mysql.connector.Error as ERR:PR Int ("Something went wrong: {}". Format (Err)) exit () Finally:conn.commit () # Commit Modify Cursor.close () #  Close Database Conn.close ()Else:return a_listdef login_admin (): If Bottle.request.GET.get (' Bs-submit ', '). Strip (): #点击登录按钮 a_list = Check_use Rinfo () If a_list:a_name = a_list[0][0] # Get user name return Bottle.template (' Templates/index_user.tpl ', username = A_name) Else:return bottle.template (' Templates/login_admin.tpl ', action= '/login_admin ', Error_inf O= ' Please enter the correct username or password! ') Else:return bottle.template (' Templates/login_admin.tpl ', action= ', error_info= ')


These are the simple uses of MySQL in Botlle,

By the way: installation and management of MySQL, it is recommended to install the use of XAMPP,XAMPP integrated Apache, MySQL, PHP, Tomcat and other tools, one-time solution installation, without their own cumbersome installation and configuration, and management is also very convenient. XAMPP installed MySQL Default user is: The root password is empty.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.