Advertisement
peter9477

read/write BLOB test/timing

Aug 3rd, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.desktop.NativeApplication;
  5.     import flash.events.Event;
  6.     import flash.events.InvokeEvent;
  7.     import flash.system.Capabilities;
  8.     import flash.filesystem.File;
  9.     import flash.filesystem.FileStream;
  10.     import flash.filesystem.FileMode;
  11.     import flash.utils.*;
  12.     import mx.utils.ObjectUtil;
  13.     import qnx.ui.text.Label;
  14.  
  15.     [SWF(width="1024", height="600", backgroundColor="#cccccc")]
  16.     public class SQLTest2 extends Sprite
  17.     {
  18.         public var label:Label;
  19.  
  20.         public function SQLTest2()
  21.         {
  22.             label = new Label();
  23.             label.multiline = true;
  24.             label.width = stage.stageWidth;
  25.             label.height = stage.stageHeight;
  26.             label.wordWrap = true;
  27.             label.text = "this will be replaced";
  28.  
  29.             addChild(label);
  30.             stage.nativeWindow.visible = true;
  31.  
  32.             import flash.data.SQLConnection;
  33.             import flash.data.SQLStatement;
  34.             import flash.errors.SQLError;
  35.             import flash.data.SQLResult;
  36.             import flash.filesystem.File;
  37.  
  38.             var imagePath:File = File.userDirectory.resolvePath('shared/camera/IMG_00000338.jpg');
  39.  
  40.             var dbpath:File = File.applicationStorageDirectory.resolvePath('my.db');
  41.             dbpath.deleteFile();
  42.  
  43.             var times:Array = [];
  44.             var conn:SQLConnection = new SQLConnection();
  45.  
  46.             try
  47.             {
  48.                 conn.open(dbpath);
  49.                 times.push(getTimer()); // [0] 1533
  50.  
  51.                 var stmt:SQLStatement = new SQLStatement();
  52.                 stmt.sqlConnection = conn;
  53.                 stmt.text = "CREATE TABLE IF NOT EXISTS test(foo TEXT PRIMARY KEY, bar BLOB)";
  54.                 stmt.execute();
  55.                 trace("executed", stmt.text);
  56.                 times.push(getTimer()); // [1] 366 ms
  57.  
  58.                 var blob:ByteArray = new ByteArray();
  59.                 var imageFile:FileStream = new FileStream();
  60.                 imageFile.open(imagePath, FileMode.READ);
  61.                 imageFile.readBytes(blob);
  62.                 times.push(getTimer()); // [2] 152 ms
  63.                 trace('len1', blob.length);
  64.  
  65.                 conn.begin();
  66.                 stmt.text = "INSERT INTO test VALUES (:foo, :bar)";
  67.                 stmt.parameters[':foo'] = 'client';
  68.                 stmt.parameters[':bar'] = blob;
  69.                 stmt.execute();
  70.                 trace("executed", stmt.text);
  71.                 times.push(getTimer()); // [3] 474 ms
  72.  
  73.                 conn.commit();
  74.                 times.push(getTimer()); // [4] 3636 ms
  75.  
  76.                 stmt.text = "SELECT foo, bar, length(bar) as len FROM test";
  77.                 stmt.clearParameters();
  78.                 stmt.execute();
  79.                 trace("executed", stmt.text);
  80.                 var result:SQLResult = stmt.getResult();
  81.                 label.text = String(result.data[0].foo);
  82.                 blob = result.data[0].bar;
  83.                 trace("results", result.data.length, result.data[0].len, blob.length);
  84.                 times.push(getTimer()); // [5] 282 ms
  85.  
  86.                 conn.close();
  87.  
  88.                 imagePath = File.applicationStorageDirectory.resolvePath('written.jpg');
  89.                 imagePath.deleteFile();
  90.                 imageFile.open(imagePath, FileMode.WRITE);
  91.                 imageFile.writeBytes(blob);
  92.                 imageFile.close()
  93.                 times.push(getTimer()); // [6] 36 ms
  94.                 }
  95.             catch (error:SQLError)
  96.             {
  97.                 trace("Error message:", error.message);
  98.                 trace("Details:", error.details);
  99.             }
  100.  
  101.             trace('times', ObjectUtil.toString(times));
  102.             var time:uint = times[0];
  103.             for (var i:int = 1; i < times.length; i++) {
  104.                 trace(i, times[i] - time);
  105.                 time = times[i];
  106.             }
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement