【问题标题】:How do insert a file like this into a MySQL Database如何将这样的文件插入 MySQL 数据库
【发布时间】:2012-06-26 06:05:11
【问题描述】:

这是我试图存储在数据库中的文件。我想存储标题和 cmets。我确实得到了它,但它只存储第一行。 请帮忙,对数据库不太好。

title : The Descent Part 2 comment : badbaz83 : No it wasn't as good as the first one, but a hell of a lot better than I first feared. Worth a watch, but don't expect it to stand up to the first film.

title : Supernatural comment : MissAyla : I loved this episode. :) I'm thoroughly enjoying the new style, just like the original episodes.

title : Soccer Mom comment : chantellel93 : it's only 10 min so if u smoke 1 its worth the laugh 2/5. xx

【问题讨论】:

  • 它是否遍历所有行?有没有抛出异常?
  • 解析纯 txt 文件会是*ss 的痛苦...你考虑过使用 XML 文件吗?
  • 你在哪里设置id?如果id 是主键,它必须是唯一的。如果你想自动增加 id 你不需要在你的插入语句中提供它
  • @mj_ 没有抛出异常。它只在第一行读取并停止。
  • 很抱歉,我对此很固执,但是“停止”是什么意思?程序完全崩溃了?我们可以注释掉数据库插入行,让它完全运行并确保其他一切正常。 SQL 本身看起来还不错。你能在 MySQL admin/developer 应用程序中尝试一下吗(我不记得它叫什么了)。

标签: java mysql database jdbc text-files


【解决方案1】:

您首先需要将line 拆分为TitleComment 并分配一个id。试试这个(注意区分大小写):

String s = line.toString();
Title = s.substring(8, s.indexOf("comment :")).trim();
Comment = s.substring(s.indexOf("comment :") + 10, s.length()).trim();

然后告诉我们你要去哪里。

我假设您正在尝试执行此数据库插入 (SQLFiddle )。 要自动分配一个 id,您可以将 id 设置为您的 auto_increment 主键(根据我的示例)。然后你可以使用executeUpdate(String sql, int autoGeneratedKeys)

当您提供问题的更多详细信息时,我会尝试更新我的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-03
    • 2021-03-15
    • 1970-01-01
    • 2013-03-07
    • 2012-12-07
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多