本文整理汇总了Python中flask.ext.mysql.MySQL类的典型用法代码示例。如果您正苦于以下问题:Python MySQL类的具体用法?Python MySQL怎么用?Python MySQL使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MySQL类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setupDb
def setupDb():
mysql = MySQL()
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'nanivallu'
app.config['MYSQL_DATABASE_DB'] = 'devathon'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
return mysql
示例2: create_connection
def create_connection():
mysql = MySQL()
app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'virball'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
con = mysql.connect()
return con
示例3: MySQL
# _password = request.form['inputPassword']
# # validate the received values
# if _name and _email and _password:
# return json.dumps({'html':'<span>All fields good !!</span>'})
# else:
# return json.dumps({'html':'<span>Enter the required fields</span>'})
# if __name__ == '__main__':
# app.run()
from flask import Flask, render_template, json, request
from flask.ext.mysql import MySQL
from werkzeug import generate_password_hash, check_password_hash
mysql = MySQL()
app = Flask(__name__)
# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'imack'
app.config['MYSQL_DATABASE_DB'] = 'Bucketlist'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
# the toolbar is only enabled in debug mode:
app.debug = True
# set a 'SECRET_KEY' to enable the Flask session cookies
app.config['SECRET_KEY'] = '<replace with a secret key>'
示例4: Flask
from flask import Flask
from flask.ext.mysql import MySQL
app = Flask(__name__)
app.debug = True
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'ivan'
app.config['MYSQL_DATABASE_DB'] = 'tp_db'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
conn = mysql.connect()
conn.autocommit(True)
cursor = conn.cursor()
from app import User, Forum, Thread, Post, General
示例5: MySQL
import os
import uuid
import flask
import flask.views
from flask import render_template
from flask import request, make_response,send_from_directory
from werkzeug import secure_filename
from flask.ext.httpauth import HTTPBasicAuth
from flask import jsonify , json
app = flask.Flask(__name__,static_url_path='')
print app.static_url_path
from flask.ext.mysql import MySQL
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = '17576cube'
app.config['MYSQL_DATABASE_DB'] = 'forum'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
cursor = mysql.connect().cursor()
@app.route('/')
@app.route('/login',methods=['GET'])
def index():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login():
user = request.form['email']
password = request.form['pass']
return authenticate(user,password)
def authenticate(username, password):
示例6: load_config
from flask import Flask, url_for, request, jsonify, make_response, g
from flask.ext.mysql import MySQL
from import_config import load_config
import json
import types
config = load_config()
mysql = MySQL()
basic_api = Flask(__name__)
basic_api.config['MYSQL_DATABASE_USER'] = config["database"]["user_name"]
basic_api.config['MYSQL_DATABASE_PASSWORD'] = config["database"]["password"]
basic_api.config['MYSQL_DATABASE_DB'] = config["database"]["db_instance"]
basic_api.config['MYSQL_DATABASE_HOST'] = config["database"]["connection_url"]
mysql.init_app(basic_api)
url_root = '/todo/api/v3.0/'
def format_json(element):
new_task = {}
new_task["id"] = element[0]
new_task["title"] = element[1]
new_task["description"] = element[2]
new_task["done"] = element[3]
return new_task
@basic_api.before_request
def db_connect():
g.db_conn = mysql.connect()
示例7: __init__
def __init__(self, app):
self.mysql = MySQL()
self.mysql.init_app(app)
示例8: Database
class Database():
def __init__(self, app):
self.mysql = MySQL()
self.mysql.init_app(app)
def get_all_users(self):
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
query = """SELECT username, email, password FROM Users WHERE is_active = True"""
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
users = []
for row in rows:
users.append(User(row[0], row[1], row[2]))
#self.mysql.get_db().close()
return users
def get_user(self, username=None, email=None, password=None):
if email is None and username is None:
return None
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
first = True
query = """SELECT username, email, password, is_active FROM Users WHERE """
if username:
query += """username = '%s'""" % (username)
first = False
if email:
if not first:
query += """ AND """
query += """email = '%s'""" % (email)
first = False
if password:
if not first:
query += """ AND """
query += """password = '%s'""" % (password)
cursor.execute(query)
u = cursor.fetchone()
cursor.close()
#self.mysql.get_db().close()
if not u:
return None
return User(u[0], u[1], u[2], u[3])
def insert_user(self, username, email, password):
if (not (username and email and password)):
return None
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
statement = """INSERT INTO Users (username, email, password) VALUES ('%s', '%s', '%s')""" % (username, email, password)
cursor.execute(statement)
self.mysql.get_db().commit()
cursor.close()
#self.mysql.get_db().close()
return User(username, email, password)
def update_user(self, username, email=None, password=None, is_active=True):
if (not (username) or not (email or password)):
return None
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
statement = """UPDATE Users SET is_active = %s""" % (str(is_active))
if email:
statement += """, email = '%s'""" % email
if password:
statement += """, password = '%s'""" % password
statement += """ WHERE username = '%s'""" % username
cursor.execute(statement)
self.mysql.get_db().commit()
cursor.close()
#self.mysql.get_db().close()
return User(username, email, password)
def get_all_players(self):
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
query = """SELECT player_id, first_name, last_name, nfl_team FROM Players"""
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
players = []
for row in rows:
players.append(Player(row[0], row[1], row[2], row[3]))
#self.mysql.get_db().close()
return players
def get_player(self, player_id=None, first_name=None, last_name=None, nfl_team=None):
if player_id is None and first_name is None and last_name is None and nfl_team is None:
return None
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
first = True
query = """SELECT player_id, first_name, last_name, nfl_team FROM Players WHERE """
if player_id:
query += """player_id = '%s'""" % (player_id)
#.........这里部分代码省略.........
示例9: MySQL
from flask import Flask, render_template, json, request
from flask.ext.mysql import MySQL
from werkzeug import generate_password_hash, check_password_hash
mysql = MySQL()
application = app = Flask(__name__)
# MySQL configurations
"""
application.config['MYSQL_DATABASE_USER'] = 'jay'
application.config['MYSQL_DATABASE_PASSWORD'] = 'signupjay'
application.config['MYSQL_DATABASE_DB'] = 'signup'
application.config['MYSQL_DATABASE_HOST'] = 'signup.cfj54gpjcmhy.us-west-2.rds.amazonaws.com:3306'
mysql.init_app(application)"""
@app.route('/')
def main():
return render_template('index.html')
@app.route('/showSignUp')
def showSignUp():
return render_template('signup.html')
@app.route('/signUp',methods=['POST','GET'])
def signUp():
try:
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']
示例10: Flask
#
from __future__ import print_function
from flask import Flask
from flask import request, redirect, url_for, render_template, send_from_directory, session
from flask.ext.mysql import MySQL
from werkzeug import secure_filename
from mimetypes import MimeTypes
import os
import time
import logging
import boto3
from boto3.s3.transfer import S3Transfer
from config import *
image_uploader = Flask(__name__)
mysql = MySQL()
image_uploader.config['MYSQL_DATABASE_USER'] = DB_USER
image_uploader.config['MYSQL_DATABASE_PASSWORD'] = DB_PASS
image_uploader.config['MYSQL_DATABASE_DB'] = DB_NAME
image_uploader.config['MYSQL_DATABASE_HOST'] = DB_HOST
mysql.init_app(image_uploader)
#
# Help functions
#
def allowed(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in IMAGE_TYPES
def put_s3_objetct(image_name, storage_dir):
mime = MimeTypes()
s3 = boto3.client('s3', region_name=AWS_REGION)
示例11: Database
class Database(object):
def __init__(self, app):
self.mysql = MySQL()
self.mysql.init_app(app)
####### USERS #######
def get_all_users(self):
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
query = """SELECT uid, uname, email, pass FROM Users WHERE is_active = True"""
cursor.execute(query)
rows = cursor.fetchall()
cursor.close()
users = []
for row in rows:
users.append(User(row[0], row[1], row[2], row[3]))
return users
def get_user(self, username=None, email=None, password=None, dob=None):
if email is None and username is None:
return None
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
first = True
query = """SELECT uid, uname, email, pass, udob FROM Users WHERE """
if username:
query += """uname = '%s'""" % (username)
first = False
if email:
if not first:
query += """ AND """
query += """email = '%s'""" % (email)
first = False
if password:
if not first:
query += """ AND """
query += """pass = '%s'""" % (password)
if dob:
if not first:
query += """ AND """
query += """udob = '%s'""" % (dob)
cursor.execute(query)
u = cursor.fetchone()
cursor.close()
if not u:
return None
return User(u[1], u[2], u[3], u[4], u[0])
def insert_user(self, username, email, password, dob):
if (not (username and email and password)):
return None
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
statement = """INSERT INTO Users (uname, email, pass, udob) VALUES ('%s', '%s', '%s', '%s')""" % (username, email, password, dob)
cursor.execute(statement)
self.mysql.get_db().commit()
cursor.close()
return User(username, email, password, dob)
def update_user(self, username, email=None, password=None, is_active=True):
if (not (username) or not (email or password)):
return None
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
statement = """UPDATE Users SET is_active = %s""" % (str(is_active))
if email:
statement += """, email = '%s'""" % email
if password:
statement += """, pass = '%s'""" % password
statement += """WHERE uname = '%s'""" % unsername
cursor.execute(statement)
self.mysql.get_db().commit()
cursor.close()
return User(username, email, password)
####### MOVIES #######
def get_people_like(self, pname, limit=10):
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
query = """SELECT pid, pname FROM People WHERE pname LIKE '%s%%' OR pname LIKE '%%%s' OR pname LIKE '%%%s%%' LIMIT %s;""" % (pname, pname, pname, limit)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
r = []
for result in results:
r.append(dict(id=result[0],name=result[1]))
return json.dumps(r)
def get_movies_like(self, title, limit=5):
self.mysql.before_request()
cursor = self.mysql.get_db().cursor()
query = """SELECT mid, title FROM Movies WHERE title LIKE '%s%%' OR title LIKE '%%%s' OR title LIKE '%%%s%%' LIMIT %s;""" % (title, title, title, limit)
cursor.execute(query)
results = cursor.fetchall()
cursor.close()
r = []
for result in results:
#.........这里部分代码省略.........
示例12: Flask
from flask import Flask, request, Response, jsonify;
from flask_restful import Resource, Api, reqparse;
from flask.ext.mysql import MySQL;
import json;
app = Flask(__name__);
#MySQL configurations
app.config['MYSQL_DATABASE_USER'] ='root';
app.config['MYSQL_DATABASE_PASSWORD'] ='root1234';
app.config['MYSQL_DATABASE_DB'] ='analysis_app_db';
app.config['MYSQL_DATABASE_HOST'] ='localhost';
mysql=MySQL();
mysql.init_app(app);
class Tag(Resource):
def get(self):
try:
query_result = execute_query("SELECT * FROM tb_tag");
data = json.dumps(query_result);
resp = Response(data, status=200, mimetype='application/json');
return resp;
except Exception as e:
return {'error':str(e)};
class Test(Resource):
def get(self):
示例13: Flask
from __future__ import division
from flask import Flask, Response, request, abort, render_template, jsonify
from flask.ext.mysql import MySQL
from flask.ext.cors import CORS
import json
import time
import datetime
import math
import os
import requests
application = Flask(__name__)
CORS(application)
mysql = MySQL()
application.config['MYSQL_DATABASE_USER'] = os.environ.get('RDS_USERNAME')
application.config['MYSQL_DATABASE_PASSWORD'] = os.environ.get('RDS_PASSWORD')
application.config['MYSQL_DATABASE_DB'] = 'parking'
application.config['MYSQL_DATABASE_HOST'] = 'parking.c9q5edmigsud.us-west-2.rds.amazonaws.com'
application.config['MYSQL_DATABASE_PORT'] = 3306
mysql.init_app(application)
@application.route('/')
def index():
return render_template('index.html')
@application.route('/paystations', methods=['GET', 'POST'])
def get_paystations():
element_keys = request.args.get('element_keys', None)
cur = mysql.connect().cursor()
query = "SELECT * FROM blockfaces"