python MySQLdb 参数化查询

2018-07-10  本文已影响0人  duandaoke

Python MySQLdb 做参数化查询:

some_dictionary_with_the_data =  {  'name':  'awesome song',  'artist':  'some band', etc...  } 
cursor.execute ("""
            INSERT INTO Songs (SongName, SongArtist, SongAlbum, SongGenre, SongLength, SongLocation)
            VALUES
                (%(name)s, %(artist)s, %(album)s, %(genre)s, %(length)s, %(location)s)
        """, some_dictionary_with_the_data)

注意execute方法的第一个参数是一个格式化字符串,这个字符串的 %(name)s是一个参数,这个参数的格式类似于python字符串的格式化,在(name)中是第二个参数dict的key。

上一篇 下一篇

猜你喜欢

热点阅读