ORM exemplary operation of the database

The following example, copy directly to the sublime, save as ****. Py file, then run it!

 

Note: The ORM generate database did not write within the file, enter the command-line tool in python environment:

from ****** import #### (from test_mysql_orm import News)

from ****** import #### (from test_mysql_orm import engine)

News.metadata.create_all(engine)

Three lines of code above may generate a data table; Note: generating a database using the above operations is a prerequisite navicat;

 

 

'' 'Mysql database operations using SQLalchemy' ''
# SQLalchemy is a very common use of ORM tools, SQLalchemy mysql database itself can not directly operate, but the corresponding database API provided by this plugin MySQLdb to operate the database;
#SQLalchemy as ORM representatives can operate almost all of the database, the user need not be familiar with database statements, write SQL statements, but to generate and operation and maintenance database via the ORM API;
# if you want to change the database, it is also very simple, just change SQLalchemy of database set up just fine, do not make any changes to the program;


# import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey,DateTime,Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker


engine = create_engine('mysql://hiadmin:[email protected]:3306/newsdata?charset=utf8')
Base = declarative_base()
Session = sessionmaker(bind=engine)

class News(Base):
__tablename__='newsdata'
id = Column(Integer, primary_key=True)
title = Column(String(200),nullable=False)
content = Column(String(2000),nullable=False)
types = Column(String(10),nullable=False)
image = Column(String(300),)
author = Column(String(20),)
view_count = Column(String(20),)
created_at = Column(DateTime)
is_valid = Column(Boolean)

'''from test_mysql_orm import News'''
'''from test_mysql_orm import engine'''
'''News.metadata.create_all(engine)'''

class OrmTest(object):
"""docstring for OrmTest"""
def __init__(self):
self.session = Session()

def add_one(self):
new_obj = News(
title = '标题',
content = '内容',
types = '百家'
)
self.session.add(new_obj)
self.session.commit()
return new_obj

def add_all(self):
self.session.add_all([
News(
title = '标题0',
content = '内容0',
types = '百家0',
),
News(
title = '标题1',
content = '内容1',
types = '百家1',
),
News(
title = '标题2',
content = '内容2',
types = '百家2',
),
News(
title = '标题3',
content = '内容3',
types = '百家3',
),
News(
title = '标题4',
content = '内容4',
types = '百家4',
),
News(
title = '标题5',
content = '内容5',
types = '百家5',
),
News(
title = '标题6',
content = '内容6',
types = '百家6',
),
])
self.session.commit()

'''查询一条数据'''
def get_one(self):
return self.session.query(News).get(1)
'''查询多条数据'''
def get_more(self):
return self.session.query(News).filter_by(is_valid=1)

def main():
obj = OrmTest()
# rest = obj.add_one()
# print(rest.id)
# rest = obj.get_one()
# print('ID:{0}=>{1}=>{2}'.format(rest.id,rest.title,rest.content))
# rest = obj.get_more()
# print(rest.count())
obj.add_all()


if __name__ == '__main__':
main()

Guess you like

Origin www.cnblogs.com/braveheart007/p/11022641.html