duanpu1111 2019-02-25 23:38
浏览 244
已采纳

如何将.txt文件导入到不同表中的mysql数据库中

I am interested to import this 5000 line .txt file into a mysql database.

 P|1|A PRODUCT|1|0002000|204|123|
 P|2|ANOTHER PRODUCT|10000371|0001990|055|031|
 B|055|A BRAND NAME|
 B|204|ANOTHER BRAND NAME|
 G|123|GROUP NAME|
 G|031|ANOTHER GROUP NAME|
 

Where P means that line refers to a "Product" table, B means "Brand" table and G means "Group". I need them separatedly in the database. If it's impossible to do it only with sql, there is some way i'll get the result I want using PHP?

</div>
  • 写回答

2条回答 默认 最新

  • dongquexi1990 2019-02-26 03:41
    关注

    the php explode will do the job for you, here is how

    $data = file_get_contents('data.txt')
    // make an array for each line
    $lines = explode('/n', $data)
    foreach ($lines as $line) {
    // Make array  for each part of the line
    $eline = explode('|', $line) ;
    // $eline[0] is first part that have b or g or p
    if ($eline[0] == 'B') {
    // the sql code for brands
    }
    }
    

    if you want i can complete the full code for you just tell me what the sql querys

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题