简体   繁体   English

如何像 flask 中的 sqlite 一样配置 mysql?

[英]How do I configure mysql the same way as sqlite in flask?

This is the suggested configuration from the Doc for sqlite.这是 sqlite 文档中的建议配置。 How would it be for mysql? mysql 会怎样?

import sqlite3

import click
from flask import current_app, g
from flask.cli import with_appcontext


def get_db():
    if 'db' not in g:
        g.db = sqlite3.connect(
            current_app.config['DATABASE'],
            detect_types=sqlite3.PARSE_DECLTYPES
        )
        g.db.row_factory = sqlite3.Row

    return g.db


def close_db(e=None):
    db = g.pop('db', None)

    if db is not None:
        db.close()
def init_db():
    db = get_db()

    with current_app.open_resource('schema.sql') as f:
        db.executescript(f.read().decode('utf8'))

I would like to use the same command init-db to initialize the mysql我想使用相同的命令 init-db 来初始化 mysql

@click.command('init-db')
@with_appcontext
def init_db_command():
    """Clear the existing data and create new tables."""
    init_db()
    click.echo('Initialized the database.')

In ext.py :ext.py

import pymysql

db = None

def get_db():
   global db
   if db is None:
       db = pymysql.connect("localhost","testuser","test123","TESTDB")
   return db
   

In other module, if you want to execute sql:在其他模块中,如果要执行 sql:

from ext import get_db
db = get_db()
db.cursor.execute(sql)

db is the singleton object. db是 singleton object。 I suggest to use the sqlalchemy in Flask.我建议在 Flask 中使用 sqlalchemy。

I would def.我会定义。 recommend you to use Flask-SQLAlchemy , so you do not have to deal with low level SQL problems.建议您使用Flask-SQLAlchemy ,这样您就不必处理低级 SQL 问题。

As a plus, you could use eg SQLite locally for testing, and MySQL or Postgresql in production, without changed your code (only your config).另外,您可以在本地使用例如SQLite进行测试,并在生产中使用MySQLPostgresql ,而无需更改您的代码(仅您的配置)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在GoDaddy上为我的Python Flask应用配置设置 - How do I configure settings for my Python Flask app on GoDaddy 如何将“flask”配置为仅从浏览器运行文件? - How do I configure “flask” to just run files from the browser? 我如何使用Windows防火墙配置python / flask进行公共访问 - how do i configure python/flask for public access with windows firewall 如何在同一台服务器上为flask和php配置apache - How to configure apache for flask and php on the same server 如何配置多个Django应用程序以使用同一数据库? - How do I configure multiple django applications to use the same database? Flask 如何在同一个模板中处理多个 sqlite3 查询? - Flask how can I handle multiple sqlite3 queries in the same template? 如何使用nginx和uwsgi配置基于Flask的python应用程序? - How do I configure a Flask-based python app using nginx and uwsgi? 如何使用 flask_msearch 配置模糊性等参数? - How do I configure parameters like fuzziness using flask_msearch? 按照流行的 Flask 教程尝试从数据库中读取时,如何解决 Flask 错误“((sqlite3.OperationalError)没有这样的表:)”? - How do I solve a Flask error '((sqlite3.OperationalError) no such table:)' when attempting to read from a database following a popular Flask tutorial? 如何使用 Python、Flask 和 SQLite3 实现下拉 select 选项的动态相关更改? - How do I implement dynamic dependent change of dropdown select options, using Python, Flask and SQLite3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM