无法插入数据库

时间:2013-06-25 11:33:59

标签: python mysql insert mysql-python

我想使用以下脚本将数据插入数据库。可以有3个供应商,每个供应商都需要存储仓库状态和邮政编码。

import MySQLdb as mdb
import sys,os
import logging
from logging.handlers import RotatingFileHandler
import time


WEBAPP_CONSTANTS = {
'LOGFILE': '/home/ai/Desktop/home/ubuntu/LCF/Database/db.log',
}
def getWebAppConstants(constant):
     return WEBAPP_CONSTANTS.get(constant, False)

LOGFILE = getWebAppConstants('LOGFILE')
log_handler = RotatingFileHandler(LOGFILE, maxBytes=1048576, backupCount=5)
log_handler.setFormatter(logging.Formatter( '%(asctime)s %(levelname)s: %(message)s ' '[in %(pathname)s:%(lineno)d]'))
applogger = logging.getLogger("DB")
applogger.setLevel(logging.DEBUG)
applogger.addHandler(log_handler)
applogger.debug("Starting of Database")
orig_stdout = sys.stdout
completeName = os.path.abspath("/home/ai/Desktop/home/ubuntu/LCF/Database/out.txt")
f = file(completeName, 'w')
sys.stdout = f


try:
    con = mdb.connect('*********', '********', '*******', '****')
    cur = con.cursor()
    con.autocommit(True)

except mdb.Error, e: 
    print "Error %d: %s" % (e.args[0],e.args[1])
    sys.exit(1)

def databaseinsert(id1,data):
    print "entered database insert with",id1,"and",data
    if id1=="1" or id1=="3":
        for i in data:
            print i
            print "entered with ",id1
            print i[0],i[1]
            d=i[0]
            d1=i[1]
            cur.execute("SELECT EXISTS(SELECT * FROM d_supplier_warehouse WHERE d_supplier_warehouse.state = %s AND d_supplier_warehouse.zipcode= %s) ",(d,d1))
            print "repeat check"
            rows = cur.fetchall()
            print "fetched rows"
            if rows[0][0]==1:
                print "same"
                continue
            else:
                print "successful entry"
                cur.execute("INSERT INTO d_supplier_warehouse (supplier_id,state,zipcode) Values(%s,%s,%s)",(id1,d,d1))
                print "completed"
    if id1=="2":
        print "entered with 2"
        d=data[0];d1=data[1]
        print d,d1
        cur.execute("SELECT EXISTS(SELECT * FROM d_supplier_warehouse WHERE d_supplier_warehouse.state = %s AND d_supplier_warehouse.zipcode= %s )",(d,d1))
        print "repeat check"
        rows = cur.fetchall()
        print "fetched rows"
        if rows[0][0]==1:
            print "same"
            pass
        else:
            print "successful entry"
            cur.execute("INSERT INTO d_supplier_warehouse (supplier_id,state,zipcode) Values(%s,%s,%s)",(id1,d0,d1))
            print "completed"   

我得到的out.txt是:

entered database insert with 1 and [('GA', '30024', '0', '77.3'), ('MI', '49544', '0', '77.3'), ('MA', '01801', '6', '77.3'), ('CA', '91749', '13', '77.3'), ('MD', '21076', '28', '77.3'), ('TN', '37086', '2', '77.3'), ('OH', '44087', '6', '77.3'), ('MN', '55121', '5', '77.3'), ('IL', '62246', '1', '77.3'), ('NY', '12051', '13', '77.3'), ('MO', '64116', '8', '77.3'), ('OR', '97203', '10', '77.3'), ('OK', '74116', '0', '77.3'), ('WA', '98188', '3', '77.3'), ('IL', '60188', '13', '77.3'), ('TX', '75061', '14', '77.3'), ('AZ', '85282', '0', '77.3'), ('FL', '33178', '1', '77.3'), ('OH', '43228', '10', '77.3'), ('IN', '46268', '0', '77.3'), ('TN', '38141', '8', '77.3'), ('TX', '77040', '5', '77.3'), ('CA', '95842', '9', '77.3'), ('UT', '84104', '0', '77.3'), ('NJ', '08512', '10', '77.3'), ('CO', '80238', '3', '77.3'), ('FL', '32819', '17', '77.3'), ('NC', '28278', '14', '77.3')]
('GA', '30024', '0', '77.3') 
entered with  1
GA 30024
entered database insert with 2 and ('MO', '63042', '11', '72.82')
entered with 2
MO 63042
entered database insert with 3 and [('CA', '94538', '1', '72.84'), ('GA', '30071', '1', '72.84'), ('TX', '75081', '6', '72.84'), ('IL', '60446', '3', '72.84'), ('MS', '38654', '2', '72.84'), ('NJ', '08831', '9', '72.84'), ('VA', '20151', '0', '72.84'), ('OR', '97008', '0', '72.84'), ('CA', '91761', '1', '72.84'), ('OH', '43123', '2', '72.84'), ('FL', '33182', '4', '72.84')]
('CA', '94538', '1', '72.84') 
entered with  3
CA 94538

清楚地表明代码不会完成。结果我的数据库没有被填满。

如何解决这个问题以及将数据推送到数据库背后的原因是什么?

0 个答案:

没有答案
相关问题