2014-09-26 52 views
0

我試圖在wordpress上批量插入locations。我已經定義函數checkadding術語和分類Python不插入查詢

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 

我保存城市dicionaries的字典

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分貝

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語句不執行。我不明白。我查詢,但它仍然沒有執行。哪裏有問題?

回答

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);