当前位置:Gxlcms > 数据库问题 > Python:MySQLdb中sql语句'in'和‘like’要注意的一些问题

Python:MySQLdb中sql语句'in'和‘like’要注意的一些问题

时间:2021-07-01 10:21:17 帮助过:5人阅读

  •   conn = MySQLdb.connect(host=‘xxx‘,user=‘xxxxx,passwd=‘xxxx‘,db=‘xxxx‘)
  •   cursor = conn.cursor()
  •   sql = """select sum(amount_total),sum(amount_untaxed) from account_invoice where comment ="换美金"   and create_time >= ‘%s‘ and create_time <= ‘%s‘"""%(date_begin,date_end)
  •   cursor.execute(sql)
  •   result = cursor.fetchall()
  • 这是我当时查询数据库的指令,出现了乱码,提示latin1编码失败,即UnicodeEncodeError: ‘latin-1‘ codec can‘t encode character,后来我知道Mysql默认是latin1编码。首先我想到的解决办法是在文件开头加上

    1.   import sys
    2.   reload(sys)
    3.   sys.setdefaultencoding(‘utf-8‘)

    因为以前出现中文乱码时,用这种方法解决过,很遗憾,不过这次不管用。然后就查看官方手册呀,全是英文,虽然看不懂全部,但借助谷歌翻译还是能开的懂十之六七,那也足够了,发现MySQLdb.connect()函数可接受一个可以指定编码方式的参数charset,将其设置为utf-8即可,还有一种方法就是在conn=MySQLdb.connetct()后面加如下一句代码:

    conn.set_character_set(‘utf8‘)

    或者在cursor = conn.cursor()后面加下面三句任意一句。

    1.   cursor.execute(‘SET NAMES utf8;‘)
    2.   cursor.execute(‘SET CHARACTER SET utf8;‘)
    3.   cursor.execute(‘SET character_set_connection=utf8;‘)

     

    (2)“in”中碰到的问题

     

    如果已知‘in’在那个集合里就好办了,比如下句:

    sql = """select sum(credit) from account_moveline where  journal_id not in (34,18) """

    关键是不知道那个集合的具体是什么,是个动态的,不知道集合里具体有多少元素。

     

    想想平时是怎么用sql语句的。

    select * from table where id in (1,2,3,4)
    select * from table where name in (‘Jhon‘,‘James‘,‘Kobe‘)

    这里要分两种情况,一个是in后面集合()里的元素没有引号,一个是有引号。

    对于没有引号的,表示该字段类型是int型,加入有一个so_id = [one for one in range(1,100)],现在要查询表table1中id在so_id集合中的记录呢,这个sql语句怎写呢?

    先开始是这样写的

    1.   sql = """select * from table1 where id in %s "%(tuple(so_is))
    2.   cursor.execute(sql)
    3.   result = cursor.fetchall()

    是模仿select * from table where id in (1,2,3,4),结果报错,后来在stackoverflow上找到了解决方法,如下:

    1.   so_id_string = ",".join(map(str, so_id))
    2.   sql = """select * from table1 where id in (%s) "%(so_id_string)
    3.   cursor.execute(sql)
    4.   result = cursor.fetchall()

    其实最后想想,也不难,还是围绕这select * from table where id in (1,2,3,4)这种格式来的,将so_id里的元素变成一个用逗号隔开的字符串

     

    对于有引号的,表明该字段类型是字符串型,得想办法然集合里面的元素都带有引号还是上面例子假设表table1里有个字段state,是char型,其值是用字符‘1’,‘2’等表示此时用上面的办法就不行了。解决方法如下:

    1.   so_strings = ", ".join(map(lambda x:"‘%s‘"% x,so_id))#加了‘‘引号
    2.   sql = """select * from table1 where state in (%s) "%(so_string)
    3.   cursor.execute(sql)
    4.   result = cursor.fetchall()

    可能这个例子不好吧,不知道你们能不能看懂,希望有机会的能自己动手操作下,然后再仔细想想,其实原理不是很难,很好理解,万变不离其宗!

     

    (3)like

            在Mysql中 like ‘%123%’在MySQLdb中等价与like ‘%%123%%‘,它用一个%对Mysql中的%进行转义。所以Mysql中 like ‘123%’在MySQLdb中等价与like ‘123%%;Mysql中 like ‘%123’在MySQLdb中等价与like ‘%%123’。

     

    (4)时间类型

    用‘%s‘进行转义

    """select * from table where create_time >= ‘%s‘ and create_time <= ‘%s‘"""%(date_begin,date_end,)

     

    转载于:https://blog.51cto.com/11026142/1863669

    Python:MySQLdb中sql语句'in'和‘like’要注意的一些问题

    标签:python   header   for   _id   mount   方式   etc   net   结果   

    人气教程排行