SQLAlchemy

SQLAlchemy

ORM

全称

  object - Relation - mapping  对象关系映射

作用

  将数据库中的一张表table,通过某种方式,转换为python中的Class或者是Class实例化得到的对象。至于是通过何种方式,我们不必计较。然后使用orm操作数据库。

   Class(object) - 某种方式 - table

  与Django中的model的使用方法是一样的。

  Django - Model

使用

创建新的虚拟环境

  创建一个干净的环境

安装SQLAlchemy

pip install SQLAlchemy

安装pymysql

pip install pymysql

创建表

Django中的ORM
Class - obj
创建数据库引擎
将所有的Class序列化成数据表
ORM操作 - CRUD
SQLAlchemy的使用

  SQLAlchemy的使用方法与Django的ORM的使用方式相似,我们参考Django的使用方法进行操作。

创建Class
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, INT, VARCHAR, String


Base = declarative_base()

  Base是ORM模型的基类。

  ORM模型 

    - object里面的属性等同于table中创建的字段。

    - Obj定义table的操作方式和属性。

class User(Base):
    __tablename__ = 'student'
    id = Column(INT, primary_key=True, autoincrement=True)
    name = Column(String(32), index=True)
创建数据库引擎
from sqlalchemy import create_engine

engine = create_engine('mysql+pymysql://root:@127.0.0.1:3306/sqlalchemy?charset=utf8')  # 注意:这里的utf8不能写成utf-8

  这里要注意的是编码方式设置的部分utf8不能写成utf-8,否则将会报错。

将所有继承Base的Class序列化成数据表
Base.metadata.create_all(engine)

  正确创建表完成后,会产生一个警告。这是正常的,警告如下:

C:\sqlalchemy\venv\lib\site-packages\pymysql\cursors.py:170: Warning: (1366, "Incorrect string value: '\\xD6\\xD0\\xB9\\xFA\\xB1\\xEA...' for column 'VARIABLE_VALUE' at row 484")
  result = self._query(query)
注意

  在运行代码之前,需要先启动MySQL,然后手动创建数据库。

登录数据库
mysql -u root -p  # 登录数据库
创建数据库
create database xxoo;  # xxoo为数据库名称

 操作

增加数据

原生sql语句

insert into 表名(字段名) values(值);

 

 

 

 

 

 

 

 

 

 

 

0

转载于:https://www.cnblogs.com/ZN-225/p/10403704.html

The SQLAlchemy Object Relational Mapper presents a method of associating user-defined Python classes with database tables, and instances of those classes (objects) with rows in their corresponding tables. It includes a sys- tem that transparently synchronizes all changes in state between objects and their related rows, called a unit of work, as well as a system for expressing database queries in terms of the user defined classes and their defined relationships between each other. The ORM is in contrast to the SQLAlchemy Expression Language, upon which the ORM is constructed. Whereas the SQL Expression Language, introduced in SQL Expression Language Tutorial, presents a system of representing the primitive constructs of the relational database directly without opinion, the ORM presents a high level and abstracted pattern of usage, which itself is an example of applied usage of the Expression Language. While there is overlap among the usage patterns of the ORM and the Expression Language, the similarities are more superficial than they may at first appear. One approaches the structure and content of data from the perspective of a user-defined domain model which is transparently persisted and refreshed from its underlying storage model. The other approaches it from the perspective of literal schema and SQL expression representations which are explicitly composed into messages consumed individually by the database. A successful application may be constructed using the Object Relational Mapper exclusively. In advanced situations, an application constructed with the ORM may make occasional usage of the Expression Language directly in certain areas where specific database interactions are required. The following tutorial is in doctest format, meaning each >>> line represents something you can type at a Python command prompt, and the following text represents the expected return value.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值