【问题标题】:How to insert large text into mysql database in android?如何在android中将大文本插入mysql数据库?
【发布时间】:2016-08-22 11:25:20
【问题描述】:

我有一个允许用户在编辑文本中插入 html 代码的应用程序。单击保存时,此 html 代码将作为字符串发送到 mysql 数据库并插入到文本字段中。以下代码将 html 内容插入到数据库中。

$instruction =$_POST["instruction"];
$plainHTML = str_replace(chr(194),"",$instruction);
$sql = "INSERT INTO rreadyreckoner (id, instruction)
VALUES (NULL, '$plainHTML')";

这里的问题是数据被保存,但是当我尝试检索数据时,如果添加了 mysqli_set_charset($con, "utf8"); 这一行,则“和 \u00a0 等特殊字符会作为响应。 我尝试使用此代码检索数据:-

$query ="SELECT instruction FROM `rreadyreckoner` ORDER BY id DESC LIMIT 1;";

$res = mysqli_query($con,$query);
$result = array();
while($row = mysqli_fetch_array($res))
{
                  array_push($result,
                  array('instruction'=>$row[0]));
}
if(!$result)
{
echo "Nothing to display";
}else
{
echo json_encode(array("result"=>$result));
}

这是我得到的实际响应。

{"result":[{"instruction":"<html>\n\n\u00a0 \u00a0<head><\/head>\n\n\u00a0 \u00a0<body>\n\n\u00a0 \u00a0 \u00a0 <ul>\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0<li>The Comprehensive R Archive Network<\/li>\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0<li
>A network of global web servers storing identical, up-to-date, versions of<br \/>code and documentation for R<\/li>\n\n\u00a0 \u00a0 \u00a0 <\/ul>\n\n\u00a0 \u00a0 \u00a0 <p><br \/><strong>Download and Install R:<\/strong><\/p>\n\n\u00a0 \
u00a0 \u00a0 <ul>\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0<li>Use the CRAN mirror nearest to you to download R setup at a faster<br \/>speed. Go to <a href=\"url\">\u00a0http:\/\/cran.r-project.org<\/a><\/li>\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u0
0a0<li>Select one of the three download links according to your machine.<\/li>\n\n\u00a0 \u00a0 \u00a0 <\/ul>\n\n\u00a0 \u00a0 \u00a0 <img src=\"file:\/\/\/storage\/emulated\/0\/rreadyreckoner_images\/download-r.png\" alt=\"downloadr\" widt
h=\"191\" height=\"129\" \/>\u00a0\n\n\u00a0 \u00a0 \u00a0 <ul>\n\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0<li>Run the R set up and follow the instructions of the installer.<\/li>\n\n\u00a0 \u00a0 \u00a0 <\/ul>\n\n\u00a0 \u00a0<\/body>\n\n<\/html
>\n\n"}]}

我应该怎么做才能将 html 内容保存为文本以及何时 检索它我可以在 webview 中显示它吗?

非常感谢任何帮助或建议。谢谢。

【问题讨论】:

  • 您不需要将 HTML 存储在数据库中,也不应该这样做。为什么不使用 HTML 模板,并在获得数据后用数据填充它们?
  • 您的 sql 查询(很容易)容易受到 sql 注入的影响。请告知自己准备好的陈述:php.net/manual/en/mysqli.quickstart.prepared-statements.php
  • 我的建议是使用文本数据类型而不是 varchar
  • @bub 我无法控制 html 内容,因此无法使用 html 模板。
  • @KarthiVenture 我使用的是文本数据类型。

标签: php android mysql sqlite


【解决方案1】:

谢谢@KarthiVenture 我首先添加了斜杠,然后使用instructions[i] = instructions[i].replaceAll("Â"," ");删除了我的java代码中的Â

【讨论】:

    【解决方案2】:

    在此处检查限制 https://www.sqlite.org/limits.html

    对于大文本,您可以使用 BLOB 类型而不是 TEXT 类型(但是您需要在插入/更新时将大文本转换为字节数组,并在从数据库加载时将字节数组转换回大文本)

    参考: http://www.sqlite.org/faq.html#q9

    【讨论】:

      猜你喜欢
      • 2016-11-23
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 2014-05-03
      • 2011-08-06
      • 2017-07-03
      • 2017-07-27
      相关资源
      最近更新 更多