2013-07-25 4 views
0

저는 person, player, coach 및 team 테이블을 가진 데이터베이스를 가지고 있습니다. 모든 테이블에는 기본 키로 자동 증분 ID 필드가 있습니다. Person은 id, firstname, lastname을가집니다. 플레이어와 코치는 모두 id 필드와 person_id 및 team_id를 외래 키로 사용하여 다른 테이블의 team.id 또는 person.id 필드에 연결합니다. 나는 하나의 마스터 CSV 파일을 가지고 있는데, 그것으로부터 ID를 가진 MySql 다른 테이블의 모든 값을 가져오고 싶습니다. 그리고 데이터베이스의 값도 확인하고 싶습니다. 값이 데이터베이스에 있으면 해당 값을 가져 오지 마십시오. CSV 구문 분석 및 인덱싱 기능을 사용했습니다. 그러나 나는 그것을 할 수 없다. 하나는mysql에 csv 파일 임포팅

mysql> describe person; 
+-----------+-------------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+-----------+-------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| firstname | varchar(30) | NO |  | NULL |    | 
| lastname | varchar(30) | NO |  | NULL |    | 
+-----------+-------------+------+-----+---------+----------------+ 
mysql> describe player; 
+-----------+---------+------+-----+---------+----------------+ 
| Field  | Type | Null | Key | Default | Extra   | 
+-----------+---------+------+-----+---------+----------------+ 
| id  | int(11) | NO | PRI | NULL | auto_increment | 
| person_id | int(11) | NO | MUL | NULL |    | 
| team_id | int(11) | NO | MUL | NULL |    | 
+-----------+---------+------+-----+---------+----------------+ 
mysql> describe team; 
+-----------+-------------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+-----------+-------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| teamname | varchar(25) | NO |  | NULL |    | 
| location | varchar(40) | NO |  | NULL |    | 
| city  | varchar(25) | NO |  | NULL |    | 
| state  | varchar(2) | NO |  | NULL |    | 
| venue  | varchar(35) | NO |  | NULL |    | 
| league_id | int(11)  | NO | MUL | NULL |    | 
+-----------+-------------+------+-----+---------+----------------+ 

내 CSV 파일 아래에 그 내 SQL 테이블에서 나를 도울 수있는 것은 이름 성 teamname 위치 도시 국가입니다 | 장소
ABC CDF 인도의 CSV BNG의 KAR ABC

을 가져온 후

나는 사람, 선수, 코치 및 팀 테이블이있는 데이터베이스를 가지고 있습니다. 모든 테이블에는 기본 키로 자동 증분 ID 필드가 있습니다. Person은 id, firstname, lastname을가집니다. 플레이어와 코치는 모두 id 필드와 person_id 및 team_id를 외래 키로 사용하여 다른 테이블의 team.id 또는 person.id 필드에 연결합니다. 나는 하나의 마스터 CSV 파일을 가지고 있는데, 그것으로부터 ID를 가진 MySql 다른 테이블의 모든 값을 가져오고 싶습니다. 그리고 데이터베이스의 값도 확인하고 싶습니다. 값이 데이터베이스에 있으면 해당 값을 가져 오지 마십시오. CSV 구문 분석 및 인덱싱 기능을 사용했습니다. 그러나 나는 그것을 할 수 없다. 하나는

mysql> describe person; 

+-----------+-------------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+-----------+-------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| firstname | varchar(30) | NO |  | NULL |    | 
| lastname | varchar(30) | NO |  | NULL |    | 
+-----------+-------------+------+-----+---------+----------------+ 
mysql> describe player; 
+-----------+---------+------+-----+---------+----------------+ 
| Field  | Type | Null | Key | Default | Extra   | 
+-----------+---------+------+-----+---------+----------------+ 
| id  | int(11) | NO | PRI | NULL | auto_increment | 
| person_id | int(11) | NO | MUL | NULL |    | 
| team_id | int(11) | NO | MUL | NULL |    | 
+-----------+---------+------+-----+---------+----------------+ 
mysql> describe team; 
+-----------+-------------+------+-----+---------+----------------+ 
| Field  | Type  | Null | Key | Default | Extra   | 
+-----------+-------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| teamname | varchar(25) | NO |  | NULL |    | 
| location | varchar(40) | NO |  | NULL |    | 
| city  | varchar(25) | NO |  | NULL |    | 
| state  | varchar(2) | NO |  | NULL |    | 
| venue  | varchar(35) | NO |  | NULL |    | 
| league_id | int(11)  | NO | MUL | NULL |    | 
+-----------+-------------+------+-----+---------+----------------+ 

내 CSV 파일 아래에 그 내 SQL 테이블에서 나를 도울 수있는 것은

First Name Last Name teamname Location city state |venue 
abc cdf india csv bng kar abc 

좀 작은 코드

# 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"  
으로 노력하고

id First Name Last Name teamname Location city state |venue coment 
1 1 1 1 1 1 1 abc abc 

을 가져온 후입니다

+0

왜이 python에 태그가 지정되어 있습니까? – P0W

+0

이것을 파이썬으로 태그했지만 파이썬 코드는 포함하지 않았습니다. 당신이 시도하지 못한 것을 업데이트하십시오. –

답변

0

나는 하나의 마스터 csv 파일을 가지고 있는데, 그 중 모든 값을 가져 오기를 원한다. ID를 가진 MySql 다른 테이블.

가져 오기 루틴이 데이터를 저장할 위치를 알 수 없기 때문에 불가능합니다.

마스터 csv 파일 당신이 할 수 테이블 이름을 포함하는 열이 포함 된 경우

  • 해당 테이블로 데이터를 이동하는 임시
  • 사용하는 다른 SQL 문에 CSV 파일을 가져
+0

Bro 나는 몇 가지 일을했습니다. 파이썬 코드를 공유했습니다. 하나의 테이블 테이블에서 가져올 수 있습니다. 하지만 두 번째 국가 테이블로 이동하면 오류가 표시됩니다. 실행하면 내 코드가 표시됩니다. – Rohit

관련 문제