Beginners Python Flask Development API Interface __python

Source: Internet
Author: User

using Python to access the MySQL database under Windows,

data access is provided externally using the Flask Development API interface.

1, view the Python version python-v
Python 2.7.12:: Anaconda 4.1.1 (64-bit)

2, install driver Mysql-python, download the corresponding version of Python

https://pypi.python.org/pypi/MySQL-python/

3. Check whether the MySQLdb module can be imported normally
Python
Import MySQLdb
Importerror:dll load failed:%1 is not a valid WIN32 application.
Conclusion: The driver version does not correspond to Python, it must be 32 bits corresponding to 32 bits and 64 bits corresponding to 64 bits.
Re-download the non-installed version: Mysql-python-1.2.5.zip
Unpack, enter mysql-python-1.2.5 directory:
Python setup.py Install
Error: Error:microsoft Visual C + + 9.0 is required (unable to find Vcvarsall.bat).
Continue downloading: Microsoft Visual C + + Compiler for Python 2.7
http://www.microsoft.com/en-us/download/details.aspx?id=44266
After you install this compiler, continue with the installation:
Python setup.py Install
Continue the error:
_MYSQL.C: Fatal error C1083:cannot open include file: ' Config-win.h ': No such file or directory
Cause: The associated version of the MySQL connector is inconsistent.
Depressed, why also need MySQL Connector.
Too toss, search a bit, unofficial online, there are 64-bit Mysql-python Download:
http://www.codegood.com/archives/129
After installation, test again:
Python
Import MySQLdb
OK, okay.
Colleagues recommend using Conda to manage the Python development environment, as described below:
Anaconda provides a management tool Conda that can be seen as Pip + virtualenv + PVM (Python Version Manager) + Some of the necessary underlying libraries, a more complete and larger integrated management tool. If you only want to use Conda, you can download the simplified version of Miniconda (Https://conda.io/docs/install/quick.html#linux-miniconda-install), you need anaconda Can install the full version, a total of about 400M, with Aliyun server from the official website to download about 6 hours, but fortunately Tsinghua University tuna Mirror Source: https://mirrors.tuna.tsinghua.edu.cn/anaconda/

4. Installation Flask
Pip Install flask
After installation, test again:
Python
Import Flask
OK, okay.

5, Code

sql.py

#-*-Coding:utf-8-*-
'''
Encapsulation class
'''
Import MySQLdb

Class MyTest (object):
'''
MyTest

' Def __init__ (self, HOST, USER, PASSWD, DB, PORT):
Self.conn = MySQLdb.connect (
Host=host, User=user, passwd=passwd, Db=db, Port=port)
Self.cur = Self.conn.cursor ()

Def getdata (self, SQL):
'''
GetData
'''
result = None
Try
Count = Self.cur.execute (SQL)
info = Self.cur.fetchmany (count)
result = []
For row in info:
Result.append (Row)
return result
Except Mysqldb.error, E:
Return "error:%s"% str (e)
Finally
# The code snippet that can change the scope of a variable is Def, class, Lamda
If Self.cur:
Self.cur.close ()
Self.conn.commit ()
Self.conn.close ()

app.py
# Coding:utf-8
'''
Access the MySQL database using Python under Windows and provide data access using the Flask Development API Interface
'''
From SQL Import MyTest
From flask import flask, jsonify
APP = Flask (__name__)

@APP. Route ('/test ')
def test ():
'''
Routing test
'''
Host, user, passwd = ' 127.0.0.1 ', ' * * * *, ' * * * *
dbname, port = ' Dbdemo ', 800
Instance = MyTest (host, user, passwd, dbname, Port)
result = Instance.getdata ("SELECT * from MYTB limit 0,10")
If not None:
return jsonify (Result)
Else
Return "No Data"

if __name__ = = "__main__":
App.run (host= ' 0.0.0.0 ', port=7777, Debug=true)

TestHttp://127.0.0.1:7777/test
OK.

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.