zhyiwww
          用平實(shí)的筆,記錄編程路上的點(diǎn)點(diǎn)滴滴………
          posts - 536,comments - 394,trackbacks - 0
          功能:把csv里面的數(shù)據(jù)導(dǎo)入到postgres數(shù)據(jù)庫。

          實(shí)現(xiàn)步驟:

          (1)查看數(shù)據(jù),取得所有的字段名稱

          "ID","CALLID","CALLERID","CALLEDID","LOGTIME","ACTION_TYPE","ACTION_DES"
          5829,76881,cccccc,tttt,2009年07月13日09時(shí)57分34秒,"bbbbb",
          5830,76881,cccccc,tttt,2009年07月13日09時(shí)57分34秒,"aaaaa","1\116.kkkk"
          5831,76881,cccccc,tttt,2009年07月13日09時(shí)57分43秒,"aaaaa","1\142.kkkk"
          5832,76881,cccccc,tttt,2009年07月13日09時(shí)57分46秒,"aaaaa","1\141.kkkk"
          5833,76881,cccccc,tttt,2009年07月13日09時(shí)57分57秒,"aaaaa","1\116.kkkk"
          5834,76881,cccccc,tttt,2009年07月13日09時(shí)58分07秒,"aaaaa","1\142.kkkk"

          通過上面的字段名稱和下面的數(shù)據(jù),可以知道數(shù)據(jù)的字段名稱和數(shù)據(jù)類型。

          (2)根據(jù)字段名稱和類型,寫出創(chuàng)建表的腳本

          drop table? aa_log ;

          create table aa_log
          (
          ??? id int8,
          ??? callid int8,
          ??? callerid int8,
          ??? calledid int8,
          ??? logtime varchar(100),
          ??? action_type varchar(100),
          ??? action_des varchar(100)
          );

          腳本保存為/tmp/a.sql

          (3)創(chuàng)建數(shù)據(jù)表

          ?$ psql -hlocalhost -U postgres -d logdb? -f? /tmp/a.sql

          執(zhí)行,創(chuàng)建數(shù)據(jù)表。

          (4)修改數(shù)據(jù)
          ? 上面的數(shù)據(jù),字符串類型有"aaaaa" ,那么導(dǎo)入到數(shù)據(jù)庫的時(shí)候,對應(yīng)的值也會(huì)有符號"aaaaa",所以,其值實(shí)際是""aaaaa"",為了避免這樣,我們要先把數(shù)據(jù)里面的"去掉。
          在vim里面,用%s/“//g把所有的"符號替換掉。

          5829,76881,cccccc,tttt,2009年07月13日09時(shí)57分34秒,bbbbb,
          5830,76881,cccccc,tttt,2009年07月13日09時(shí)57分34秒,aaaaa,1\116.kkkk
          5831,76881,cccccc,tttt,2009年07月13日09時(shí)57分43秒,aaaaa,1\142.kkkk
          5832,76881,cccccc,tttt,2009年07月13日09時(shí)57分46秒,aaaaa,1\141.kkkk
          5833,76881,cccccc,tttt,2009年07月13日09時(shí)57分57秒,aaaaa,1\116.kkkk
          5834,76881,cccccc,tttt,2009年07月13日09時(shí)58分07秒,aaaaa,1\142.kkkk

          (5)刪除title

          把數(shù)據(jù)最上面的那行字段定義數(shù)據(jù)刪除掉。否則會(huì)出現(xiàn)導(dǎo)入數(shù)據(jù)庫錯(cuò)誤。


          (6)導(dǎo)入數(shù)據(jù)
          logdb=# copy aa_log? from '/tmp/aa.csv' with delimiter ',';
          COPY 273
          logdb=#

          (7)檢查數(shù)據(jù)

          至此,導(dǎo)入數(shù)據(jù)處理完畢。





          |----------------------------------------------------------------------------------------|
                                     版權(quán)聲明  版權(quán)所有 @zhyiwww
                      引用請注明來源 http://www.aygfsteel.com/zhyiwww   
          |----------------------------------------------------------------------------------------|
          posted on 2009-07-15 14:14 zhyiwww 閱讀(3315) 評論(1)  編輯  收藏 所屬分類: databasePostgres

          FeedBack:
          # re: 從csv格式導(dǎo)入數(shù)據(jù)到postgres
          2009-09-21 09:36 | Trader
          謝謝。  回復(fù)  更多評論
            
          主站蜘蛛池模板: 新闻| 九龙城区| 金平| 沧州市| 会昌县| 城口县| 大城县| 集安市| 清新县| 友谊县| 西安市| 三台县| 普定县| 青铜峡市| 潢川县| 江西省| 凌海市| 山丹县| 衡阳市| 克东县| 尼勒克县| 博客| 彭阳县| 虞城县| 东台市| 万全县| 清新县| 门源| 厦门市| 陆河县| 陵川县| 益阳市| 淮北市| 咸宁市| 昭平县| 上思县| 辰溪县| 潮安县| 夏河县| 广南县| 贞丰县|