NKTableView, NKFile, NKNKFileDownloader

Ask questions and share your skills here

Re: NKTableView, NKFile, NKNKFileDownloader

Postby thedjmixman » Sun Jul 04, 2010 3:13 pm

Sorry about the wait. My HDD replacement finally arrived and I spent the day filling it with operating systems again.

Here goes.
I started off with Big-O's excellent SQLite guide here. I used the FF extension to create a database that has all the tables that I am downloading, plus 1 for user credentials. Therefore, the entire DB never gets erased (or replaced) with the running of the app. I just empty some tables with each app load after the app has detected that the user exists in the DB.
I'll explain that in brief..
The app checks the DB for the user and if there is none, it shows a login form. I use onchange to trigger an ajax call. The user/pass are checked on the server and if the credentials are corrent, it returns the user ID. If the app gets back an error message, it displays it. If not, and the returned data is > 0, the user ID and username are stored in the DB and the app refreshes main.html

-App pretty much starts again and from now on, loads with the user logged in, and a different sequence of functions are run-

I display a welcome message and a button to trigger the JSON download.

If you are using a framework, you wont need the full Ajax listed here. I havent used one inside NK yet as I dont feel that the things I do warrent the bloat.

playbutton was my user button to trigger the Ajax
progress and updateLabel was created when the button was pressesd

Code: Select all
function downloadLatestProductsAction(){
    playButton.hide();
    progress.setPosition(20);
    progress.show();
    updateLabel.show();

   
var database2 = new NKSQLite();
database2.openDatabase("ifeed.sqlite", 0);
database2.executeSQL("DELETE FROM cart");
//Do an eqecuteSQL for every table that you wish to empty.
database2.closeDatabase();


progress.setPosition(25);

     http_request = false;
      if (window.XMLHttpRequest) {
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html');
         }
      }

     var xmlDoc;
     var url="http://www.YourURL.com/iphone_data.php";
     parameters = "user_id=" + myRows[0].user_id + "&random="+Math.random();
          // I send the user ID so that the product prices returned match the user's region.  You could send any parameters here.
    
      http_request.onreadystatechange = productResult;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.send(parameters);

   }

   function productResult() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            var productDetails = http_request.responseText;

      progress.setPosition(50);
      updateLabel.setString("Processing Data...");
      evaluateJson(productDetails);

   } else {
      NKAlert("Error", "There was a problem updating the product list.  Please restart & try again.");
   }
      
    }
}





//Because JSON is an oblect, not an array, The function below will let us count it's contents

function countProperties(obj) {
  var prop;
  var propCount = 0;
 
  for (prop in obj) {
    propCount++;
  }
  return propCount;
}






function evaluateJson(productDetails){

updateLabel.setString("Populating Product Categories...");
progress.setPosition(60);
var returned_data = eval('('+productDetails+')');



var databaseCats = new NKSQLite();
databaseCats.openDatabase("ifeed.sqlite", 0);

for (i=1;i<=countProperties(returned_data["categories"]);i++)
{
databaseCats.executeSQL('INSERT INTO categories (cat_title, cat_id) VALUES ("'+returned_data["categories"][i][0]+'", "'+returned_data["categories"][i][1]+'")');
}

progress.setPosition(65);
updateLabel.setString("Populating Products...");



// Keep doing these FOR loops and progress bar / label; updates until you have populated the DB

progress.setPosition(100);
progress.hide();
NKLog("Added Last DB Info");
progress.hide();
updateLabel.hide();

NKStartPreloadingPage("main2.html");
}





Please excuse the formatting. I have stripped out a lot of the stuff in there to show you what is relevant.

Once main2.html is loaded, I set the nav & tabs.. main.html is never shown again becuase it is not referenced anywhere in the app.
My version of main2.html goes on to list all of the categories that were downloaded. For example....


Code: Select all
var databaseCategories = new NKSQLite();
databaseCategories.openDatabase("ifeed.sqlite", 0);
databaseCategories.executeSQL("SELECT * from categories");
var categories = new Array();
categories = databaseCategories.getResults();

