Page 1 of 4 1234 LastLast
Results 1 to 20 of 68
  1.    #1  
    Hoping that some kind soul can help me out here

    Trying to work out just how I can create a list based on the results of an SQL query.

    If anyone can post some sample code or point me towards a tutorial I'd appreciate it.

    TIA.
  2. yiffzer's Avatar
    Posts
    218 Posts
    Global Posts
    232 Global Posts
    #2  
    I could be wrong but... check this thread?: SQL query from javascript - WebDeveloper.com
  3.    #3  
    Thanks for the info. Not sure if that's what I'm really looking for. If I have a database table called DOW (for days of the week, for example) then how can I create a list based on a simple select from this table, and the list be selectable, so that I can then select a day of the week ?
  4. yiffzer's Avatar
    Posts
    218 Posts
    Global Posts
    232 Global Posts
    #4  
    Are you running the javascript on the server, or on the client? Javascript is normally run on the client. Which would mean that the SQL database system is on the client, too. Is that really how you've got things set up?
  5.    #5  
    There must be a way of doing this from within the Mojo framework. There is a section in the roughcuts manual but it barely touches on incorporating the results obtain from an SQL query into other mojo objects, such as a list.
  6. yiffzer's Avatar
    Posts
    218 Posts
    Global Posts
    232 Global Posts
    #6  
    Can you link us to that section in the Rough Cuts manual?
  7.    #7  
    I'm trying to write a Pre app using the SDK. Thus, isn't the server and client one and the same, the Pre ?

    Look in Chapter 6 of the rough cuts manual and the section "HTML 5 storage".

    Also, I never once mentioned Javascipt guys !
  8. yiffzer's Avatar
    Posts
    218 Posts
    Global Posts
    232 Global Posts
    #8  
    Oh dear.

    But doesn't the Mojo SDK require JavaScript as part of making an app?
  9.    #9  
    yiffzer, yes you are correct. I can see how to execute SQL and get the results back, so I am ok with that part. Or at least I think I am Once I have the results how can I then populate a list with these results is what I am struggling with.
  10.    #10  
    Ok, so here is the coding that I have and I am hoping that someone can tell me what's wrong. I'm not getting any errors, but I am also not seeing the results I'd expect.

    First section of code creates the tables that I need, but let's just concentrate on 1 table. The table is created in my first scene assistant

    Code:
    var db;
    	
    	db = openDatabase("golfcaddie","1.0.0","PreGolfCaddie",200000);
    	if (!db)
                alert("Failed to open the database on disk.  This is probably because the version was bad or there is not enough space left in this domain's quota");
    	
    	db.transaction(function(tx) {
            tx.executeSql("CREATE TABLE players (id INTEGER UNIQUE, name TEXT, handicap INTEGER)", []);
    	});
    	
    	db.transaction(function(tx) {
            tx.executeSql("INSERT INTO players (id, name, handicap) VALUES (?,?,?)", [2, "Dave Williams", 12]);
    	});
    I also insert a record into the table just so that I at least have something in the table. However, since I don't seem to find a way on the Pre that will allow me to look at the different DB's out there and see what's in them I really have no idea if the SQL is working or not.

    In my first scene I have a button that calls my players scene that should initially list out all the players that are currently stored in the players table and allow me to add another player. When I go into the players scene it is not showing me the player that I inserted into the players table in the first scene.

    in my player setup I have the following code
    Code:
    	this.ListAttrs = {
    		listTemplate: 'players/list/listcontainer',
    		itemTemplate: 'players/list/listItem',
    		emptyTemplate: 'players/list/empty',
    		addItemLabel:$L("+")
    	};
    	
    	this.listModel = {
    		items: []
    	};	
    	
    	this.controller.setupWidget('contacts_list', this.ListAttrs, this.listModel);
    	Mojo.Event.listen(this.controller.get('contacts_list'), Mojo.Event.listTap, this.listClickHandler.bind(this))
    	Mojo.Event.listen(this.controller.get('contacts_list'), Mojo.Event.listAdd, this.AddContact.bindAsEventListener(this));
    In my player activate I have the following code:
    Code:
    this.getContactsList();
    My getContactsList code is as follows:

    Code:
    PlayerAssistant.prototype.getContactsList = function(){
    	db.transaction(function(tx) {
            tx.executeSql("SELECT id, name, handicap FROM players", [], this.handleListResponse.bind(this));
    		}).bind(this);
    }
    The handleListResponse code is as follows:

    Code:
    PlayerAssistant.prototype.handleListResponse = function(transaction, SQLResultSet){ 
        var playerList = [];
    	if (SQLResultSet.list.length > 0) {
            SQLResultSet.list.each(function(item){
                playerList.push({
                    id: item.id,
                    name: item.name,
                    handicap: item.handicap
                });
            });
        	this.controller.get('info_area').update('');
    	}else {
            this.controller.get('info_area').update('<center>There are no players</center>');
        }
        
        this.listModel.items = playerList;
        this.controller.modelChanged(this.listModel);
    }
    So, I have no idea why when I go into the player scene I am not seeing the player that I inserted into the table from the first scene.

    If anyone can help on this I'd really appreciate it. Also, how do you go about knowing for sure if a record was actually inserted into the database ?
  11.    #11  
    Trying to debug what is going on and it just gets more and more baffling to me

    I tried the following code and I didn't get either message indicating if the table was created successfully or not !!!

    Code:
    	db.transaction(function(tx) {
            tx.executeSql("CREATE TABLE players (id INTEGER UNIQUE, name TEXT, handicap INTEGER)", [],
    				this.createdplayertable.bind(this), this.errorplayertable.bind(this)); 
    		
    	});
    
    MainAssistant.prototype.createdplayertable = function(transaction, results)
    {
    	this.controller.showAlertDialog({
    		onChoose: function(value){
    		},
    		title: 'Alert',
    		message: "Created players table.",
    		choices: [{
    			label: 'OK',
    			value: 'OK',
    			type: 'color'
    		}]
    	});
    }
    
    MainAssistant.prototype.errorplayertable = function(transaction, error)
    {
    	this.controller.showAlertDialog({
    		onChoose: function(value){
    		},
    		title: 'Alert',
    		message: "Could not create players table.",
    		choices: [{
    			label: 'OK',
    			value: 'OK',
    			type: 'color'
    		}]
    	});
    }
  12. #12  
    I'm having a similar problem than yours. I don't know why, but it seems it doesn't call the response handler function. At least retrieving data.
  13.    #13  
    deCorvett. Glad I am not alone, but sucks that you're having a similar problem too

    I too thought it was related to retrieving data, but now I am not even sure that it is even inserting data, or creating tables as neither event handler is being called !!

    At a complete loss now as the documentation on this is skimpy at best
  14. #14  
    I'm creating tables ok, right now. The handler fires ok, and (in my case only shows a message in the scene) creating tables, works ok, I think, and adding rows seems to work also. My problem is only retrieving data from the database, and I don't figure out why. I think we are doing something wrong. I've read something about using "readtransaction" instead of "transaction". But there isn't anything more than "use readtransaction" on webosdev.
  15. worthb's Avatar
    Posts
    241 Posts
    Global Posts
    259 Global Posts
    #15  
    Have you looked at this?
    Mojo Storage Database - WebOS Internals
  16. #16  
    Yeah, and that is exactly that it doesn't works:

    Code:
    
    
    EquiposAssistant
    .prototype.activate =function(event){
    var string ="SELECT * FROM teams;";
    this.db.transaction( (function(transaction){ transaction.executeSql(string,[],this.RetrieveRecordsDataHandler.bind(this),this.errorHandler.bind(this)); }).bind(this) );
    }
    EquiposAssistant
    .prototype.RetrieveRecordsDataHandler =function(transaction, results)
    {
    $('info').update("Registro Creado");
    }
  17.    #17  
    deCorvett: How do you know that your tables are being created fine, and that rows are being inserted ? Is your code any different from mine ?

    worthb: Thanks, will take a look at that.
  18. #18  
    Quote Originally Posted by davetati View Post
    deCorvett: How do you know that your tables are being created fine, and that rows are being inserted ? Is your code any different from mine ?

    worthb: Thanks, will take a look at that.
    Well, you can assign a function to fire on transaction error and on transaction success. I'm updating a scene div on each table and row creation and the success functions fires, updating the div. So I'm assuming all this stuff is working ok.
  19. #19  
    Code:
     
    
    FirstUseAssistant
    .prototype.createRecordDataHandler =function(transaction, results)
    {
    $('info').update("Registro Creado");
    }
  20.    #20  
    With this code, I am still not getting an alert for either success or failure So frustrating. deCorvett, are you testing on the emulator or on an actual Pre ?

    Code:
    	var db = openDatabase("golfcaddie","1.0.0","PreGolfCaddie",200000);
    	
    	if (!db) {
    		this.controller.showAlertDialog({
    			onChoose: function(value){
    			},
    			title: 'Alert',
    			message: "Cound not create database.",
    			choices: [{
    				label: 'OK',
    				value: 'OK',
    				type: 'color'
    			}]
    		});
    	}
    	
    	db.transaction(function(transaction) {
            transaction.executeSql("CREATE TABLE IF NOT EXISTS players (id INTEGER UNIQUE, name TEXT, handicap INTEGER)", [],
    		function(transaction, results) {
    			this.controller.showAlertDialog({
    			onChoose: function(value){
    			},
    			title: 'Alert',
    			message: "Created table..",
    			choices: [{
    				label: 'OK',
    				value: 'OK',
    				type: 'color'
    			}]
    			});
    		},
    		function(transaction, error) {
    			this.controller.showAlertDialog({
    			onChoose: function(value){
    			},
    			title: 'Alert',
    			message: "Cound not create table." + error.message,
    			choices: [{
    				label: 'OK',
    				value: 'OK',
    				type: 'color'
    			}]
    			});
    		}); 
    	});
    	
    	db.transaction(function(transaction) {
            transaction.executeSql("INSERT INTO players (id, name, handicap) VALUES (?,?,?)", [2, "Dave Williams", 12],
    		function(transaction, results) {
    			this.controller.showAlertDialog({
    			onChoose: function(value){
    			},
    			title: 'Alert',
    			message: "Player inserted..",
    			choices: [{
    				label: 'OK',
    				value: 'OK',
    				type: 'color'
    			}]
    			});
    		},
    		function(transaction, error) {
    			this.controller.showAlertDialog({
    			onChoose: function(value){
    			},
    			title: 'Alert',
    			message: "Player not inserted: " + error.message,
    			choices: [{
    				label: 'OK',
    				value: 'OK',
    				type: 'color'
    			}]
    			});
    		}); 
    	});
Page 1 of 4 1234 LastLast

Posting Permissions