Last active
December 28, 2015 21:39
-
-
Save itsmonktastic/7566503 to your computer and use it in GitHub Desktop.
Example code to use FMDB in a Trigger.io Forge module.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <script src="js/jquery-2.0.2.js"></script> | |
| <script src="js/bootstrap.js"></script> | |
| <script src="js/main.js"></script> | |
| <link href="css/bootstrap.css" rel="stylesheet"> | |
| </head> | |
| <script type="text/javascript"> | |
| function output(data) { | |
| $('#out').append('<pre>'+JSON.stringify(data)+'</pre>') | |
| } | |
| function testDB() { | |
| forge.internal.call('tm_test_dependencies.setupDB', {}, function () { | |
| forge.internal.call('tm_test_dependencies.testQuery', {}, output); | |
| }); | |
| } | |
| </script> | |
| <body> | |
| <button onclick="testDB()">Test DB</button> | |
| <div id="out"></div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "tm_test_dependencies_API.h" | |
| #import "FMDatabase.h" | |
| @implementation tm_test_dependencies_API | |
| // | |
| // Here you can implement your API methods which can be called from JavaScript | |
| // an example method is included below to get you started. | |
| // | |
| + (void)setupDB:(ForgeTask*)task { | |
| NSString *dbPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"dogs.db"]; | |
| FMDatabase *db = [FMDatabase databaseWithPath:dbPath]; | |
| [db open]; | |
| BOOL createSuccess = [db | |
| executeUpdate:@"CREATE TABLE IF NOT EXISTS dogs(name text primary key)"]; | |
| [ForgeLog i:[NSString stringWithFormat:@"create: %hhd", createSuccess]]; | |
| BOOL insertSuccess = [db | |
| executeUpdate:@"INSERT INTO dogs (name) VALUES (?);", @"fido"]; | |
| [ForgeLog i:[NSString stringWithFormat:@"insert: %hhd", insertSuccess]]; | |
| [db close]; | |
| [task success:nil]; | |
| } | |
| // This will be callable from JavaScript as 'tm_test_dependencies.showAlert' | |
| // it will require a parameter called text | |
| + (void)testQuery:(ForgeTask*)task { | |
| NSString *dbPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"dogs.db"]; | |
| FMDatabase *db = [FMDatabase databaseWithPath:dbPath]; | |
| [db open]; | |
| FMResultSet *results = [db executeQuery:@"SELECT * FROM dogs"]; | |
| NSMutableString *resultString = [NSMutableString string]; | |
| while ([results next]) { | |
| [resultString appendString:[results stringForColumn:@"name"]]; | |
| [resultString appendString:@"\n"]; | |
| } | |
| [db close]; | |
| [task success:resultString]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment