2013-07-15 97 views
-4

我在我的sql中使用csv解析導入csv文件。我在Mysql中有幾張表。我想在所有表中導入csv文件一次。所以我使用下面的腳本。在那個國家能夠導入,但狀態表中沒有顯示它我下面的錯誤從腳本導入Mysql

csv_spliter.py:74: Warning: Incorrect integer value: ''country_id'' for column 'id' at row 1 
    row) 
csv_spliter.py:74: Warning: Incorrect integer value: ''0'' for column 'id' at row 1 
    row) 
csv_spliter.py:74: Warning: Incorrect integer value: ''1'' for column 'id' at row 1 
    row) 
csv_spliter.py:74: Warning: Incorrect integer value: ''2'' for column 'id' at row 1 
    row) 
csv_spliter.py:74: Warning: Incorrect integer value: ''3'' for column 'id' at row 1 
    row) 
Done 
Traceback (most recent call last): 
    File "csv_spliter.py", line 89, in <module> 
    row) 
    File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 174, in execute 
    self.errorhandler(self, exc, value) 
    File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler 
    raise errorclass, errorvalue 
_mysql_exceptions.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`lcm`.`state`, CONSTRAINT `state_ibfk_1` FOREIGN KEY (`country`) REFERENCES `country` (`id`) ON DELETE CASCADE)') 
[email protected]:~/Desktop$ 


     <i>  

     # initialize with empty ints and dicts 
     name,cities,countries,states=[],[],[],[] 

     with open('ind.csv','rb') as csvfile: 
      reader = csv.reader(csvfile, delimiter=',') 
      reader.next() #skip header 
      for row in reader: 
       name.append(row[0]) 
       cities.append(row[2]) 
       states.append(row[3]) 
       countries.append(row[4]) 
     cl = list(set(countries)) 
     sl = list(set(states)) 
     citl = list(set(cities)) 
     inf1 = list(set(name)) 



     with open('countries.csv','w') as cfile: 
      writer = csv.writer(cfile, delimiter=',') 
      writer.writerow(['country_id','name']) 
      for i,x in enumerate(cl): 
       writer.writerow([i,x]) 

     with open('state.csv','w') as cfile: 
      writer = csv.writer(cfile, delimiter=',') 
      writer.writerow(['state_id','country_id','state']) 
      for i,x in enumerate(sl): 
       writer.writerow([i,x,cl.index(countries[states.index(x)])]) 

     with open('cities.csv','w') as cfile: 
      writer = csv.writer(cfile,delimiter=',') 
      writer.writerow(['city_id','city','st_id','country_id']) 
      for i,x in enumerate(citl): 
       writer.writerow([i,x,sl.index(states[cities.index(x)]), 
           cl.index(countries[cities.index(x)]) 
           ]) 


     with open('inf123.csv','w') as cfile: 
      writer = csv.writer(cfile,delimiter=',') 
      writer.writerow(['Name_id', 'Name','city_id','st_id','country_id']) 
      for i,x in enumerate(inf1): 
       writer.writerow([i,x, 
           citl.index(cities[name.index(x)]), 
           sl.index(states[name.index(x)]), 
           cl.index(countries[name.index(x)]) 

           ]) 

     import MySQLdb 
     import csv 
     mydb = MySQLdb.connect(host="localhost", # The Host 
     user="root", # username 
     passwd="root", # password 
     db="abcm") # name of the data base 


     cursor = mydb.cursor() 

     csv_data = csv.reader(file('countries.csv')) 
     for row in csv_data: 

      cursor.execute('INSERT INTO country(id, \ 
        name)' \ 
        'VALUES("%s", "%s")', 
        row) 
     #close the connection to the database. 
     mydb.commit() 
     cursor.close() 
     print "Done" 


     cursor = mydb.cursor() 

     csv_data = csv.reader(file('state.csv')) 
     for row in csv_data: 

      cursor.execute('INSERT INTO state(id, \ 
        country, name)' \ 
        'VALUES("%s", "%s", "%s")', 
        row) 
     #close the connection to the database. 
     mydb.commit() 
     cursor.close() 
     print "Done" 
     </i> 

任何一個可以給我一些意見,我該怎麼辦呢?

回答

0

在將數據轉儲到數據庫之前,您是否將字符串轉換爲整數?

我認爲這就是Incorrect integer value錯誤信息的來源。

+0

我也採取了整數值。但是我會顯示同樣的錯誤 – Rohit