9 Replies Latest reply on Aug 27, 2016 2:33 AM by VectorP

    Sqlite data storage from server

    sawank18707653 Level 1

      I am building an android app in cross platform, in my app when i store data in sqlite from remote server it gets stored in a single column of the table, not storing in particular columns. i am getting data in JSON object and also i tried to JSON.strinify..but still.whole data stores in a single column.

       

       

        • 1. Re: Sqlite data storage from server
          VectorP Level 4

          So, (either) your json object is wrong, or your table creation, or your query.

          What exactly is your question? And how are forum participants supposed to answer it, without any further detail?

          • 2. Re: Sqlite data storage from server
            sawank18707653 Level 1

            here is the code...btw my query is working and can store data in sqlite too..but not able to store particular data in particular column

             

            $("#xxx").click(function()

              {

             

              var e = $("#mob").val();

              var p = $("#key").val();

              myDB.transaction(function(transaction)

              {

              transaction.executeSql('CREATE TABLE IF NOT EXISTS User_data (data)', [],

              function(tx, result)

              {

              navigator.notification.alert("table created");

              },

              function(error)

              {

              navigator.notification.alert("error, table exists");

              });

              });

             

              $.ajax

              ({

              url: URL,

              type: "GET",

              datatype: "json",

              data: { type:'login', phone: e, name: p },

              ContentType: "application/json",

              success: function(response)

              {

             

                    myDB.transaction(function(transaction)

              {

                          var valuesInArray = [JSON.stringify(response)];

             

             

                        

             

             

              transaction.executeSql("INSERT OR REPLACE INTO User_data (data) VALUES (?)", valuesInArray,

              function(tx, result)

              {

                navigator.notification.alert("registerd");

              },

              function(error)

              {

              navigator.notification.alert("error, table exists");

              });

              });

              },

                  error: function(e)

                  {

                  

                    alert('Got ERROR: ' + JSON.stringify(e));

                  }

                  });

              });

            • 3. Re: Sqlite data storage from server
              VectorP Level 4

              You are creating a table with one column, only.

              And using stringify(), you turn your json data into one string as well, so that fits nicely.

               

              It might not be what you had in mind, but the app performs exactly as coded.

               

              BTW:

              is this still the project you started weeks ago, where you want one single database tuple stored in a table, without further relationships?

              If so, wouldn't you (again) consider using localStorage (or perhaps a flat file) instead of a database? There would be two reasons for this:

              1. It makes no sense to store only 1 tuple in 1 row in a relational database, because none of the database advantages will be used and additional overhead is all you get.

              2. You seem to be in over your head. Better keep it simple, unless this 'project' is just meant to be a learning experience for you (in which case I recommend studying the official docs much more than you are doing now).

              • 4. Re: Sqlite data storage from server
                sawank18707653 Level 1

                if i create table with multiple column, it will store entire data in one column only..can you tell me how can i store particular data in one column.

                 

                appriciate your help

                • 5. Re: Sqlite data storage from server
                  sawank18707653 Level 1

                  getting error in this :-

                   

                  $.ajax

                    ({

                   

                    url: "URL",

                    type: "GET",

                    datatype: "json",

                    data: { type:'login', phone: e, name: p },

                    ContentType: "application/json",

                    success: function(data)

                    {

                    

                          myDB.transaction(function(transaction)

                    {

                                $.each(data, function(i,item)

                                {

                                  alert("error");

                                    transaction.executeSql('INSERT INTO User_data (id, name, email_from, phone) VALUES ("' + item.id + '", "' + item.name + '", "' + item.email_from + '", "' + item.phone + '")');

                          });

                   

                   

                    });

                    },

                        error: function(e)

                        {

                        

                          alert('Got ERROR: ' + JSON.stringify(e)); //error : {"ready state":4,"responseText":"","status":404,,"statusText":"Not Found"}

                        }

                        });

                  • 6. Re: Sqlite data storage from server
                    VectorP Level 4

                    You have:

                    $.each(data, function(i,item)....

                    What exactly does the object 'data' contain at that point?

                     

                    getting error in this :-

                    ...

                    Are you referring to this?:

                    alert("error");

                    If so, you are alerting that string in the success callback function, which might be quite confusing.

                    • 7. Re: Sqlite data storage from server
                      sawank18707653 Level 1

                      Capture.PNG

                      this is the data i am getting in the object'data'

                      • 8. Re: Sqlite data storage from server
                        sawank18707653 Level 1

                        Screenshot_1472284180.png

                        Getting this data in phonegap application form object'data'

                        • 9. Re: Sqlite data storage from server
                          VectorP Level 4

                          So, you have a three-dimensional array in your 'data' object!

                          The 'each()' will loop through the outer dimension of the object's array, which is again an array with ust one element.

                           

                          In other words: not only was your CREATE TABLE query wrong, but your JSON object is most likely not what you want, either.