def __init__(self, app, db): config = { 'host': 'localhost', 'database': db, # we got db as an argument 'user': '******', 'password': '******', 'port': '3306' # change the port to match the port your SQL server is running on } # this will use the above values to generate the path to connect to your sql database DATABASE_URI = "mysql://{}:{}@127.0.0.1:{}/{}".format(config['user'], config['password'], config['port'], config['database']) app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True # establish the connection to database self.db = SQLAlchemy(app)
bcrypt = Bcrypt(app) #Arch ---Remove these lines once you start using the views module FEARAPP_STATUS_CREATED=0 FEARAPP_STATUS_SENT_SMS=1 FEARAPP_STATUS_RECVD=2 FEARAPP_STATUS_UPDATED=3 flask.ext.sqlalchemy import SQLAlchemy #Arch ---And uncomment the following line #from views import app, bcrypt import os from datetime import datetime, timedelta app.config['SQLALCHEMY_DATABASE_URI']='postgresql+psycopg2://fearappuser:abc123@localhost:5432/fearappdb' db = SQLAlchemy(app) class User(db.Model): """ Defines the columns and keys for User table """ userid = db.Column(db.Integer, primary_key=True) username = db.Column(db.String) password = db.Column(db.String) firstname = db.Column(db.String) lastname = db.Column(db.String) phone = db.Column(db.String(10)) email = db.Column(db.String) users = db.relationship("Event", backref = "user") def __init__ (self, username, password, firstname, lastname, phone, email): self.username = username
from flask-sqlalchemy import SQLAlchemy db = SQLAlchemy()
#coding:utf-8 from flask import Flask, render_template, request, url_for, redirect import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite' db = SQLAlchemy(app) class Pessoa(db.Model): __tablename__='cliente' _id=db.Column(db.Integer, primary_key=True, autoincrement=True) nome=db.Column(db.String) telefone=db.Column(db.String) cpf=db.Column(db.String) email=db.Column(db.String) def __init__(self, nome, telefone, cpf, email): self.nome=nome self.telefon=telefone self.cpf=cpf self.email=email db.create_all() @app.route("/index") def index(): return render_template("index.html")
import logging <% if (package.services.mongodb && package.flask.mongoengine) { %> import sys <% } %> from flask import Blueprint <% if (package.services.mongodb && package.flask.mongoengine) { %> from flask.ext.mongoengine import MongoEngine, MongoEngineSessionInterface #from flask.ext.mongoengine import MongoEngineSessionInterface from pymongo.errors import AutoReconnect <% } %> <% if (package.flask.sqlalchemy) { %> from flask.ext.sqlalchemy import SQLAlchemy from flask import got_request_exception, current_app db = SQLAlchemy() def got_request_exception_handler(sender, exception, **extras): db.session.rollback() # pragma: no cover <% } %> <% if (package.services.mongodb && package.flask.mongoengine) { %> mongo = MongoEngine() def do_mongoengine_healthcheck(): conn = mongo.connection.connection try: return conn.command('ping').get('ok', 0) == 1.0 except AutoReconnect: logging.exception(sys.exc_info()[1])