如何获取只删除SQLAlchemy发出的命令

时间:2017-11-07 22:38:08

标签: python sqlalchemy

我正在对每秒发送数十个SQL查询的应用程序进行故障排除。我知道有一种方法可以使用这种方法记录sqlalchemy发出的所有SQL语句:

import java.util.Scanner;
import java.util.Random;

public class Lottery {

   public static void main(String[] args) {

   Scanner keyboard = new Scanner(System.in);
   Random random = new Random();

   int guess, guess2, counter;
   int win1000, win10, win1;

   int number = random.nextInt(10); int number2 = random.nextInt(10);
   System.out.println("Welcome to the Super Lottery! \nLet's Play!");
   System.out.println("Round 1 - you have 5 chances to win money");

   System.out.print("Enter your guess: ");
   guess = keyboard.nextInt(); 
   guess2 = keyboard.nextInt();
   System.out.println("The lottery number is " + number + number2);

   win1 = 1;
   win10 = 10;
   win1000 = 1000;

   counter = 1;
   while(counter < 5) {
      number = random.nextInt(10); number2 = random.nextInt(10);
      System.out.print("Enter your guess: ");
      guess = keyboard.nextInt(); guess2 = keyboard.nextInt();
      System.out.println("The lottery number is " + number + number2);
      counter++;

   if(guess == number2 && guess2 == number) {
      System.out.println("You guessed both numbers, but in a different order! You win $" + win10);  
   }
   else if(guess == number && guess2 == number2) {
      System.out.println("CONGRATULATIONS! You win the Super Lottery!" + win1000);
   }
   else if(guess == number || guess2 == number2) {
       System.out.println("You guessed one number! You win $" + win1);
   }
   }

   }
}

但是如何仅记录import logging logging.basicConfig() logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) 语句?

1 个答案:

答案 0 :(得分:0)

您可以写logging filter

import logging
from sqlalchemy import create_engine

class Filter(logging.Filter):
    def filter(self, record):
        return 'DELETE' in record.msg

logging.basicConfig()
logging.getLogger('sqlalchemy.engine.base.Engine').setLevel(logging.INFO)
logging.getLogger('sqlalchemy.engine.base.Engine').addFilter(Filter())

e = create_engine("sqlite://")
e.execute("CREATE TABLE t(c);")
e.execute("INSERT INTO t (c) VALUES (NULL);")
e.execute("SELECT NULL")
e.execute("DELETE FROM t WHERE c = NULL")

作为SQLAlchemy本人的作者Mike Bayer的想法是suggested,作为类似问题的解决方案。