I am going to post some code I wrote to directly save javascript objects to an SQLite database using Google gears. It's an example of writing a single method to save any type of object and I think changes the way we think about saving objects. Correct me if I am wrong, but this seems to be the least amount of code from any programming language for saving objects to an SQL database.
- function addSQLite(obj, table) {
- query = "INSERT INTO " + table + " VALUES (";
- z = 0;
- for(x in obj) {
- switch (typeof(obj[x])) {
- case 'string':
- if(z>0) query += ",";
- query += "'" + obj[x] + "'"
- break;
- case 'number':
- if(z>0) query += ",";
- query += obj[x];
- break;
- }
- z++;
- }
- query += ")"
- db.execute(query);
- }
This basically just puts a string together based on any objects properties. All you would have to do it make sure there is a table in the database with the same properties and you have yourself an incredibly versitale SQL function.
No comments:
Post a Comment