Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法插入到数据库中_Python_Mysql_Insert_Mysql Python - Fatal编程技术网

Python 无法插入到数据库中

Python 无法插入到数据库中,python,mysql,insert,mysql-python,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', }

我想使用下面的脚本将数据插入数据库。可以有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
这清楚地表明代码不会完成。结果我的数据库没有被填满


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

能否显示调用函数databaseinsert的代码部分,如果可能,请向我们提供您为此函数ID1提供的输入,数据是函数的输入,显示在out.txt中。但是,我不认为提供调用databaseinsert的代码部分会有任何帮助,因为代码库太大太复杂。查询中存在使用cur.executeSELECT exists从d_supplier_warehouse中选择*,其中d_supplier_warehouse.state=%s和d_supplier_warehouse.zipcode=%s,d,d1不是很清楚,因为您没有在函数中的任何位置使用此查询的输出。还有一件事,当这个查询失败时,您如何处理异常?我使用EXISTS查询来检查数据库中的唯一性。