Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

SQLITE Wrapper with array?

$
0
0

Hello, Im trying to write a Sqlite database wrapper for my project, and everything seems to be fine- until now. (For iPhone, SDK 3.5.1)

I really like the approach I use on our backend, where we bind each value before we put it in to the db-table. Since that's not possible, I figured that maybe it's possible to insert and update with an array instead of appending variables in the execution.

Here's my code, and the problem Im having is that this seems to work with INSERT, but not UPDATE. Why?

this.insert = function(table, data)
    {
        try
        {
            var database = Ti.Database.open(DATABASE_NAME);
 
            var sql = "INSERT INTO " + table + " (";
 
            var columns     = [];
            var values      = [];
            var questions   = [];
 
            for(var col in data)
            {
                columns.push("`" + col + "`");
                questions.push("?");
                values.push(data[col]);
            }
 
            sql += columns.join() + ") VALUES(" + questions.join() + ")";
 
            database.execute(sql, values);
 
            database.close();
            database = null;
        }
        catch(e)
        {
            Ti.API.info("Exception while inserting data: " + e);
        }
    };
 
    this.update = function(table, data, where)
    {
        try
        {
            var database = Ti.Database.open(DATABASE_NAME);
 
            var sql = "UPDATE " + table + " SET ";
 
            var columns     = [];
            var values      = [];
            var wheres      = [];
 
            for(var col in data)
            {
                columns.push("`" + col + "`=?");
                values.push(data[col]);
            }
 
            for(var col in where)
            {
                wheres.push("`" + col + "`=?");
                values.push(data[col]);
            }
 
            sql += columns.join() + " WHERE " + wheres.join() + ")";
 
            Ti.API.info(sql);
 
            database.execute(sql, values);
 
            database.close();
            database = null;
        }
        catch(e)
        {
            Ti.API.info("Exception while updating data: " + e);
        }       
    };
As you can see, I want to put the values through a array; database.execute(sql, values);

But why does this only work when Inserting and not updating? Anyone else who might have a better solution than my attempt?


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>