我正在尝试存储SQLAlchemy生成的SQL查询,以及每个运行所需的时间。我正在使用事件监听器来存储SQL:
statements = []
@listens_for(DBSession.get_bind(), "before_cursor_execute", named=True)
def before_cursor_execute(**kw):
statements.append(kw['statement'])
我可以以某种方式使用相同的事件监听器来存储执行时间,还是应该使用其他东西?
答案 0 :(得分:2)
您可以使用after_cursor_execute
记录查询的开始时间,然后计算@event.listens_for(engine, "before_cursor_execute")
def _record_query_start(conn, cursor, statement, parameters, context, executemany):
conn.info["query_start"] = datetime.now()
@event.listens_for(engine, "after_cursor_execute")
def _calculate_query_run_time(conn, cursor, statement, parameters, context, executemany):
print("this query took {}".format(datetime.now() - conn.info["query_start"]))
中的差异,如下所示:
String[] options = new String[]{
"Existing Customer",
"New Customer"
};
int selection = 0;
do{
System.out.println("Please choose one of the following to determine whether you are a new or existing customer");
for (int i = 0; i < options.length; i++) {
System.out.printf("[%s] - %s%n",i+1, options[i]);
}
String input = new Scanner(System.in).nextLine();
try{
selection = Integer.parseInt(input);
}catch(NumberFormatException e){
System.out.println("That is not a vaild. Please select from one of the following options.");
selection = 0;
}
}while(selection <= 0 || selection > options.length);
switch(selection){
case 1:
System.out.println("Welcome Back");
break;
case 2:
System.out.println("Hello, I see you are a new customer,");
System.out.println("let's get your account setup!");
break;
}