from sqlalchemy import create_engine,Table,Column,Integer,String,MetaData,ForeignKey engine=create_engine("mysql+pymysql://root:a5230411@localhost:3306/test",echo=True) metadata=MetaData(engine) user=Table('user',metadata, Column('id',Integer,primary_key=True), Column('name',String(20)), Column('fullname',String(40)), ) address_table = Table('address', metadata, Column('id', Integer, primary_key=True), Column('user_id', None, ForeignKey('user.id')), Column('email', String(128), nullable=False) ) metadata.create_all()
from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String connect = create_engine("mysql+pymysql://root:123456@localhost:3306/mysql", encoding="utf-8", echo=True) # 连接数据库,echo=True =>把所有的信息都打印出来 Base = declarative_base() # 生成ORM基类 class User(Base): __tablename__ = "hello_word" # 表名 id = Column(Integer, primary_key=True) name = Column(String(32)) password = Column(String(64)) Base.metadata.create_all(connect) # 创建表结构
mysql语句建表
CREATE TABLE user ( id INTEGER NOT NULL AUTO_INCREMENT, name VARCHAR(32), password VARCHAR(64), PRIMARY KEY (id) )