4 Replies Latest reply on Jun 12, 2011 6:23 PM by zsc1234

    How to write bytearray  in sqlite

    zsc1234 Level 1

      I do not see where mentioned this problem.

        • 1. Re: How to write bytearray  in sqlite
          Peter Blazejewicz Level 4

          Hi,

          I think you could create parametrized query and by assigning ByteArray object to such parameter it will be translated under-the-hood into BLOB data column type, see:

          http://help.adobe.com/en_US/as3/dev/WSd47bd22bdd97276f2aceae3b1262b7f2d43-8000.html

          http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/data/SQLStatement.html#par ameters

          so when you're creating SQL statements make sure you're parametrize them, then create parameter(s) for that statement assigning binary content and it will be injected/stored in database without much fuss

           

          hth,

          kind regards,

          Peter

          • 2. Re: How to write bytearray  in sqlite
            zsc1234 Level 1

            thank you

            Peter

             

            I know the general method of data insertion.

            But I do not know the data is inserted BLOB.
            this is my code:
            public function addData(name:String,path:String,data:ByteArray):void
            {
            var ssm:SQLStatement = new SQLStatement();
            ssm.sqlConnection = _scn;
            var sql:String = "INSERT INTO thumbnail (fileName, filePath, fileData) VALUES ('";
            sql += name + "','";
            sql += path + "','";
            sql += data + "')";
            ssm.text = sql;
            ssm.execute();
            ssm = null;
            sql = null;
            }
            but it is error:
            Error #2044: Unhandled SQLErrorEvent:. errorID=3115, operation=execute , message=Error #3115: SQL Error. , details=unrecognized token: ''‰PNG

            • 3. Re: How to write bytearray  in sqlite
              Peter Blazejewicz Level 4

              Hi,

               

              you've 3115 unrecognized token error - which could be for example that statement do not contains required/expected value - or contains value that is not recognized (unexpected) I think. Have you tried parameters?

               

               

              var sql:String = "INSERT INTO thumbnail (fileName, filePath, fileData) VALUES (@name, @path, @data)";

              ssm.text = sql;

              ssm.parameters["@name"] = name;

              ssm.parameters["@path"] = path;

              ssm.parameters["@data"] = data;

               

               

              or:

               

               

              var sql:String = "INSERT INTO thumbnail (fileName, filePath, fileData) VALUES (:name, :path, :data)";

              ssm.text = sql;

              ssm.parameters[":name"] = name;

              ssm.parameters[":path"] = path;

              ssm.parameters[":data"] = data;

               

               

              or:

               

               

              var sql:String = "INSERT INTO thumbnail (fileName, filePath, fileData) VALUES (?, ?, ?)";

              ssm.text = sql;

              ssm.parameters[0] = name;

              ssm.parameters[1] = path;

              ssm.parameters[2] = data;

               

               

               

              Your "data" needs to be ByteArray (as it is raw byte blob). You need parameters because (as in linked doc) when parametrized value is used it is directly:

              Values supplied as parameters using the SQLStatement.parameters associative array are assigned the storage class that most closely matches the native data type bound. For example  [...] ByteArray objects are given the BLOB storage class.

               

              When using object to be written as BLOB in SQL string literal (as in your sample) the previous paragraph in Adobe's docs says it is expected to provide data in special hexadecimal notation and I haven't yet seen someone using bytearray with that way of data conversion during sql injection  - but I've seen people using parametrized values to insert byte array into blob already.

               

              regards,

              Peter

              • 4. Re: How to write bytearray  in sqlite
                zsc1234 Level 1

                Hi,Perter

                     The problem finally resolved.

                     Thank you very much!