for (i=0;i<categories.length;i++)
{
   catList = (catList+'<div class="section"><h1>'+categories[i].cat_title+'</h1><div class="cells">');
}


Also, if any of this seems a little backward... I have not used JSON before. I had to do a little research on it and the above is what I came up with. I decided to go the JSON route becuase it read on here that it is much faster than dealing with XML.

If you have never used JSON before either, I found this to be of help...
http://json.parser.online.fr

I edited it at the top until I got the kind of result that I was looking for then copied it from the left, put that in to a PHP page then made it dynamic by pulling what is to be the JSON content from the site's MySQL.

Here is part of my JSON, some of which is shown in the code examples:

Code: Select all
{

"currency": "GBP",

"categories": {
"1": ["Racing", 17],
"2": ["Breeding", 21],
"3": ["Competition & Leisure", 27],
"4": ["KER Products ", 28],
"5": ["Specialist feeds ", 29],
"6": ["Fibre Products", 31],
"7": ["Dog Products", 32]
} ,

"cards": {
"1": [3333, 10],
"2": [6969, 11]
} ,


"cart": {
"1": [60, 29, 1],
"2": [37, 29, 1],
"3": [51, 28, 1]
}

}
User U1318808401
User avatar
thedjmixman
NimbleKit Expert
 
Posts: 60
Joined: Sun Jun 13, 2010 4:05 am
Location: Kent, UK

Re: NKTableView, NKFile, NKNKFileDownloader

Postby mouts13 » Tue Jul 06, 2010 2:20 am

thank you ;-)
User avatar
mouts13
 
Posts: 93
Joined: Sun Feb 14, 2010 1:06 am

Re: NKTableView, NKFile, NKNKFileDownloader

Postby mouts13 » Mon Jul 19, 2010 5:10 am

I managed to do what I wanted for cons I still have a problem, I would use NKTableView to view the content of my database, but nothing appears, you have an idea.

Here is my code main.html
Code: Select all
var tabController = new NKTabBarController;
   tabController.setTabBarForPage("main.html", "DataBase View", "");
   tabController.setTabBarForPage("tableView.html", "TableView", "");


var database = new NKSQLite();
   database.openDatabase("tableDB.sqlite", "no");
   database.executeSQL("CREATE TABLE radio(pk INTEGER PRIMARY KEY, category TEXT, title TEXT, subtitle TEXT, image TEXT, right_image TEXT, js TEXT)");
   

var req = new XMLHttpRequest();
   req.open("GET", "fichier.json", true);
   req.onreadystatechange = myCode;   // the handler
   req.send(null);
   
function myCode()
{


      if (req.readyState == 4)
      {
      
             var doc = eval('(' + req.responseText + ')');
           var genre;   
           for(i=0 ; i<countProperties(doc.Radio); i++)
           {
               database.executeSQL("INSERT INTO radio(category,title,subtitle,image,right_image,js) VALUES('"+doc.Radio[i].category+"','"+doc.Radio[i].title+"','"+doc.Radio[i].subtitle+"', '"+doc.Radio[i].image+"', '"+doc.Radio[i].right_image+"', '"+doc.Radio[i].js+"')");
           }
          
   database.executeSQL("SELECT * FROM radio");
   results = database.getResults();
   for (i=0; i<results.length; i++)
   {
      document.body.innerHTML+="<p>"+results[i].category+" | "+results[i].title+" | "+results[i].subtitle+"</p>";
   }
       database.closeDatabase();
          
}
          

}

function countProperties(obj) //Cette fonction compte le nombre de radio
{
  var prop;
  var propCount = 0;

  for (prop in obj) {
    propCount++;
  }
  return propCount;
}


Screenshot of the page main.html most strikingly, the data are recorded
Image

Here is my tableView.html
Code: Select all
var ListeRadio = new NKTableView();
   ListeRadio.init(0, 0, 320, 460);
   ListeRadio.bindToDataBase("tableDBsqlite", "radio");
   ListeRadio.show();
      


