3 Replies Latest reply on Aug 3, 2008 9:46 PM by anirudhs

    inserting and retrieving images from db

    drecute
      Hi all,
      Is there a specified format for retrieving and inserting images into sqlite using air. I have made connections to sqlite. It returned successful. Here is my code except;

      <script language="javascript" type="text/javascript">
      var insertStmt = new air.SQLStatement();
      var selectImg = new air.SQLStatement();
      //var data = new ByteArray();
      var img;
      var imgSelected;
      function uploadImage() {
      try {
      img = document.getElementById("upload").value;
      var imgfile = new air.File(img);
      var filepath = imgfile.url;
      alert(filepath);
      insertStmt.sqlConnection = conn;
      // the SQL query
      insertStmt.text = "INSERT into adverts (banner) VALUES (\"" + img + "\")";
      // register listeners for the result and failure (status) events
      insertStmt.addEventListener(air.SQLEvent.RESULT, insertResult);
      insertStmt.addEventListener(air.SQLErrorEvent.ERROR, insertError);
      insertStmt.execute();
      }catch(e) {
      air.SQLErrorEvent.ERROR;
      }
      }
      function insertResult(event) {
      alert("a new record has been inserted" );
      }
      function insertError(event) {
      alert("records unsuccessful" + event.error.details + event.error.message);
      }

      function downloadImg() {
      //imgSelected = document.getElementById("dbimg").value;
      selectImg.sqlConnection = conn;
      selectImg.text = "select stage_name, pix from artiste WHERE pix = ? and stage_name = 'dbanj'";
      alert(selectImg.getResult());
      selectImg.addEventListener(air.SQLEvent.RESULT, resultHandler);
      selectImg.addEventListener(air.SQLErrorEvent.ERROR, errorHandler);
      selectImg.parameters[0] = resultHandler(event);
      //selectImg.parameters[1] = document.getElementById("dbimg").value;
      //alert(selectImg.parameters[0]);
      selectImg.execute();
      }
      function resultHandler(event) {
      var result = selectImg.getResult();
      if(result == null)
      alert("result:" + " "+ result);
      else
      var colResults = result.data;
      //if(colResults == "undefined")
      alert("colResults:"+ " "+ colResults);
      //else
      for (i = 0; i < colResults; i++) {
      var row = result.data ;
      var output = "banner: " + row.banner;
      alert(output);
      }
      }
      function errorHandler(event) {
      alert("the image was not retrieved" + event.error.details +" "+ event.error.message);
      }
      </script>


      //then the HTML code

      <table width="591" border="0" cellpadding="0" cellspacing="0">
      <!--DWLayoutTable-->
      <tr>
      <td width="60" height="28"> </td>
      <td width="351"> </td>
      <td width="99"> </td>
      <td width="81"> </td>
      </tr>
      <tr>
      <td height="45"> </td>
      <td colspan="2" valign="top"><form id="form1" name="upload" enctype="multipart/form-data" method="post" action="#">
      <label>Image upload
      <input type="file" name="upload" id="upload"/>
      <input name="upload" type="submit" value="Upload" onclick="uploadImage()" />
      </label>
      </form></td>
      <td> </td>
      </tr>
      <tr>
      <td height="24"> </td>
      <td rowspan="2" valign="top"><div id="dbimg">
      <script language="javascript">
      </script></div></td>
      <td valign="top"><input type="submit" name="dnbtn" id="dnbtn" value="download" onclick="downloadImg()" /></td>
      <td> </td>
      </tr>

      I want to know if there are any special way of doing this