现在,我通过Python ORM'SqlAlchemy'在MySql中创建一个表
models.py
from sqlalchemy import Table, Column, Integer, String, DateTime, MetaData,ForeignKey
from sqlalchemy.sql import select
from database import Base
metadata = MetaData()
class test(Base): __abstract__ = True
_tablename_ = 'aa_test'
id = Column(String)
id2 = Column(String)
def __init__(self, id, id2):
self.id = self.id
self.id2 = id2
def __repr__(self):
return "<test('%d', '%s'>" % (self.id, self.id2)
database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from ConnectingValue import password, id, host, db
re_str = 'mysql+mysqlconnector://' + id + ':' + \
password + '@' + host + '/' + db + ''
engine = create_engine(
re_str)
db_session = scoped_session(sessionmaker(
autocommit=False, autoflush=False, bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
conn = engine.connect()
def init_db():
import models
Base.metadata.create_all(engine)
和上一次testFile1.py
from database import init_db, db_session
from sqlalchemy import select
from sqlalchemy.orm import mapper
from database import conn
def main():
create_entry()
if __name__ == "__main__":
main()
def create_entry():
init_db()
我想知道创建表虽然“类测试” 我上课了......但是,我一直无法运作。