Here is the page to view the results as TableView but nothing appears you have a solution???
Image

my json file
Code: Select all
{
  "MesFiches": "Fichier",
  "Radio": [
      {
          "category": "RAP",
          "title":"Skyrock",
        "subtitle":"radio sur paris",
        "image":"",
        "right_image":"",
        "js":""
      },
      {
          "category": "POP",
          "title":"Maritima",
        "subtitle":"1er radio sur martigues",
        "image":"",
        "right_image":"",
        "js":""
      },
      {
          "category": "POP",
          "title":"Radio Star",
     "subtitle":"100% Marseillais Zik",
      "image":"",
      "right_image":"",
       "js":""
      },
     {
          "category": "ROCK",
          "title":"France Bleu Prov",
        "subtitle":"Du Rock non Stop !!!",
        "image":"",
        "right_image":"",
        "js":""
      }
   ]
}
User avatar
mouts13
 
Posts: 93
Joined: Sun Feb 14, 2010 1:06 am

Re: NKTableView, NKFile, NKNKFileDownloader

Postby stupha » Mon Jul 19, 2010 11:35 am

It looks like you may have the database file name incorrect in your bindToDataBase() call (it is missing the period).
Try changing it to:

ListeRadio.bindToDataBase("tableDB.sqlite", "radio");

I came unstuck in the same way by not providing the correct database file name to bindToDataBase().
User avatar
stupha
 
Posts: 39
Joined: Thu Jul 15, 2010 12:10 pm
Location: Bude, Cornwall, UK

Re: NKTableView, NKFile, NKNKFileDownloader

Postby mouts13 » Mon Jul 19, 2010 12:29 pm

stupha wrote:It looks like you may have the database file name incorrect in your bindToDataBase() call (it is missing the period).
Try changing it to:

ListeRadio.bindToDataBase("tableDB.sqlite", "radio");

I came unstuck in the same way by not providing the correct database file name to bindToDataBase().


Actually there was a problem with the name of the database by cons I corrected but the problem remains the same, I do not understand ...? :(
User avatar
mouts13
 
Posts: 93
Joined: Sun Feb 14, 2010 1:06 am

Re: NKTableView, NKFile, NKNKFileDownloader

Postby mouts13 » Mon Jul 19, 2010 1:04 pm

I run a test with the Firefox tool Sql Manager, I created a table and add items myself it works ... By cons I do the same thing since my main.html page that does not work, I am forced to use Firefox Sql Manager?
User avatar
mouts13
 
Posts: 93
Joined: Sun Feb 14, 2010 1:06 am

Re: NKTableView, NKFile, NKNKFileDownloader

Postby mouts13 » Sat Jul 24, 2010 4:33 pm

Someone can you help me?
User avatar
mouts13
 
Posts: 93
Joined: Sun Feb 14, 2010 1:06 am

Re: NKTableView, NKFile, NKNKFileDownloader

Postby sunny » Sat Jul 24, 2010 10:30 pm

send me the sample project which represents the issue, I'll see what's wrong.
User avatar
sunny
Staff
 
Posts: 1786
Joined: Sat May 30, 2009 5:18 am
Location: kiev, Ukraine

Re: NKTableView, NKFile, NKNKFileDownloader

Postby marcw7174 » Sat Jul 24, 2010 10:59 pm

are you trying to display the tableview on the same page as you are inserting the json into the sql, if so I had that problem and would be happy to post some sample code if you need?
marcw7174
 
Posts: 38
Joined: Thu Jul 01, 2010 10:38 pm

Re: NKTableView, NKFile, NKNKFileDownloader

Postby mouts13 » Sun Jul 25, 2010 4:57 am

marcw7174 wrote:are you trying to display the tableview on the same page as you are inserting the json into the sql, if so I had that problem and would be happy to post some sample code if you need?


@ Marcw7174: I want to see your code please
User avatar
mouts13
 
Posts: 93
Joined: Sun Feb 14, 2010 1:06 am

PreviousNext

Return to How To...

Who is online

Users browsing this forum: No registered users and 1 guest