Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Regular Contributor
anthonyq
Posts: 73
Registered: ‎12-09-2010
My Device: Blackberry Curve 8530
My Carrier: Virgin Mobile USA
Accepted Solution

SQLLite

Hi there,

 

I'm writing an app that requires persistent data storage, and from poking around the web I see that Air/AS3 has this built right in, which is awesome! I was taking a look over at the code at http://seantheflexguy.com/blog/2007/06/14/super-simple-sqlite-example-for-adobe-air-1-beta/  but wasn't sure how I could apply this to the Blackberry tablet, namely, 

exampleDBFile = new File("app-resource:/ExampleDatabase.db");

 

 

that line.  What would I use here?

 

Thanks,

Anthony

Anthony
http://www.anthonyquarella.com
Accepted Apps: UpToDateTV
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: SQLLite

[ Edited ]

hey anthony,

 

welcome to the forums! instead of typing in that line use the following form:

 

 

exampleDBFile = File.applicationStorageDirectory.resolvePath("ExampleDatabase.db");

 

this essentially does the same thing but different environments access file paths differently. what's happening in the above code is its creating a new file ExampleDatabase.db in your app's specific storage directory. hope that helps! good luck!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Regular Contributor
anthonyq
Posts: 73
Registered: ‎12-09-2010
My Device: Blackberry Curve 8530
My Carrier: Virgin Mobile USA

Re: SQLLite

Great!!!  Thank you very much!

Anthony
http://www.anthonyquarella.com
Accepted Apps: UpToDateTV
Please use plain text.
Regular Contributor
anthonyq
Posts: 73
Registered: ‎12-09-2010
My Device: Blackberry Curve 8530
My Carrier: Virgin Mobile USA

Re: SQLLite

One more question - how am I to display results from a table?  (If you could point me to an example that'll do it!)

 

I can display one column by adding a series of LabelButtons (with their label set to the text from the column) to a dataprovider of a List, but that seems like a pretty hacked up way to do it.

 

I tried a datagrid example from Adobe, but that threw all sorts of style errors for some reason.

 

Thanks!

Anthony
http://www.anthonyquarella.com
Accepted Apps: UpToDateTV
Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: SQLLite

There is no grid available in QNX.  You might get a multi-column list to work, but with some effort.  You could also probably roll your own if you're looking for just some basic view.  Also might want to do a Google search on ActionScript grids and tables.  I know there are a few that might be able to get ported to the PB.

Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: SQLLite

Hey Guys,

 

ok so i've been working on a QNX way of doing grids and tables. im kinda becoming fond of the lack of documentation for the API's. but i find it a good challenge :smileyhappy:

 

anyways here's the code on a working grid layout. we are going to use the TileList class and then for the dataprovider we are going to use the SQLLiteModel (had some help from the FullRSSReader's SQLLiteModel.as file). This method doesnt exactly work like you'd think it would. but maybe we just gotta find more ways to go about it. Here's the code:

 

TileList.as (Main Application):

 

 

package
{	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	import qnx.ui.data.DataProvider;
	import qnx.ui.listClasses.ScrollDirection;
	import qnx.ui.listClasses.TileList;
	
	[SWF(width="1024", height="600", backgroundColor="#CCCCCC", frameRate="30")]
	public class TileListTest extends Sprite
	{
		private var myList:TileList;
		private var myDataProvider:DataProvider;
		private var mySQLData:SQLLiteModel;
		
		public function TileListTest()
		{
			super();
			
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			mySQLData = new SQLLiteModel();
			
			
			myList = new TileList();
			
			myList.dataProvider = mySQLData.getTableData();
			myList.setSize(900,500);
			myList.scrollDirection = ScrollDirection.VERTICAL;
			myList.columnCount = 3;
			myList.columnWidth = 300;
			myList.rowHeight = 47;
			myList.cellPadding = 1;
			
			addChild(myList);
			
			//mySQLData.deleteAll();
			
			
		}
	}
}

 

 

SQLLiteModel.as (Our Data Class):

 

 

