【问题标题】:Why sqlalchemy insert "?" value in sqlite?为什么 sqlalchemy 插入“?” sqlite的价值?
【发布时间】:2020-08-12 20:07:47
【问题描述】:

请帮忙解决问题。在数据库(sqlite)中创建表并在其中插入数据的问题。 难道是编码问题?或者可能还有差异版本包等?

build_database.py 开始得到这个

...
INSERT INTO good (art, size, timestamp) VALUES (?, ?, ?)
INSERT INTO good (art, size, timestamp) VALUES (?, ?, ?)
...

有人坚持吗(?

有3个文件:

config.py:

import os
import connexion
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow


basedir = os.path.abspath(os.path.dirname(__file__))

# Create the Connexion application instance
connex_app = connexion.App(__name__, specification_dir=basedir)

# Get the underlying Flask app instance
app = connex_app.app

# Configure the SQLAlchemy part of the app instance
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'ec.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# Create the SQLAlchemy db instance
db = SQLAlchemy(app)

# Initialize Marshmallow
ma = Marshmallow(app)

models.py

from datetime import datetime
from config import db, ma


class Good(db.Model):
    _tablename__ = 'good'
    good_id = db.Column(db.Integer, primary_key=True)
    art = db.Column(db.String(32), index=True)
    size = db.Column(db.String(32))
    timestamp = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)


class GoodSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Good
        load_instance = True

build_database.py

import os
from config import db
from models import Good

# Data to initialize database with
GOODS = [
    {'art': 'art1', 'size': '42'},
    {'art': 'art2', 'size': '44'},
]

# Delete database file if it exists currently
if os.path.exists('ec.db'):
    os.remove('ec.db')

# Create the database
db.create_all()

# Iterate over the PEOPLE structure and populate the database
for good in GOODS:
    p = Good(art=good['art'], size=good['size'])
    db.session.add(p)

db.session.commit()

【问题讨论】:

  • ? 是一个占位符,是驱动程序的标记,它应该在那里放置一个参数。
  • 谢谢你说“司机”——这对对方很有帮助

标签: python sqlite flask sqlalchemy marshmallow


【解决方案1】:

数据库表不可见,因为没有预装 PyCharm IDE 驱动

。就这样)

【讨论】:

    猜你喜欢
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-30
    相关资源
    最近更新 更多