Besides, db.session.execute only able to run in console command line(enter line by line) but not in script.
Doing a simple experiment by comment out db.session.execute it return "hihi", but has internal server error if doesn't comment out db.session.execute
from flask import Flask,jsonify,abort,make_response,request,render_template
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
import MySQLdb
import MySQLdb.cursors
from collections import OrderedDict
import json
app = Flask(__name__)
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="xxx",
password="xxx",
hostname="xxx.mysql.pythonanywhere-services.com",
databasename="xxx$default",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
db = SQLAlchemy(app)
@app.route('/KJ', methods=['GET'])
def KLSE1():
call = db.session.execute("SELECT * FROM KJ")
#col = ['index','Stock','Name','MACD','STOCH','RSI','ATR','Bollinger','SMA','SMAcross','Momentum','Volume_changes_pc','Previous_close','Change','Change_pc','R1','R2','S1','S2','52w_High','52w_Low','52w','50d','Trend_date','Stockcode','Syariah','Trade','BUY_total','HOLD_total','SELL_total','Market_sentiment','gin','Time','Market','Vin_change','Grading','Yield','EPS','DPS','Net_Margin','EPS_1g','EPS_5g','ROE','ROA','ROI','divpayout','PER','Fix','PEG','PEGY','CF_per_share','BV_per_share','QQ','QoQ','value_BG','NAV','Next_Report','Last_Report']
#a = call.fetchmany(5)
#d = [OrderedDict(zip(col,t)) for t in a]
#json.dumps(d, ensure_ascii=False)
return "hihi"
session isn't it for ORM?
error log
2017-01-12 07:56:39,184 :Exception on /KJ [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/xxx/mysite/flask_app.py", line 33, in KJ
call = db.engine.execute("SELECT * FROM KJ")
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy.py", line 746, in engine
return self.get_engine(self.get_app())
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy.py", line 763, in get_engine
return connector.get_engine()
File "/usr/local/lib/python2.7/dist-packages/flask_sqlalchemy.py", line 443, in get_engine
self._engine = rv = sqlalchemy.create_engine(info, **options)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/__init__.py", line 332, in create_engine
return strategy.create(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/strategies.py", line 64, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/dialects/mysql/mysqlconnector.py", line 77, in dbapi
from mysql import connector
File "./mysql.py", line 133, in <module>
curs.close()
NameError: name 'curs' is not defined
I have a mysql.py in my /vinasia, but they error persist even I deleted mysql.py