import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String, DateTime, Boolean engine = create_engine('mysql://root:@localhost:3306/news_test') Base = declarative_base() class News(Base): ''' 新闻类型 ''' __tablename__ = 'news' 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(Integer) created_at = Column(DateTime) is_valid = Column(Boolean)
Python console 中输入以下,没有报错,也没成功?
terminal中,输入以下,报错:sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1049, "Unknown database 'news_test'")
import sqlalchemy from test_mysql_orm import News from test_mysql_orm import engine News.metadata.create_all(engine)
一次实战同时掌握Python操作MySQL,MongoDB,Redis 三大数据库使用技巧
了解课程1.2k 13
1.1k 12
3.0k 12
1.6k 11
5.7k 10