python MySQLdb 参数化查询

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。

你可能感兴趣的:(python MySQLdb 参数化查询)