Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python代码上使用Flask将SQLite转换为PostgreSQL_Python_Postgresql_Sqlite_Flask - Fatal编程技术网

在python代码上使用Flask将SQLite转换为PostgreSQL

在python代码上使用Flask将SQLite转换为PostgreSQL,python,postgresql,sqlite,flask,Python,Postgresql,Sqlite,Flask,我的应用程序是通过使用启动的:不幸的是,对我来说,他们使用了我将用于我的项目的所有技术,但数据库除外,它是SQLite。我想使用PostgreSQL,特别是因为它提供了更好的性能和安全性,但我不知道如何将我的代码转换为使用PostgreSQL而不是SQLite 我不想丢失我的数据(只是一堆随机数据——比如一个名字的uzcvbuyz,你知道我的意思),我如何转换下面的代码 这是使用sqlite3库的单独文件,名为db.py: import sqlite3 import click from fl

我的应用程序是通过使用启动的:不幸的是,对我来说,他们使用了我将用于我的项目的所有技术,但数据库除外,它是SQLite。我想使用PostgreSQL,特别是因为它提供了更好的性能和安全性,但我不知道如何将我的代码转换为使用PostgreSQL而不是SQLite

我不想丢失我的数据(只是一堆随机数据——比如一个名字的uzcvbuyz,你知道我的意思),我如何转换下面的代码

这是使用
sqlite3
库的单独文件,名为db.py

import sqlite3

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

def init_app(app):
    app.teardown_appcontext(close_db)
    app.cli.add_command(init_db_command)

def init_db():
    db = get_db()
    with current_app.open_resource('schema.sql') as d:
        db.executescript(d.read().decode('utf8'))

@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.')

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()  
如果还有其他步骤要做(我使用Ubuntu 18.04,所以命令行对我来说并不难),请告诉我。请记住,我不在乎数据丢失,我之前已经用这个或那个链接记录了我自己的迁移过程


我也尝试使用
psycopg2
转换它,但我无法很好地完成(如果没有,我就不会问这个问题;)

关于转换此代码,您特别关注什么?不幸的是,“所有这些”都会使这个问题对于堆栈溢出来说过于宽泛。@roganjosh在第4部分,在Flask SQLAlchemy配置中,他说
在开发过程中,我将使用SQLite数据库
@halfer仅仅转换
get_db()
定义对于
psycopg2
包来说是不够的吗?我已经读到了
psycopg2
也有一个
connect
对象,但是我没有找到哪一个可以替换
sqlite3.Row
:/