使用Base64和远程查询进入远程数据库的问题..(较大图像的等待时间较长???)

时间:2011-01-31 19:30:13

标签: sql mysql sqlite base64

我有5Kb的图像,当我将其转换为Base64字符串并且我上传到我的远程数据库时,远程INSERT查询只需要几秒钟

但是..我有100Kb的图像,当我将其转换为Base64字符串并且我上传到我的远程数据库时,远程INSERT查询需要很多秒才能执行

为什么?

这是因为Base64字符串需要100KB的空间,就像非编码图像一样?

有办法解决这些等待时间吗?

更多信息:即时通讯使用PHP + JSOn连接到mysql远程数据库。

Oded使我不使用Base64并使用BLOB而不是LONGTEXT。 但....¿如何在JSON + PHP中使用BLOB?我不知道据我所知,JSON + PHP需要接收和发送字符串,而BLOB不是字符串

感谢

编辑2:

这是需要等待一段时间的代码(它在行中等待:while ((line = reader.readLine()) != null) {,等待reader.readLine()

此代码从远程数据库获取一个用户,需要花费一些时间在我的应用上显示用户

public Friend RetrieveOneUser(String email)
{

    Friend friend=null;

    String result = "";
    //the parameter data to send
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("email",email));

    //http post
    InputStream is=null;
    try{
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(this.BaseURL + this.GetOneUser_URL);
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    }catch(Exception e){
            Log.e("log_tag", "Error in http connection "+e.toString());
    }
    //convert response to string
    try{

            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
            }
            is.close();

            result=sb.toString();
    }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
    }

    //parse json data
    try{
            JSONArray jArray = new JSONArray(result);


            for(int i=0;i<jArray.length();i++)
            {
                    JSONObject json_data = jArray.getJSONObject(i);
                    friend=new Friend(json_data.getString("email"),json_data.getString("password"), json_data.getString("fullName"), json_data.getString("mobilePhone"), json_data.getString("mobileOperatingSystem"),"",json_data.getString("photo"));
            }
    }
    catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return friend;
}

2 个答案:

答案 0 :(得分:2)

为什么不直接将图像存储为BLOB

所有转换都是延迟和额外的CPU时间。


更新

现在我们知道为什么需要 base64(因为JSON不能传输二进制数据),我修改了我的答案。

您需要检查为什么这需要很长时间。是网络转移?是数据库吗?一旦你知道答案,我们就可以开始寻找解决方案了。

答案 1 :(得分:1)

Base64是6位编码:它需要4个字符(4个字节)来传输3个字节的图像。因此,在Base64中存储100kb图像需要133kb的空间。

您还没有说过您正在使用哪个数据库,但如果每行存储的数据量超过8kb,并非所有数据库都能正常运行。