[英]How can I write a python script to connect AngularJS with mysql database?
All over the web people use Php script to make queries between AngularJS and Mysql database. 网络上的所有人都使用Php脚本在AngularJS和Mysql数据库之间进行查询。 I'm making a Flask application and would like to use Python for the queries but I don't know how.
我正在制作一个Flask应用程序,并且想使用Python进行查询,但是我不知道如何做。 Can anyone help me?
谁能帮我? Here's what I tried so far: Python script:
到目前为止,这是我尝试过的操作:Python脚本:
import pymysql
connection = pymysql.connect(host='localhost',
user='root',
password='*******',
db='testdb',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
cursor.execute("SELECT * from posts")
posts = cursor.fetchall()
JS: JS:
var application = angular.module("myApp", []);
application.config(['$interpolateProvider', function($interpolateProvider) {
$interpolateProvider.startSymbol('[{');
$interpolateProvider.endSymbol('}]');
}]);
application.controller("postController", function($scope, $https){
$https.get("dbconnect.py").then( function(response) {
$scope.posts = response.data.records;
});
});
return data from your pyscript. 从pyscript返回数据。 use
flask
to run. 用
flask
运行。 call python file.py
you can see your data in http://localhost:5000/
调用
python file.py
可以在http://localhost:5000/
查看数据
use this url in your angular to GET
data... 使用此网址以角度
GET
数据...
import pymysql, json
from flask import Flask
app = Flask(__name__)
def date_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
def get_db_data():
connection = pymysql.connect(host='host',
port=3307,
user='uname',
password='pwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
cursor.execute("SELECT * from user")
posts = cursor.fetchall()
return json.dumps(posts, default=date_handler)
@app.route("/")
def hello():
xml = get_db_data()
# make fancy operations if you want
return xml
if __name__ == "__main__":
app.run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.