How to insert sequence fasta data into a mysql database?
0
0
Entering edit mode
7 months ago
USER • 0

I would like to insert the data of the fasta sequence in a mysql database.

But my code doesn't work and I don't know what I might be doing wrong

I can insert the data by pasting the data for each line. but when I put the for it doesn't work

mysql biopython pymysql python3 • 424 views
ADD COMMENT
0
Entering edit mode

If you failed to connect in your try you will fall into the except where conexao does not exist.

Try to put a sys.exit(1) after print("Not conect") in your except statement, the script will stop if you are unable to connect.

ADD REPLY
0
Entering edit mode

I can make the connection the error is here

line 16, in <module>
        with conexao:
NameError: name 'conexao' is not defined

here:

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(%s, %s, %s, %s,%s)"
            cursor.execute(sql)
            conexao.commit()
ADD REPLY
0
Entering edit mode

All right, do you have these prints results in your terminal ?

print("conect")
print(conexao)
ADD REPLY
0
Entering edit mode

error:

pymysql.err.IntegrityError: (1048, "Column 'id' cannot be null")

pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s, %s, %s,%s)' at line 1")
ADD REPLY
0
Entering edit mode

Can you try running this :

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

conexao=""

try:
    conexao = MySQLdb.connect(host="localhost",user="root",passwd="xxx",db="db_teste")
    print("conect")
    print(conexao)
except:
    print("Not conect")

print(conexao)

for item in SeqIO.parse('seqteste.txt', 'fasta'):
    print(item)
    with conexao:
         with conexao.cursor() as cursor:
            sql = "INSERT INTO tabelateste(id,id_name, host,organism, seq) values(%s, %s, %s, %s,%s)"
            cursor.execute(sql)
            conexao.commit()
ADD REPLY

Login before adding your answer.

Traffic: 2327 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6