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

Python 批量插入数据到Mysql

WBOY
Release: 2016-06-06 20:45:13
Original
2428 people have browsed it

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

示例数据:

<code class="lang-python">{
    '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}
        ]
    }
}
</code>
Copy after login
Copy after login

我写的处理:

<code class="lang-python">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)
</code>
Copy after login
Copy after login

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

回复内容:

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

示例数据:

<code class="lang-python">{
    '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}
        ]
    }
}
</code>
Copy after login
Copy after login

我写的处理:

<code class="lang-python">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)
</code>
Copy after login
Copy after login

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template