Home  >  Article  >  Backend Development  >  Python 批量插入数据到Mysql

Python 批量插入数据到Mysql

WBOY
WBOYOriginal
2016-06-06 20:45:132307browse

我用python弄了半天没搞定,高手来看下,我使用的是cursor.executemany来实现插入

示例数据:

{
    'info': {
        'files': [
            {'path': ['Runner Runner [2013]HDRip XviD-SaM[ETRG].avi'], 'length': 733460868}, 
            {'path': ['SaMple.avi'], 'length': 5577446}, 
            {'path': ['ETRG.mp4'], 'length': 1515390}, 
            {'path': ['RUNNERR-ETRG.nfo'], 'length': 7806}, 
            {'path': ['Torrent Downloaded From ExtraTorrent.cc.txt'], 'length': 367}
        ]
    }
}

我写的处理:

values = []
for i in data['info']['files']:
    values.append(i['path'][0])   
    cur.executemany('INSERT INTO `btss`.`magnetic_file` (`Uuid`, `Hash`, `File`, `Size`) VALUES (NULL, \'%s\', \'%s\', \'%s\')', values)

我这样用是不对的,但是append只能一个参数,请教该如何处理
新手哈,刚开始学

回复内容:

我用python弄了半天没搞定,高手来看下,我使用的是cursor.executemany来实现插入

示例数据:

{
    'info': {
        'files': [
            {'path': ['Runner Runner [2013]HDRip XviD-SaM[ETRG].avi'], 'length': 733460868}, 
            {'path': ['SaMple.avi'], 'length': 5577446}, 
            {'path': ['ETRG.mp4'], 'length': 1515390}, 
            {'path': ['RUNNERR-ETRG.nfo'], 'length': 7806}, 
            {'path': ['Torrent Downloaded From ExtraTorrent.cc.txt'], 'length': 367}
        ]
    }
}

我写的处理:

values = []
for i in data['info']['files']:
    values.append(i['path'][0])   
    cur.executemany('INSERT INTO `btss`.`magnetic_file` (`Uuid`, `Hash`, `File`, `Size`) VALUES (NULL, \'%s\', \'%s\', \'%s\')', values)

我这样用是不对的,但是append只能一个参数,请教该如何处理
新手哈,刚开始学

多append几次 - -。也可以不用append。
另外values放在for里面声明

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn