doujuan2688 2015-08-04 15:42
浏览 22
已采纳

根据文本文件调整MySQL数据库

I'm doing a movie database project to learn MySQL and also PHP. So far, I've got a Python script to search a folder and see if a file was added or removed. If it has detected that a file was added or removed, it will run another Python script that gets the name of the movie file and then searches OMDb and returns certain values (like title, year, plot, rating, etc). This information is then stored into a file for MySQL to use.

The file is formatted like

"\N Title Year Plot Rating" (tabs between each)

In my PHP file I have this code:

if (!mysqli_query($con, "LOAD DATA LOCAL INFILE '/home/user/movies.txt' INTO TABLE films")) {
    printf("Errormessage: %s
", mysqli_error($con));
}

$result = mysqli_query($con, "SELECT title, yr, genre, plot, rating FROM films") or die("Unable to query.");

And then after that I have a loop which echoes some HTML to produce a table.

This all works fine. When I add a file, Python detects it, gets the movie info from the internet and adds the info to a file, and then PHP loads that information to a MySQL database and then displays it.

However, when I remove a file from the directory that Python looks in, the file for the MySQL db is updated, but MySQL still has the removed movie/file in the database.

How could I get MySQL (through PHP) to load the data from the file, but replacing it instead of appending it?

  • 写回答

1条回答 默认 最新

  • dongmaopan5738 2015-08-04 15:44
    关注

    If you want the table to be completely identical to the input file, the simplest solution is to TRUNCATE it beforehand (assuming there are no foreign keys pointing to the rows).

    TRUNCATE will wipe the table clean, after which the LOAD DATA command will insert the fresh, new rows.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器