Python-SqlAlchemy-如何通过避免重复来优雅地插入?

时间:2018-08-19 19:34:53

标签: python mysql sqlalchemy sql-insert

我已经尝试过了,但是没有运气:

class Program
{
    static void Main(string[] args)
    {
        using (DnsServer server = new DnsServer(System.Net.IPAddress.Any, 10, 10))
        {
            server.QueryReceived += OnQueryReceived;

            server.Start();

            Console.WriteLine("Press any key to stop server");
            Console.ReadLine();
        }
    }

    static async Task OnQueryReceived(object sender, QueryReceivedEventArgs e)
        {
            DnsMessage query = e.Query as DnsMessage;
            if (query == null) return;
            DnsMessage response = query.CreateResponseInstance();

            if (response.Questions.Any())
            {
                DnsQuestion question = response.Questions[0];
                DnsMessage upstreamResponse = await DnsClient.Default.ResolveAsync(question.Name, question.RecordType, question.RecordClass);

                response.AdditionalRecords.AddRange(upstreamResponse.AdditionalRecords);
                response.ReturnCode = ReturnCode.NoError;

                if (!question.Name.ToString().Contains("example.com"))
                {
                    response.AnswerRecords.AddRange(upstreamResponse.AnswerRecords);
                }
                else
                {
                    response.AnswerRecords.AddRange(
                        upstreamResponse.AnswerRecords
                            .Where(w => !(w is ARecord))
                            .Concat(
                                upstreamResponse.AnswerRecords
                                    .OfType<ARecord>()
                                    .Select(a => new ARecord(a.Name, a.TimeToLive, IPAddress.Parse("192.168.0.199"))) // some local ip address
                            )
                    );
                }

                e.Response = response;
            }
        }
}

我仍然收到此错误:

def sqlTryInsert(result_obj):
 try:
  session.add(result_obj)
  session.flush()
  session.commit() 
 except (exc.SQLAlchemyError, exc.DBAPIError) as e:
  code, msg = e.orig
  if code == 1062:
    # send warning
    pass

0 个答案:

没有答案