postgres数据库通过models创建表,直接上代码:如下:
# coding=utf-8
import psycopg2
import os
import time
import json
from flask import Flask, request, session, redirect, url_for, flash
from flask_redis import FlaskRedis
from flask import jsonify
from flask_sqlalchemy import SQLAlchemy
import datetime
# config = {
# 'CACHE_TYPE': 'redis',
# 'CACHE_REDIS_HOST': '127.0.0.1',
# 'CACHE_REDIS_PORT': 6379,
# 'CACHE_REDIS_DB': 1,
# 'CACHE_REDIS_PASSWORD': '',
# }
# conn = psycopg2.connect(database="skyeye", user="postgres", password="", host="127.0.0.1", port="5432") # 链接数据库
print " ------ Opened database successfully"
app = Flask(__name__)
# postgresql://postgres:postgres@127.0.0.1:5432/skynet
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@127.0.0.1:5432/database_name'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
# app.config.from_object(config)
# redis = FlaskRedis(app)
# redis.init_app(app)
# 初始化对象
db = SQLAlchemy(app)
class Test(db.Model):
__tablename__ = 'my_test'
id = db.Column(
'id',
db.Integer,
primary_key=True,
nullable=False,
autoincrement=True)
type = db.Column('type', db.Integer)
def __init__(self, alert_type, event_id, task_type):
self.alert_type = alert_type
db.drop_all()
db.create_all()