且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在单个sql查询中编写多个条件以获取数据-Python mysql

更新时间:2023-01-30 08:00:07

Jeremy,

为回答您的问题,Martijn做了出色的解释:

To answer your question, Martijn did a great job of explaining here:

sql =从PERSON中选择*,其中F_Name =%s或L_Name =%s或Age =%s或Gender =%s",>然后self.cursor.execute(sql,(self.fname,self.lname ,self.age,self.gender))

sql = "select * from PERSON where F_Name = %s or L_Name = %s or Age = %s or Gender = %s", > then self.cursor.execute(sql, (self.fname, self.lname, self.age, self.gender))

但是,对于您而言,***的主意是使用ORM.从长远来看,这将为您省去很多麻烦!

But, in your case, the best idea would be to use an ORM. This would save you a lot of trouble in the long run!

编写自己的SQL查询很好,但是,您会遇到一些问题.基于SQL的攻击以及难以解析数据的攻击有两种.

Writing your own SQL queries is fine, however, you open yourself to several problems. SQL-based attacks, as well as just having a harder time parsing your data, are two.

有关ORM的常规信息: http://en.wikipedia.org/wiki/Object-relational_mapping

Generic information on ORMs: http://en.wikipedia.org/wiki/Object-relational_mapping

对于Python,有一些不错的选择. SQlAlchemy是最著名的,但是环顾四周,看看您需要什么.

For Python, there are a few good ones. SQlAlchemy is the most known, but look around and see what you need.