2016-06-21 9 views
0

Ich versuche, einige Daten in eine Datenbank einfügen, die ich mache und es wird nicht eingefügt. Ich habe buchstäblich die gleiche Einfügemethode für anderen Code verwendet, und es scheint immer noch zu funktionieren, aber dieser lehnt es ab. Hilfe!Python MYSql.connector wird keine Daten einfügen

from Bio import Entrez 
from sys import exit 
Entrez.email = "[email protected]"  # Always tell NCBI who you are 
sranumber = raw_input("Input SRA number here") 
sranumber2= raw_input("re-type SRA number here") 
while True: 
    if sranumber != sranumber2: 
     print "SRA numbers do not match" 
     sranumber2 = raw_input("Please re-type sra number to match intitial sra number") 
     continue 
    else: 
     break 
print "SRA ID:" + sranumber 
#answer = raw_input("Are you sure this is the sra number you wish to use? Type Y/N") 
while True: 
    answer = raw_input("Are you sure this is the sra number you wish to use? Type Y/N") 
    if answer == "Y": 
     print "Let's do it!" 
     break 
    elif answer == "y": 
     print "Let's do it!" 
     break 
    elif answer == "yes": 
     print "Let's do it!" 
     break 
    elif answer == "YES": 
     print "Let's do it!" 
     break 
    elif answer == "N": 
     exit() 
    else: 
     print "Not a valid answer" 
search = Entrez.esearch(term = sranumber, db = "SRA", retmode = "xml") 
record = Entrez.read(search, validate = False) 
newstuff = record 
#print newstuff 
for j in record: 
    if j == "WarningList": 
     newstuff = record['WarningList'] 
#print newstuff 
poop = newstuff 
for item in poop: 
    if item == "OutputMessage": 
     poop = poop['OutputMessage'] 
#print poop 
crap = ['Wrong UID' + " " + sranumber] 
cool = "'"+crap[0]+"'" 
#print cool 
continuity = '' 
for j in poop: 
    if j == 'No items found.' or j == cool: 
     print "[-] This is not a valid SRA identity" 
     continuity = 'done' 
if continuity == 'done': 
    exit() 
print "[+] This is a valid SRA identity" 
print "SRA ID:" + sranumber 


condition = raw_input("Type in the condition of your ngs_data here") 
condition2 = raw_input("re-type the condition of your ngs_data here") 
print condition 
while True: 
    if condition != condition2: 
     print "Conditions do not match!" 
     condition2 = raw_input("Please retype condition here to match first condition") 
    else: 
     break 
print "just dropped in to check on what condition my condition was in" 
stuff = [] 
stuff.append(sranumber) 
stuff.append(condition) 
stuff2 = '+'.join(stuff) 
print stuff2 
stuff3 = stuff2.split('+') 
print stuff3 
experiment = [tuple(stuff3)] 
print experiment 
from mysql.connector import MySQLConnection, Error 
from python_mysql_dbconfig import read_db_config 

def insert_books(experiment): 
    query = "INSERT IGNORE INTO organisms(sra#, condition) " \ 
       "VALUES(%s,%s)" 

    try: 
     db_config = read_db_config() 
     conn = MySQLConnection(**db_config) 

     cursor = conn.cursor() 
     cursor.executemany(query, experiment) 

     conn.commit() 
    except Error as e: 
     print('Error:', e) 

    finally: 
     cursor.close() 
     conn.close() 

def main(): 

    insert_books(experiment) 

if __name__ == '__main__': 
     main() 
+1

Erhalten Sie irgendwelche Fehler? Was wäre, wenn Sie 'INSERT INTO' anstelle von' INSERT IGNORE INTO' verwenden würden? – alecxe

+0

('Fehler:', ProgrammingError()) Dies ist der einzige Fehler, den ich bekomme. und die Einfügung hat das gleiche Problem wie die Einfügung ignorieren. Außerdem gibt es in dieser Tabelle keine Fremdschlüssel, aber auf ein Attribut wird als Fremdschlüssel verwiesen. – george135

Antwort

0

Ich landete nur auf diese Weise und es hat endlich funktioniert. Ich bin mir nicht sicher, warum es vorher nicht funktioniert hat, aber ich glaube, dass es etwas mit der Formatierung der Spalteneingaben zu tun hatte.

from mysql.connector import MySQLConnection, Error 
from python_mysql_dbconfig import read_db_config 

cnx = mysql.connector.connect(user='root',password='*****',database = 'new') 
cursor = cnx.cursor() 
addstuff= ("INSERT IGNORE INTO experiment (`sra`, `condition`) VALUES(%s,%s)") 
cursor.execute(addstuff,stuff3) 
cnx.commit() 
cursor.close() 
cnx.close()