csv到多表插入更新

时间:2016-01-05 06:01:15

标签: python mysql csv

最后创建了帐户,因为这个不是: 我创建了一个这样的csv:

example :
sample.csv
jhon,28,botony
samuel,39,physics

database table

table USERS
columns: id<pk>, name, age

table COURSES
columns: id<pk>, name

table USER_COURSE
columns: id<pk>, user_id<F_key_users>, course_id<F_key_courses>

现在我必须导入此csv以及创建的运行时外键。 用100K记录实现这一目标的任何方法

<html>
<head>
    <title>Seamless Trial</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
    <style>
        img {
            padding:1px;
            border:1px solid black;
            background-color: white;
            width:100px;
            }
        article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; }
    </style>
    <script language="JavaScript">
        <!--
                // Copyright 2001 by www.CodeBelly.com Please do *not* remove this notice.

            var backImage = new Array(); // don't change this

                // Enter the image filenames you wish to use. Follow the pattern to use more images.  
                // The number in the brackets [] is the number you number in the brackets [] is the number you
                // will use in the function call to pick each image.
                // Note how backImage[0] = "" -- which would set the page to *no* background image.

            backImage[0] = "#";
            backImage[0].id = "blah";  
            backImage[1] = "#";
            backImage[1].id = "blah";  
            backImage[2] = "black-rough-leather.jpg";
            backImage[3] = "newspapers.jpg";
            backImage[4] = "seamless-fuzzy-leopard.jpg";

                // ----Do not edit below this line.---

            function changeBGImage(whichImage){
                if (document.body){
                document.body.background = backImage[whichImage];}
            }

        //-->
        <!-- input thingy   
            function readURL(input) {
                if (input.files && input.files[0]) {
                    var reader = new FileReader();

                    reader.onload = function (e) {
                    $('#blah')
                        .attr('src', e.target.result)
                        .width(150)
                        .height(200);
                    };
                reader.readAsDataURL(input.files[0]);
                }
            }
        // end input thingy --!>
    </script>
</head>
<body>
<input type='file' onchange="readURL(this);" /><br><br>
<a href="javascript:changeBGImage(1)"><img id="blah" src="#" alt="your image" /></a>

    <a href="javascript:changeBGImage(2)"><img src="black-rough-leather.jpg"></a><br><br>
    <a href="javascript:changeBGImage(3)"><img src="newspapers.jpg"></a><br><br>
    <a href="javascript:changeBGImage(4)"><img src="seamless-fuzzy-leopard.jpg"></a><br><br>
    <a href="javascript:changeBGImage(5)"><img src=""></a><br><br>

</body>

我必须在这些表中插入我的csv。

感谢任何人帮助我,我无法改变db。

1 个答案:

答案 0 :(得分:0)

对于用户: 主键由DB(或应该是)生成

用户:

LOAD DATA LOCAL INFILE 'users.csv'
INTO TABLE users
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(NAME, AGE);

课程:

LOAD DATA LOCAL INFILE 'course.csv'
INTO TABLE courses
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(NAME);

最后一个表的语法相同,只需更改表名和列名

对于您要跳过的列,请使用@dummy

所以按照这个顺序

jhon,28,botany

LOAD DATA LOCAL INFILE 'users.csv'
INTO TABLE users
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(NAME, AGE, @dummy);

LOAD DATA LOCAL INFILE 'course.csv'
INTO TABLE courses
FIELDS TERMINATED BY ',' ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS
(@dummy, @dummy, NAME);

您无法真正控制数据库中的PK,但您知道它总是会增加1.因此,基于此,您可以创建第三个表。

如果可以的话,我建议将差异csv中的课程分开,这样你只创建一门课程,然后在第三个表格中向学生介绍课程。