package
{
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.filesystem.File;
	
	import qnx.ui.data.DataProvider;
	
	public class SQLLiteModel
	{
		private var sqlDatabase:File;
		private var sqlConnection:SQLConnection;
		private var sqlStatement:SQLStatement;
		private var sqlResult:SQLResult;
		private var dp:DataProvider;
		private var totalRows:int;
		
		private var firstRun:Boolean;
		
		public function SQLLiteModel()
		{
			
			init();
			
		}
		public function init():void
		{
			
			//create database file instance
			sqlDatabase = File.applicationStorageDirectory.resolvePath("SQLTest.db");
			
			/*
			 * Check to see if this is the first time running the application
			*/
			
			if (!sqlDatabase.exists)
			{
				firstRun = true;
			}
			
			//create connection instance
			sqlConnection = new SQLConnection();
			
			//open up database for use
			sqlConnection.open(sqlDatabase);
			
			sqlStatement = new SQLStatement();
			
			//set the connection to the statement
			
			sqlStatement.sqlConnection = sqlConnection;
			
			createTable();
			
		}
		public function createTable():void
		{
			var sql:String = "CREATE TABLE IF NOT EXISTS testTable ("
				+ "testID INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ "testTitle TEXT, "
				+ "testData TEXT"
				+ ")";
			
			sqlStatement.text = sql;
			sqlStatement.execute();
			
			/*
			 * if this is the first time running this code,
			 * we need to add some dummy values so we aren't 
			 * working with an empty database (e.g. avoid errors)
			*/
			
			if (firstRun)
			{
				for (var i:int = 0; i < 15; i++)
				{
					addData("My New Title " + i, "My New Data " + i);
				}
			}
			
		}
		public function getTableData():DataProvider
		{
			/*
			* This is where the bread and butter is. This data provider
			* object gets sent to the Tile List object in the main
			* application. You have to format this in a certain
			* to get the data to show up properly.
			*/
			
			var sql:String = "SELECT * FROM testTable";
			
			sqlStatement.text = sql;
			sqlStatement.execute();
			
			sqlResult = sqlStatement.getResult();
			
			//get the total rows in the database
			totalRows = sqlResult.data.length;
			
			dp = new DataProvider();
			
			for each(var obj:Object in sqlResult.data){
				if(obj != null){
					
					/*
					* Note: when the tile list object does the cells,
					* each item in the dataprovider is used as a cell
					* so there is now way of grouping rows as one 
					* data item (yet).
					*/
					
					dp.addItem({label:obj["testID"]});
					dp.addItem({label:obj["testTitle"]});
					dp.addItem({label:obj["testData"]});
				}
			}
			return dp;
		}
		public function addData(newTitle:String, newData:String):void
		{
			var sql:String = "INSERT INTO testTable "
				+ "(testTitle, TestData) "
				+ "VALUES ("
				+ "'" + newTitle + "', "
				+ "'" + newData + "'"
				+ ")";
			
			sqlStatement.text = sql;
			sqlStatement.execute();
			
		}
		public function deleteData(delID:int):void
		{
			var sql:String = "DELETE FROM testTable WHERE testID = " + delID + "";
			
			sqlStatement.text = sql;
			sqlStatement.execute();
			
		}
		public function deleteAll():void
		{
			var sql:String = "DELETE FROM testTable";
			
			sqlStatement.text = sql;
			sqlStatement.execute();
		}
		public function getCount():int
		{
			return totalRows;
		}
	}
}

 

The comments are where it kind of gets confusing and just random places. Keep in mind i dont know how effiicient all my functions and methods are but it gets the job done. i'll build more on it later. let me know if this helps! good luck guys!

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Regular Contributor
anthonyq
Posts: 73
Registered: ‎12-09-2010
My Device: Blackberry Curve 8530
My Carrier: Virgin Mobile USA

Re: SQLLite

This looks really cool!  I've just implemented something similar, so thanks!!

 

Any ideas on how to set formatting differently for different rows/colums?  For example, make one column bigger than the others,  or make column padding (or cell padding left/right) less than row padding (or cell top/bottom).

 

Related notes on formatting - do you have to use the SetTextFormat method? I have a couple of labels set up with:

 

lblA.format.size = 32;
lblA.format.bold = true;

 

but that style is completely ignored.  Also, is there a way to define a border or background of the List class, so that it's easier to see when scrolling is necessary (also looks better)?

 

Anthony
http://www.anthonyquarella.com
Accepted Apps: UpToDateTV
Please use plain text.
Developer
JRab
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: SQLLite

hey,

 

as for making custom column widths and row heights thats not possible from all the testing i've done so far. thats the one thing i dislike about the listClasses package is the lack of ability to modify each individual column or row. the cell renderer can only do so much. so maybe we should request this feature.

 

as for formatting the Label object. i ran into the same problem before and found that you must create a TextFormat object first and then set the label's format property to that textformat. you cant simply set the property of the format property like it shows in the documentation. they dont show the declaration which confuses people. so to format do the following:

 

 

var myFormat:TextFormat = new TextFormat();

myFormat.size = 12;
myFormat.bold = true;

lblA.format = myFormat;

 

as for the list background and border, i think that is theortically possible by defining a custom cell renderer class and creating borders and such. i dont believe it is possible to apply an entire background color to a list. but dont quote me on that on that last part :smileyhappy:

 

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Please use plain text.
Developer
jtegen
Posts: 6,541
Registered: ‎10-27-2010
My Device: HTC One, PlayBook, LE Z10, DE Q10
My Carrier: Verizon

Re: SQLLite

Label formats have to be allocated like this:

 

lblA.format = new TextFormat( null, 32, 0x000000, true );

 

to get a 32 point, black, bolded text.

 

See here for more:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextFormat.html

Please use plain text.
Regular Contributor
anthonyq
Posts: 73
Registered: ‎12-09-2010
My Device: Blackberry Curve 8530
My Carrier: Virgin Mobile USA

Re: SQLLite

[ Edited ]

I'd LOVE to say I've tried all this out and got it working...but after making all the changes, my dev environment has broken.

 

Randomly, my simulator stops talking to FB4. It installs, launches, and then hangs at the "Loading your application..." screen, and quits comm with FB4...

 

Even after FB4 decides it can't connect to the flash debugger, I can go and try debugging again - and the tablet sim WILL respond, exiting appropriately, reinstalling and launching...and then back to hang.  I don't think that setting text format would cause a crash like this...right?

 

EDIT: after commenting out all code and just letting a trace("hi") exist in my main function - the sim still wont talk to FB after app launch (never see the trace message), but the app WILL eventually launch and show a blank screen (appropriate, since I commented my code).

Anthony
http://www.anthonyquarella.com
Accepted Apps: UpToDateTV
Please use plain text.