I've created a phonegap app for iOS using jQuery & JQTouch. Info is pulled from a test database and displayed on the webpage on the iPhone simulator. However to get this to work, I pre-made/populated the database and manually copied it across to the correct folder so I could connect to it in my code.
When installed on my iPhone for testing I need to create the database and populate it in code on first run (ultimately this method should make it compatible with android rather than trying to copy the database manually in Objective C as in 开发者_高级运维this example: http://groups.google.com/group/phonegap/browse_thread/thread/ca6c85f7d6c3976c/017cdbf51c70585a). My idea is to pull the info from a flat CSV file or JSON object (included in the package) into a local array, then create the database table, and iterate through the array inserting the info into the database.
Is this a sensible approach? I'm trying to go for minimal complexity! It seems easier to create and maintain a CSV file of my questions rather than a JSON file (which seems like adding extra complexity for the sake of it) - any thoughts? Finally, does anyone have an idea of how to extract the info from a CSV file? I found this JQuery library (http://code.google.com/p/js-tables/wiki/CSV) which looks promising - is there a better method?
Thanks for any help, I don't want to jump in before getting some advice! Nick
First, consider the size of your database file. Test in simulator and on your desktop browser will not be nearly what you encounter on the devices. I found that a 5meg text file was intolerable on older devices.
Second, I would advocate to use json always since it is much easier to use in JS and you do not need to use external libraries.
One approach you might consider is to store your "db records" in files that get bundled into your app package. Then when you need to "record" you can grab the file with ajax, read it, store it in local DB if you want for later use, etc.
$.ajax({
url: asset_path + "/" + filename,
success : function(data) {
// data is your json object
},
error : function(request,error) {
// file not found
},
async: false,
dataType: "json"
});
You might get some overhead with the file size if you generate json files as compared to CSV with all the string handling and brackets etc.
If this is going into a Phonegap package or being put into a app package, these files will be compressed to help alleviate size that way.
精彩评论