如何插入数据库 mysql。解决?

How to insert database mysql. Solve?

如何将文件数据插入 table mysql?

代码:

import pymysql.cursors
import pymysql as MySQLdb
import pymysql
from Bio import SeqIO

try:
    conexao = MySQLdb.connect(host="localhost",user="root",passwd="xxx",db="db_teste")
    print("conectado")
    print(conexao)
except:
    print("Não conectado")


for item in SeqIO.parse('seqteste.txt', 'fasta'):
    dados = print('>{}\t{}'.format(str(item.description).replace('|', '\t'), item.seq), )
    with conexao:
         with conexao.cursor() as cursor:
            sql =  "INSERT INTO `tabelateste` (`id`, `id_name`, `host`, `organism`, `seq`) VALUES(1, %s, %s, %s,%s, %s)"
            cursor.execute(sql, (dados, dados, dados, dados, dados,))
            conexao.commit()

错误:

pymysql.err.OperationalError: (1136, "Column count doesn't match value count at row 1")

我们的:

sql =  "INSERT INTO `tabelateste` (`id`, `id_name`, `host`, `organism`, `seq`) VALUES(%s, %s, %s, %s,%s, %s)"
            cursor.execute(sql, (dados, dados, dados, dados, dados))
            conexao.commit()

error:

    line 125, in mogrify
        query = query % self._escape_args(args, conn)
    TypeError: not enough arguments for format string

如何解决这个错误并能够将数据插入 mysqldb table?

myseq:

>gb:KX262887|Organism:Zika virus|Strain Name:103451|Segment:null|Subtype:Asian|Host:Human
GTTGTTGATCTGTGTGAATCAGACTGCGACAGTTCGAGTTTGAAGCGAAAGCTAGCAACAGTATCAACAG
GTTTTATTTTGGATTTGGAAACGAGAGTTTCTGGTCATGAAAAACCCAAAAAAGAAATCCGGAGGATTCC

>gb:KX262887|Organism:Zika virus|Strain Name:103451|Segment:null|Subtype:Asian|Host:Human
    GTTGTTGATCTGTGTGAATCAGACTGCGACAGTTCGAGTTTGAAGCGAAAGCTAGCAACAGTATCAACAG
    GTTTTATTTTGGATTTGGAAACGAGAGTTTCTGGTCATGAAAAACCCAAAAAAGAAATCCGGAGGATTCC

>gb:KX262887|Organism:Zika virus|Strain Name:103451|Segment:null|Subtype:Asian|Host:Human
    GTTGTTGATCTGTGTGAATCAGACTGCGACAGTTCGAGTTTGAAGCGAAAGCTAGCAACAGTATCAACAG
    GTTTTATTTTGGATTTGGAAACGAGAGTTTCTGGTCATGAAAAACCCAAAAAAGAAATCCGGAGGATTCC

预期退出输出:

mysqldb

id  id_name            host        organism         seq
1   gb:KX262887        Human       Zika Virus       aatgtgttt

解决了吗?

您必须仔细匹配列、占位符和值,以便每项都有 相同 个数:

sql =  "INSERT INTO `tabelateste` (`id`, `id_name`, `host`, `organism`, `seq`) VALUES(%s, %s, %s, %s, %s)"
            cursor.execute(sql, (dados, dados, dados, dados, dados))
            conexao.commit()

注意元组中现在如何指定 5 列、5 个占位符和 5 个绑定。你有一个额外的 %s.

概念上你想做的是:

(column_name, ...) <-- Columns specified
     |
     v
(   %s      , ... ) <-- Placeholders specified
     |
     v
(bind_value , ... ) <-- Binding on execute()

请注意,这些必须 1:1:1 完全相关 。任何不匹配都会导致如您所见的错误。

更改:

for item in SeqIO.parse('seqteste.txt', 'fasta'):
    dados = print('>{}\t{}'.format(str(item.description).replace('|', '\t'), item.seq), )

进入

import re

cnt = 0
for item in SeqIO.parse('seqteste.txt', 'fasta'):
    cnt += 1
    s=(str((item.description).strip('')))
    pattern_id = "(.*?)\|Organism"
    pattern_host = "Host:(.*.)"
    pattern_org = "Organism:(.*?)\|Strain"
    ids= re.search(pattern_id, s).group(1)
    host = re.search(pattern_host, s).group(1)
    org = re.search(pattern_org, s).group(1)
    dados = [str(cnt), ids ,host , org   ,str(item.seq)]

和:

 cursor.execute(sql, (dados, dados, dados, dados, dados))

与:

cursor.execute(sql, (dados[0] , dados[1] ,dados[2] ,dados[3] ,dados[4]))

让我知道它的行为方式,我没有你的数据库,无法测试我的代码