views:

13

answers:

1

i use a sqlalchemy as my orm,i do a mysqlimport cmd through subprocess,and before & after the execution i query the db records,which i use statistics_db method, but the records count results after import from csv didn't increase,i think this is a sqlalchemy problem,how to slove this?,thanks

def statistics_db(f):
    @wraps(f)
    def wrapper(*args,**args2):
        q=Product.query.filter("site='%s'"%g_site_show_name)
        pre=q.count()
        print "当前数据库中共有%s条关于%s的记录"%(q.count(),g_site_show_name)
        time.sleep(3)
        back = f(*args, **args2)
        post=q.count()
        count=pre-post
        description="减少" if count>0 else "增加"
        print "当前数据库中共有%s条关于%s的记录"%(q.count(),g_site_show_name)
        print "%s了%s条记录"%(description,abs(count))
        return back
    return wrapper

@cost_time
@statistics_db
def batch_csv2db():
    "将csv文件中的数据批量导入到数据库中"
    if not os.path.exists(g_xml2csv_file):
        print "找到压缩文件,从中读取csv文件"
        dirname,filename=os.path.split(src)
        base,ext=os.path.splitext(filename)
        tar_filepath=os.path.join(dirname,base+".tar.gz")
        uncompress(tar_filepath)
    if not os.path.exists(g_xml2csv_file):
        print "未找到可用的csv文件!"
        return
    cmd="""mysqlimport -u root --password='bin' -L -v --columns='name,price,productid,site,link,smallImage,bigImage,description,createdOn,modifiedOn,size,weight,wrap,material,packagingCount,stock,location,popularity,inStock,categories' --fields-terminated-by=',' --fields-enclosed-by='"' --fields-escaped-by='\\' --lines-terminated-by='\n' search """+g_xml2csv_file
    args=shlex.split(cmd)
    p=subprocess.Popen(args)
    stdout, stderr = p.communicate()
    print stdout,stderr
A: 

You have to either call commit()/rollback() to end transaction after calling count() or change isolation level. Otherwise you won't see changes made from other connections.

Denis Otkidach