zoukankan      html  css  js  c++  java
  • csv impor export with mysql

    server-side:
    SELECT id,tutorialId,tutorialName,ucreatelink,structureVersion FROM base_course
    INTO OUTFILE '/tmp/orders.csv'
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY ' '

    client side:
    mysql -uucauser -p'Key!go003' -hucampus-prd-cluster.cluster-cmtmrzrxlu53.rds.cn-northwest-1.amazonaws.com.cn --database=ucampus --default-character-set=utf8mb4 --batch --raw < /tmp/demo_sql_query.sql > /tmp/demo_csv_export.tsv


    mysql -udemo_user -p -h127.0.0.1 --port=3306
    --default-character-set=utf8mb4 --database=demo_database
    --batch --raw < /tmp/demo_sql_query.sql > /tmp/demo_csv_export.tsv


    For completeness you could convert to csv (but be careful because tabs could be inside field values - e.g. text fields)

    tr ' ' ',' < file.tsv > file.csv

    If you have PHP set up on the server, you can use mysql2csv to export an (actually valid) CSV file for an abitrary mysql query. See my answer at MySQL - SELECT * INTO OUTFILE LOCAL ? for a little more context/info.

    I tried to maintain the option names from mysql so it should be sufficient to provide the --file and --query options:

    ./mysql2csv --file="/tmp/result.csv" --query='SELECT 1 as foo, 2 as bar;' --user="username" --password="password"
    "Install" mysql2csv via

    wget https://gist.githubusercontent.com/paslandau/37bf787eab1b84fc7ae679d1823cf401/raw/29a48bb0a43f6750858e1ddec054d3552f3cbc45/mysql2csv -O mysql2csv -q && (sha256sum mysql2csv | cmp <(echo "b109535b29733bd596ecc8608e008732e617e97906f119c66dd7cf6ab2865a65 mysql2csv") || (echo "ERROR comparing hash, Found:" ;sha256sum mysql2csv) ) && chmod +x mysql2csv


    server-side:
    LOAD DATA INFILE 'c:/tmp/discounts.csv'
    INTO TABLE discounts
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY ' '
    IGNORE 1 ROWS;

    local

    LOAD DATA LOCAL INFILE 'c:/tmp/discounts.csv'
    INTO TABLE discounts
    FIELDS TERMINATED BY ','
    ENCLOSED BY '"'
    LINES TERMINATED BY ' '
    IGNORE 1 ROWS;

  • 相关阅读:
    poj 3068 Bridge Across Islands
    XidianOJ 1086 Flappy v8
    XidianOJ 1036 分配宝藏
    XidianOJ 1090 爬树的V8
    XidianOJ 1088 AK后的V8
    XidianOJ 1062 Black King Bar
    XidianOJ 1091 看Dota视频的V8
    XidianOJ 1098 突击数论前的xry111
    XidianOJ 1019 自然数的秘密
    XidianOJ 1109 Too Naive
  • 原文地址:https://www.cnblogs.com/SZLLQ2000/p/10782203.html
Copyright © 2011-2022 走看看