如何在android中将大文本插入mysql数据库?

时间:2016-08-22 11:25:20

标签: php android mysql sqlite

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

$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中显示它吗?

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-1)

在这里检查限制 https://www.sqlite.org/limits.html

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

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