In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
# _ * _ coding:utf-8_*_''' basic process of creating a SQLAlchemy application 1, creating a connection (creating a connection with a relational database) 2, declaring a mapping file (one-to-one mapping from python to tables in classes and databases In this way, you can manipulate the table in the database through the classes in python) 3, create the schema (you can create the table) 4, initialize the mapping instance 5, create the session 6, persist the instance object 'from sqlalchemy import create_engine# to create the connection engine = create_engine (' mysql://root:1qaz#EDC@192.168.89.101:3306/student' Echo=True)''parameter description: mysql indicates that the database to be connected is the mysql database root indicates that the root account is logged into the mysql database 1qaz#EDC indicates that the password @ of the root account connecting to the database is a symbol The specified format 192.168.89.1.101sql 3306 indicates the connection address and port of the database 3306soms indicates the name of the database to be connected echo=True indicates whether the sql statement is printed during execution False introduces the declarative_base class Base = declarative_base () # for not printing the mapping file from sqlalchemy.ext.declarative import declarative_base # instantiates a declarative_base instance from sqlalchemy import Column, Integer, String # introduces classes Column, Integer, String class class User (Base): # creates its own instance class name User inherits Base class _ _ tablename__ = 'users' # this attribute Indicates that this class maps to the users table in the database id = Column (Integer, primary_key=True) # this attribute indicates that id represents a column in the database table, Integer represents an integer, and id is the primary key name = Column (String (10)) # this attribute indicates that it is also a column in the database table, and its type is string passwd = Column (String (10)) # this attribute indicates that it is also a column in the database table Its type is string def _ _ repr__ (self): # defines a method to represent the User class more vividly, which is not very useful return "%" (self.name, self.name, self.passwd) # create schema, create database tables Table name: usersUser.metadata.create_all (engine) # create session (session)''create rendezvous' 'from sqlalchemy.orm import sessionmaker # introduce sessionmaker this function Session = sessionmaker (bind=engine) # create session dialog, through this session you can do some operations on the database session = Session () # bind # persist an instance object ed_user = User (id=2,name='tantianran' Passwd='1qaz#EDC') # insert data session.add (ed_user) into the column in the database table # add this instance to session session.commit () # submit
Execution result:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.