Results 1 to 13 of 13
  1.    #1  
    I've been trying for days to create a database in my app. I think I've gotten to the point where I am just completely confused and don't even know where to begin anymore.

    I just need a simple database with a couple tables, an auto generate primary key, a few other columns and to get the data out of the DB and into a scene.

    I've been looking all over the net for a simple sample that creates a simple DB, a simple table, adds a single row to the table, pulls that row from the table and then pushes the data to a scene.

    I am trying to use the sample from webosinternals, but I keep getting the error "Table doesn't exist" whenever I try to create the table. Very frustrating (of course the table doesn't exist...thats what I am trying to do) So I looked at some samples in that came with the SDK and they do things completely differently. Ugh.

    Anyway, if anyone has some code laying around that would be relatively easy to follow for a Javascript noob, I'd be forever in your debt. Thanks!
  2. #2  
    Code:
    function SqliteAssistant() {
    	/* this is the creator function for your scene assistant object. It will be passed all the 
    	   additional parameters (after the scene name) that were passed to pushScene. The reference
    	   to the scene controller (this.controller) has not be established yet, so any initialization
    	   that needs the scene controller should be done in the setup function below. */
          this.nullHandleCount = 0;
    	  this.db = null;
    }
    
    SqliteAssistant.prototype.setup = function() {
    	Mojo.Event.listen($('createDB_button'), Mojo.Event.tap, this.CreateDB.bind(this));
    	Mojo.Event.listen($('createTables_button'), Mojo.Event.tap, this.CreateTable.bind(this));
    	Mojo.Event.listen($('fillTables_button'), Mojo.Event.tap, this.FillTable.bind(this));
    	Mojo.Event.listen($('query_execute_button'), Mojo.Event.tap, this.queryData.bind(this));
    }
        `
    SqliteAssistant.prototype.queryData = function(){
    	this.controller.stageController.assistant.showScene("storage/sqlite", 'query', this.db)		
    }
    
    SqliteAssistant.prototype.activate = function(event) {
    	/* put in event handlers here that should only be in effect when this scene is active. For
    	   example, key handlers that are observing the document */
    }
    
    
    SqliteAssistant.prototype.deactivate = function(event) {
    	/* remove any event handlers you added in activate and do any other cleanup that should happen before
    	   this scene is popped or another scene is pushed on top */
    }
    
    SqliteAssistant.prototype.cleanup = function(event) {
    	/* this function should do any cleanup needed before the scene is destroyed as 
    	   a result of being popped off the scene stack */
    }
    
    SqliteAssistant.prototype.CreateDB = function(event) {
    
    	try {
    		this.db = openDatabase('SampleDB', '', 'Sample Data Store', 65536);
    		$(result).update("Created database SampleDB.");
    	}
    	catch (e)
    	{
    		$(result).update(e);		
    	}
    }
    
    SqliteAssistant.prototype.CreateTable = function(event) {
    
    	try {
    		this.nullHandleCount = 0;
    		//create table 1
    		var string = 'CREATE TABLE table1 (col1 TEXT NOT NULL DEFAULT "nothing", col2 TEXT NOT NULL DEFAULT "nothing"); GO;'
    	    this.db.transaction( 
    	        (function (transaction) { 
    				transaction.executeSql('DROP TABLE IF EXISTS table1; GO;', []); 
    	            transaction.executeSql(string, [], this.createTableDataHandler.bind(this), this.errorHandler.bind(this)); 
    	        }).bind(this) 
    	    );
    		
    		$(result).update("Created Tables.");
    	}
    	catch (e)
    	{
    		$(result).update(e);
    	}	
    }
    
    SqliteAssistant.prototype.FillTable = function()
    {
    	/*
    	 * Get a random number between 0 and 99
    	 */
    	now = new Date();
    	seed = now.getSeconds();
    	var random_number = Math.random(seed);
    	var range = random_number * 100;
    	var rounded_number = Math.round(range);
    	/*
    	 * Now add a record based on the above.
    	 */
    	$(result).update('0');
    	this.nullHandleCount = 0;
    	value1 = 'valuea' + rounded_number
    	value2 = 'valueb' + rounded_number
    	var string = 'INSERT INTO Table1 (col1, col2) VALUES ("' + value1 + '","' + value2 + '"); GO;'	
    	this.db.transaction( 
            (function (transaction) { 
                transaction.executeSql(string, [], this.createRecordDataHandler.bind(this), this.errorHandler.bind(this)); 
            }).bind(this) 
        ); 
    }
    
    SqliteAssistant.prototype.createTableDataHandler = function(transaction, results) 
    {
    	$(result).update("Created TABLE1.");
    } 
    
    SqliteAssistant.prototype.createRecordDataHandler = function(transaction, results) 
    {	
    	$(result).update("Inserted 1 record.");
    } 
    
    SqliteAssistant.prototype.errorHandler = function(transaction, error) 
    { 
        console.log('Error was '+error.message+' (Code '+error.code+')'); 
        return true;
    }
  3. #3  
    You didn't searched too much... there is a sample in the SDK that does exactly that, and curiously it's called "Data"...
  4. #4  
  5. atlanta's Avatar
    Posts
    324 Posts
    Global Posts
    337 Global Posts
    #5  
    Quote Originally Posted by deCorvett View Post
    You didn't searched too much... there is a sample in the SDK that does exactly that, and curiously it's called "Data"...
    Stop being a dO*CHE and learn how to help.
  6. #6  
    What's the problem with you?

    All the stuff is as a sample in the SDK, does EXACTLY that that he is asking and the sample has the easy to recognize name of "data". It's more than obvious that he haven't do any research in the SDK.
  7. #7  
    By the way, your answer is quite more useless than mine, I think.
  8. #8  
    From a users perspective I can tell you programmers that it is very important that you store the database in /media/internal/.app-storage/

    This means that users can make easily make their own backups of the database and can easily restore them after a full reset or exchanging devices. Some of the better homebrew apps have used this directory such as Checkbook, Shoppinglist, drpodder, and WorkOut Tracker. It would be nice if all of them did.

    From a user's perspective there is no point in putting data in your apps unless we can back up that data and restore when ever necessary.

    Thanks!
    Pilot 1000 -> Pilot 5000 ->Palm Pilot Professional -> HP 620LX -> TRG Pro -> Palm V -> Palm Vx -> Palm M505 -> Palm i705 -> Palm Tungsten|T -> Samsung i500 -> Treo 600->Treo 650 -> Treo 600-> Treo 700p ->Centro ->Treo 800w + Redfly C8n -> Palm Pre -> HP Touchpad
    R.I.P Palm 1996-2011
  9.    #9  
    I did do research in the SDK. I spent a couple hours trying to understand what was going on in the code pasted above. The problem, as I mentioned, is that I am a javascript noob. All of the different functions in there are confusing me. That's why I was hoping for something extremely simple. Like a Hello World for databases. Everything in one function, maybe with some comments. As I suspect of others, I learn in stages. A couple simple examples that I can throw together to build an app and then continue to add complication as I go is the easiest way for me to learn.

    I am going to be honest, I was extremely nervous about posting in here because there are some people on this board who seem to search out posts they can lambaste (luckily they are in the extreme minority, albeit a vocal one). I spent a lot of hours trying to figure it out on my own, hoping to avoid bothering people with my noobishness. But I am getting desperate and decided to reach out to the helpful among us.

    While I don't want to start some sort of flame war, I can't help but point this out deCorvett. I remembered you from another thread in which you were having problems with databases and nobody accused you of not searching.
    http://forums.precentral.net/web-os-...-lists-db.html

    <SoapBox>I can't help but wonder if those of the RTFM crowd would say such things to people's faces or not. What is the point of being a part of a user community with the aim of assisting each other if you "help" by telling people to figure it out on their own.</SoapBox>


    Anyway. Thank you Equilibrium for posting some information. I really do appreciate it. I am going to start over again with my project and see if I can get the DB working. If not, I'll post my code and errors, maybe someone can point out what I am doing wrong.

    Thanks.
  10. #10  
    man, the problem here is that the use of db is as is, there is no way to make it easier as it is. It involves two technologies (javascript and SQLite). As I told you, the sample in the SDK is quite simple, and there is no way to make it easier (plus it does exactly what you want to do).

    OTOH, sorry for supposing that you didn't do research.
  11.    #11  
    Quote Originally Posted by deCorvett View Post
    man, the problem here is that the use of db is as is, there is no way to make it easier as it is. It involves two technologies (javascript and SQLite). As I told you, the sample in the SDK is quite simple, and there is no way to make it easier (plus it does exactly what you want to do).

    OTOH, sorry for supposing that you didn't do research.
    Understood. The thing that is getting me stuck is the interactions between the functions. I can't discount the possibility that I am being a goof and overcomplicating things which is seeming more likely. If there really is no way to boil the code down into a simpler example, then my next step is going to be trying a new project from scratch then posting code and errors.

    One specific question I do have is whats the difference between having "Go;" at the end of the SQL statement and not having it? (SDK has it, webosinternals does not).

    Thanks.
  12. #12  
    I'm far away from being a mySQL expert, but if I'm not wrong GO forces the db engine to process complex selet queries at the same time, improving the execution time.
  13. #13  
    If you post some of your code, maybe we can help diagnose your issues.
    cohoman

    Palm Apps:

    jVault and jChecklist

Posting Permissions