Question

I searched a lot about this question but I didn't find any direct answer.

I am programming an Android application that uses SQL to store data. I'm passing SQL query trought a simple PHP Web Service.

The Web service code is

<?php

if(isset($_GET['user'])) { $user = $_GET['user']; }
if(isset($_GET['pass'])) { $pass = $_GET['pass']; }
if(isset($_GET['query'])) { $query = $_GET['query']; }

if(isset($query)) {
    $link = mysql_connect('localhost',$user,$pass) or die('Connexion impossible au serveur SQL');
    mysql_select_db('dbi',$link) or die('Impossible de sélectionner la base de donnée');

    $result = mysql_query($query,$link) or die('Erreur dans la requete : '.$query);
    $count=0;
    while($row = mysql_fetch_row($result)){
    if ($count>0) echo ";";
        for($a=0;$a<count($row);$a++){
            echo $row[$a];
            if ($a<(count($row)-1)) echo ',';
        }
    $count++;
    }

    @mysql_close($link);
}
?>

Everything is working (I in fact only use the webservice to Insert data in the database (I dump the database into a sqlite database to work with it localy)) except when I try to insert a Blob. I get illegal characters, so I try to encode the query but the blob is corrupt.

private String executeQuery(String query){
        HttpClient client= new DefaultHttpClient();
        String encodedQuery=null;
        try {
            encodedQuery = URLEncoder.encode(query,"UTF-8");
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
        HttpGet httpget = new HttpGet(serverURL+"?user="+username+"&pass="+password+"&query="+encodedQuery);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String result = client.execute(httpget, responseHandler);
        client.getConnectionManager().shutdown();
        return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
        }
    }

I execute the query like that

executeQuery("INSERT INTO " + DBIDatabase.TABLE_signatures + " (name, job, signature) VALUES ('" + pm.signature.name + "','" + job_id + "','" + new String(pm.signature.sign.imageByteArray) + "')")

Original BLOB (As printed by a System.out.println in eclipse):

�PNG


������
IHDR�����������d��������fw������sBIT|d�����RIDATx���=��D���oJ~�����DBB���OH��6B�QDP%�]��n�"����ڞ��3��G��};���������������������������������x��8�&������#9lě����۸ݪ8No���N}��؉t9u%�|��ح�jU\��㠽��f��T"��rp,������7_�
�G��]] L�*��������lK?L+P��+��;��x�I�z�r�+ؐ%���q3��ˆS��$��P�V���*���Ѷ�Dҗ$�ڟZ�b�k��'�!".#�����]So�d��-�k1���X��[�a�2�Y/#�;�~�Кx�J2�ڶ�PwݴОf,�ـ�su{��t��O�5��%��:�t�E�W�/2�?$AԸݠj[<��zEy��+�a�Wй��V̚�i��>]Pk��u���,QQM�2�
ɖ<�5�x�
d��R���wJ�����1��C����e�'K�ف�X�{��N�%'����V�ɐ����-���#V����iD|�1��xK��͋�%l�]�g�zXBܩָNP�v�l8�0נ��1�o��4��9��j�Jj*b��q�'U�񕾄،{�n`b5�x*�Ӧle�mZ��r%�GS�7G�}�Z�y�e)cyc����3����z�X���Z�����.��

��8s,��s���]~��d�v�GD|����0T�3��X�䴋��:�>�����("~�a�;�k���_s�(u:�ݒ��1�o�~x0��N�.�26Qy s��R��IC��TkaN���->��Ա�dPL�١OJ2��ؗ�N��g,s���..�ߩ�쓒Aq?�_�7��ʲv�c|��ѢHm�,e{S�q�X�l8���j3�ߕ�i�0I�%�*ۜ�s�0�2����<���!�"��u=x���rVK� �����}UE�N��F�E�YD|�#��y��G��/>G�<D����1�#�߈�+�z�m�{�>//@�����P�D��о8Z�1 ��1�z�Ÿ����!����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������P�����'3������������IEND�B`�')

Blob in the database:

�PNG


IHDR�d��fwsBIT|d��IDATx���=n�@��Q��s�4p��5�� N@�@�p (4�i�DB "��P�b�Zo�����y��VZ�^���xF��ĥ�`�I���Or��cLއ$G�?��~5���q�����k�W��M;`4��?uw*��.տ�낹V���T"�|O�o�`.���J��n��jI��=�V��
�N�`��>�%��R�N��I.�¤a��,H��47�=:m{�
e+������a�`.(��A�$ɳ���I�{����ł�X�1���)B���G����
�^�����t������0�S����T_�`P��� �,��}�S@�~f|O�uvv)�u?��}�`.�!�U[#�zN��,�?tf(7�f���R���T�s�-.��ÕY˼Iu_�`� ��w�dX�~�Fy�)�g�.u
�Y��Ƭ`.���WAM��Z��u�m6xL�k;�}�q������������$_rv��N�V(#L�E���j��e���k��X,��������K�>}�P>轺�fL��Ի��
���!v悡�%y�z�0v�p.OSos�T@��N����0L�S��(���ZK�u�ϱ�9U��@{����*J�YIEND�B`�

If I load back the picture, it's an empty picture, sadly. Is there something I missed? Is there any other way to pass the Blob to the PHP Web Service?

Thx

Was it helpful?

Solution

You need to Base64-encode your image, then store as String. This ensures that you only use valid characters on your php, database, and wherever.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top