0

個人、選手、コーチ、チームのテーブルを含むデータベースがあります。すべてのテーブルには、主キーとして自動インクリメント ID フィールドがあります。人にはID、名、姓があります。Player と Coach の両方に id フィールドと、他のテーブルの team.id または person.id フィールドに関連付けるための外部キーとして person_id と team_id があります。私は 1 つのマスター csv ファイルを持っています。そこから、ID を持つ MySql の異なるテーブルのすべての値をインポートします。また、データベースでも値を確認したいと思います。値がデータベースにある場合は、その値をインポートしないでください。CSV の解析とインデックス作成機能を使用しました。しかし、私にはそれができません。以下の私のSQLテーブルで誰かが私を助けることができますか

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ファイルはFirst Name Last Name teamname Location city state |venue
abc cdf india csv bng kar abcです

インポート後

個人、選手、コーチ、チームのテーブルを含むデータベースがあります。すべてのテーブルには、主キーとして自動インクリメント ID フィールドがあります。人にはID、名、姓があります。Player と Coach の両方に id フィールドと、他のテーブルの team.id または person.id フィールドに関連付けるための外部キーとして person_id と team_id があります。私は 1 つのマスター csv ファイルを持っています。そこから、ID を持つ MySql の異なるテーブルのすべての値をインポートします。また、データベースでも値を確認したいと思います。値がデータベースにある場合は、その値をインポートしないでください。CSV の解析とインデックス作成機能を使用しました。しかし、私にはそれができません。以下の私のSQLテーブルで誰かが私を助けることができますか

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ファイルは

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

インポート後

id First Name   Last Name   teamname    Location    city    state   |venue  coment
1   1   1   1   1   1   1 abc 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"    
4

1 に答える 1

0

私は 1 つのマスター csv ファイルを持っています。そこから、ID を持つ MySql の異なるテーブルのすべての値をインポートします。

インポート ルーチンはデータをどこに配置するかを認識していないため、これは不可能です。

マスターcsvファイルにテーブル名を含む列が含まれている場合、次のことができます

  • csvファイルを一時的にインポートします
  • さまざまなSQLステートメントを使用して、データを適切なテーブルに移動します
于 2013-07-25T07:22:57.287 に答える