Tag: Option State Directory Enter otherwise hat bin ORM Store
Mysql
Http://www.cnblogs.com/zhangzhu/archive/2013/07/04/3172486.html
1. Connect to MySQL on this machine.
First open the DOS window, then enter the directory Mysql\bin, and then type the command Mysql-u root-p, enter after the prompt you to lose the password. Note that the user name can have a space or no space, but before the password must have no space, or let you re-enter the password.
Example 1: Establishing a database named XHKDB
mysql> CREATE DATABASE xhkdb;
Py-mysql
Http://www.runoob.com/python/python-mysql.html
Python operation MySQL Database
The Python standard database interface for Python Db-api,python DB-API provides developers with a database application programming interface.
The Python database interface supports a very great number of databases, and you can choose the database that works for your project:
Python DB-API Usage Process:
- Introduce API modules.
- Gets the connection to the database.
- Executes SQL statements and stored procedures.
- Close the database connection.
#!/usr/bin/python#-*-Coding:utf-8-*-Import MySQLdb# Open Database connectionDb= MySQLdb.Connect("LocalHost", "TestUser", "Test123", "TESTDB",CharSet=' UTF8 ' )# get an action cursor using the cursor () methodCursor= Db. () # Execute SQL statement using the Execute method cursorexecute ( "SELECT VERSION ()" ) # use the Fetchone () method to get a data data = Cursor. () print " Database version:%s " % Data# close the connection db. ()
Flask SQLAlchemy ORM
http://www.sqlalchemy.org/
The Python SQL Toolkit and Object relational Mapper
SQLAlchemy is the Python SQL Toolkit and Object relational Mapper, gives application developers the full power and fle Xibility of SQL.
It provides a full suite of the well known enterprise-level persistence patterns, designed for efficient and high-performing D Atabase access, adapted into a simple and pythonic domain language.
SQLALCHEMY ' S philosophy
SQL databases behave less as object collections the more size and performance start to matter; Object collections behave less like tables and rows The more abstraction starts to matter. SQLAlchemy aims to accommodate both of these principles.
SQLAlchemy considers the database to is a relational algebra engine, not just a collection of tables. Rows can selected from not only tables but also joins and other SELECT statements; Any of these units can is composed into a larger structure. SQLAlchemy ' s expression language builds on the concept from its core.
SQLAlchemy is more famous for its object-relational mapper (ORM), a optional component that provides the data mapper Pattern, where classes can is mapped to the database in open ended, multiple ways-allowing the object model and DAT Abase schema to develop in a cleanly decoupled by the beginning.
SQLAlchemy ' s overall approach to these problems are entirely different from the most other sql/orm tools, rooted in a So-called complimentarity- oriented approach; Instead of hiding away SQL and object relational details behind a wall of automation, all processes is fully exposed< /c1> within a series of composable, transparent tools. The library takes on the job of automating redundant the and the developer remains in control of how the database is O Rganized and how SQL is constructed.
The main goal of SQLAlchemy is-to-change the the-the-think about databases and sql!
Read some key features of SQLAlchemy, as well as what people is saying about SQLAlchemy.
Example
Http://docs.jinkan.org/docs/flask-sqlalchemy/quickstart.html
FromFlaskImportFlaskFromFlask.ext.sqlalchemyImportSQLAlchemyApp=Flask(__name__)App.Config[' Sqlalchemy_database_uri ']=' Sqlite:////tmp/test.db 'Db=SQLAlchemy(App)ClassUser(Db.Model):Id=Db.Column(Db.Integer,Primary_key=True)Username=Db.Column(Db.String(80),Unique=True)Email=Db.Column(Db.String(120),unique=true) def __init__ (selfusername email): self. Username = username self. Email = email def __repr__ (self): return ' <user %r > ' % self. Username
DBdb. Create_all()
Bang, your database is ready. Now to create some users:
User User ('admin '[email protected] ')user(' guest '[email Protected] ')
But they are not yet in the database, so let's make sure they are added:
DB. Session. Add(admin)db. Session. Add(guest)db. Session. Commit()
Accessing the contents of a database is a breeze:
User. Query. All()[<user u ' admin ';, <user u ' guest ';]User. Query. Filter_by(username=' admin '). First()<user u ' admin ' >
Reference:
Http://www.pythondoc.com/flask-sqlalchemy/config.html
Https://stackoverflow.com/questions/33738467/how-do-i-know-if-i-can-disable-sqlalchemy-track-modifications
Python DB operation