Simple Method to connect the Bottle framework of MySQL and Python, pythonbottle

Source: Internet
Author: User

Simple Method to connect the Bottle framework of MySQL and Python, pythonbottle

Python has many connection plug-ins for mySQL, and some plug-ins are specially developed under Bottle: For details about how to use bottle-mysql, see the official website. In total, I feel that there are too many restrictions on its usage, which is inconvenient to use, of course, the most suitable is the general official driver provided by the mySQL official website to Python. It is very easy to use: mysql-connector the specific operations are as follows:
 

#-*-Coding: UTF-8 -*-#! /Usr/bin/python # filename: login_admin.py # codedtime: 2014-9-7 11: 26: 11 import bottleimport mysql. connector # import mysql Database connector def check_userinfo (): a_list = [] # create an empty list username = bottle. request. GET. get ('loginname ',''). strip () # Username password = bottle. request. GET. get ('Password ',''). strip () # password if username is not None or password is not None: try: # connect to database conn = mysql. connector. connect (user = 'root', pas Sword = '000000', database = 'myblog') cursor = conn. cursor () # create a data cursor # execute query = ("SELECT username, password FROM mb_users" WHERE username = % s and password = % s ") cursor.exe cute (query, (username, password) a_list = cursor. fetchall () # fetchone gets a tuples # count = int (cursor. rowcount) # Get the number of tuples return a_list using t mysql. connector. error as err: print ("Something went wrong :{}". format (err) exit () finall Y: conn. commit () # submit and modify cursor. close () # close database conn. close () else: return a_listdef login_admin (): if bottle. request. GET. get ('bs-submit ',''). strip (): # click the logon button a_list = check_userinfo () if a_list: a_name = a_list [0] [0] # Get the user name return bottle. template ('templates/index_user.tpl ', username = a_name) else: return bottle. template ('templates/login_admin.tpl ', action ='/login_admin ', error_info =' enter the correct user name or password! ') Else: return bottle. template ('templates/login_admin.tpl', action = '', error_info = '')


The above is a simple usage of MySQL in Botlle,

By the way: Install and manage mySQL. We recommend that you use XAMPP and XAMPP to integrate Apache, MySQL, PHP, Tomcat, and other tools, you do not need to install and configure them one by one, and the management is convenient. The Default User of MySQL installed in XAMPP is: the root password is blank.

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.