Python不提交插入查询

时间:2014-09-26 15:49:31

标签: python mysql wordpress

我正在尝试在wordpress上批量插入locations。我已经为checkadding术语和分类法定义了函数

def checkTerm(term,con):
    cur = con.cursor()
    query = "SELECT term_id FROM wp_terms as t WHERE t.name = '%s'" % term
    print query
    cur.execute(query)
    rows = cur.fetchall()
    if rows: return rows[0][0]
    else : return None

def addTerm(term,slug,con):

    cur = con.cursor()
    try:
        query = "INSERT INTO `wp_terms` (`name`,`slug`,`term_group`) VALUES ('%s','%s',0)" % (term,slug)
        print query
        cur.execute(query)
        con.commit()
        rows = checkTerm(term,con)
        if rows: return rows[0][0]
        else : return None

    except:
        return None

def checkTaxonomy(term_id,con):
    cur = con.cursor()
    query = "SELECT tt.term_taxonomy_id,tt.parent FROM wp_term_taxonomy AS tt INNER JOIN wp_terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'project_location' AND t.term_id = '%s'" % term_id
    print query
    cur.execute(query)
    rows = cur.fetchall()
    if rows: return rows
    else : return None


def addTaxonomy(term_id,taxonomy,description,parent,count,con):
    cur = con.cursor()
    query = "INSERT INTO `wp_term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('%s','%s','%s','%s','%s')" % (term_id,taxonomy,description,parent,count)
    print query
    cur.execute(query)
    con.commit()
    rows = checkTaxonomy(term_id,con)
    if rows: return rows
    else: return None

我在城市词典中存储城市

df = pd.read_table('./Argentina.csv',sep='\t',header=None,engine='python')
for line in xrange(len(df)):
    stringa = str(df[17][line])
    location = str(df[1][line])
    population = int(df[14][line])
    if population < limit_pop: continue

    string_state = stringa.split("/")
    country = string_state[1]
    state = string_state[2]

    if not country in states:
        states[country] = {}
    if not state in states[country]:
        states[country][state] = [location]
    else :
        states[country][state].append(location)

然后我尝试在wordpress db

中插入术语和分类法
con = mdb.connect('localhost', 'root', 'mypassword, 'Wordpress')


for country in states:
    country_id = checkTerm(country.replace("_"," "),con)
    if not country_id: 
        country_id = addTerm(country.replace("_"," "),country,con)

    taxonomy = checkTaxonomy(country_id,con)
    if not taxonomy: 
        taxonomy = addTaxonomy(country_id,'project_location','','0','0',con)
    parent = dict((y, x) for x, y in taxonomy)
    if not 0 in parent:
        taxonomy = addTaxonomy(country_id,'project_location','','0','0',con)


    for state in states[country]:

        state_id = checkTerm(state.replace("_"," "),con)
        if not state_id: 
            state_id = addTerm(state.replace("_"," "),state,con)
        taxonomy = checkTaxonomy(state_id,con)
        if not taxonomy: 
            taxonomy = addTaxonomy(state_id,'project_location','',country_id,'0',con)
        parent = dict((y, x) for x, y in taxonomy)

        if not country_id in parent:
            taxonomy = addTaxonomy(state_id,'project_location','',country_id,'0',con)

        for location in states[country][state]:
            location_id=checkTerm(location.replace("_"," "),con)
            if not location_id:
                location_id = addTerm(location.replace("_"," "),location,con)
            taxonomy = checkTaxonomy(location_id,con)
            if not taxonomy:
                taxonomy = addTaxonomy(location_id,'project_location','',state_id,'0',con)
            parent = dict((y, x) for x, y in taxonomy)
            if not state_id in parent:
                taxonomy = addTaxonomy(location_id,'project_location','',state_id,'0',con)

当我尝试执行脚本时,我发现了这种行为

SELECT term_id FROM wp_terms as t WHERE t.name = 'Argentina'
INSERT INTO `wp_terms` (`name`,`slug`,`term_group`) VALUES ('Argentina','Argentina',0)
SELECT term_id FROM wp_terms as t WHERE t.name = 'Argentina'
SELECT tt.term_taxonomy_id,tt.parent FROM wp_term_taxonomy AS tt INNER JOIN wp_terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'project_location' AND t.term_id = 'None'
INSERT INTO `wp_term_taxonomy` (`term_id`,`taxonomy`,`description`,`parent`,`count`) VALUES ('None','project_location','','0','0')
SELECT tt.term_taxonomy_id,tt.parent FROM wp_term_taxonomy AS tt INNER JOIN wp_terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = 'project_location' AND t.term_id = 'None'

脚本因以下错误而停止

./import.py:59: Warning: Truncated incorrect DOUBLE value: 'None'
  cur.execute(query)
./import.py:69: Warning: Incorrect integer value: 'None' for column 'term_id' at row 1
  cur.execute(query)
Traceback (most recent call last):
  File "./import.py", line 115, in <module>
     parent = dict((y, x) for x, y in taxonomy)
TypeError: 'NoneType' object is not iterable

这意味着不会执行insert语句。我不明白。我con.commit()查询但仍未执行。问题在哪里?

1 个答案:

答案 0 :(得分:0)

解决方案:

我改变了

import MySQLdb as mdb

import mysql.connector

con = mdb.connect(host='localhost',user='root', password='passowrd',database= 'Wordpress');

con = mysql.connector.connect(host='localhost',user='root', password='password',database= 'Wordpress',autocommit=False,buffered=False);