Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 15    Views: 106

Jlentriken
Aspiring developer
Profile
Posts: 246
Reg: Dec 26, 2012
Palm Beach, Gol...
12,110
11/13/13 03:32 AM (12 years ago)

Database question.

Hi, I am trying to make an app that needs to store nearly 20 records in a database each time the user logs a flight. Is there anyone that can point me to a good place to learn how to do this (in iOS) with a database that is contained in the app so that the app can be used offline or in flight mode? Or if someone could teach me i'd be very grateful. Thanks, Jeremy
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
11/13/13 04:51 AM (12 years ago)
I'd suggest learning SQLite rather than core data, only because SQLite is a more 'universal' database used on iOS, Android and Desktop. If you ever port to Android in the future it might help using the same basic infrastructure. And I think you mean 20 fields, one record. But even if it is 20 records, it's all simple once you know how. 2, 20, 200 won't mean much once you figure it out. :) http://maniacdev.com/2011/11/tutorial-easy-ios-databases-with-sqlite-and-fmdb Cheers! -- Smug
 
Jlentriken
Aspiring developer
Profile
Posts: 246
Reg: Dec 26, 2012
Palm Beach, Gol...
12,110
like
11/13/13 05:13 AM (12 years ago)
Thanks Smug, Yes i do mean 20 fields in the 1 record. but there will potentially be hundreds of records. Been a long time since i did Computer Science sorry. Will give it a go. Thanks again Smug, Jeremy
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/13/13 07:50 AM (12 years ago)
Smug's absolutely right on this one - SQLite is what you'll need. I have a working database class for iOS. Android is a bit trickier to implement, and I haven't gotten around to it yet. The hope is that they (or something like them) make it into a future Buzztouch core download for your projects.
 
Jlentriken
Aspiring developer
Profile
Posts: 246
Reg: Dec 26, 2012
Palm Beach, Gol...
12,110
like
11/13/13 09:21 AM (12 years ago)
Smug and Chris, you are two of the most helpful people I've come across on BuzzTouch and as such I trust you guys the most, so thanks for your help. I feel so out of my depths here. I had a look at the link you sent Smug, and all others that I have found in google, but it just can't find anything similar to what I'm doing, and am struggling to learn how to do it. I'm using story boarding for the app, and want 25 fields in the database. I have one screen where the user fills out a few fields (up to 18 per record) and adds them to the db with a button press. On another screen I want to display the contents of the most recent entry from each field in a label. There area few yes/no questions, where if 'yes' is picked the current date should be placed in the field. Am I making sense? If someone knows how to best go about doing this, or the best place to look, please help me out. I'm willing to give out vouchers when I have the app finished. Thanks, Jeremy
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
11/13/13 04:21 PM (12 years ago)
Fear not; the db tutorial link (http://www.highoncoding.com/Articles/836_Persisting_iOS_Application_Data_in_SQLite_Database_Using_FMDB.aspx) may not be 'exactly' what you're doing, but I think you should go through it anyway… The kinds of data that you're going to be holding in your database will probably end up being 'mostly' string data, even if the string is a number. Unless you plan on doing mathematics, it's not a big deal. Same with Boolean. A string 'Y' or 'N' is as good as anything else once you understand it. All data is merely 'data', don't get wrapped up in it just yet. First learn to read and write to the database. the rest will flow, One step at a time. Cheers! -- Smug
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/13/13 05:42 PM (12 years ago)
For Jeremy or anyone else that's interested, here's my "BT_database" class for iOS. https://www.dropbox.com/s/mu5ps0uo6v7r95r/BT_database.zip To use, add this line to your imports for the plugin you're working on: #import "BT_database.h" Then, add something like this in the appropriate place in your code. Note that I'm typing this by hand, so hopefully there's no typos! Also, this is just a few of the more important commands in the class. Check out the BT_database.h file for a complete list. BT_database *db = [BT_database new]; db.databaseName = @"anyNameYouLike"; /* creates a database (if needed) and associated table uses NSArray of column names. This NSArray should be filled with NSArray objects like so: NSArray *column1 = [[NSArray alloc]initWithObjects:@"fullName", @"TEXT", nil]; NSArray *column2 = [[NSArray alloc]initWithObjects:@"theAge", @"INTEGER", nil]; NSArray *columnNames = [[NSArray alloc]column1, column2, nil]; */ NSArray *column1 = [[NSArray alloc]initWithObjects:@"fullName", @"TEXT", nil]; NSArray *column2 = [[NSArray alloc]initWithObjects:@"theAge", @"INTEGER", nil]; NSArray *columnNames = [[NSArray alloc]column1, column2, nil]; [db createDataBasewithTable:@"myTableName" andColumnNames:columnNames]; /* Inserts data into database. Takes NSString with table name, and NSDictionary with values and keys, both of which should be NSString objects. */ NSMutableDictionary *columnsAndValues = [NSMutableDictionary new]; [columnsAndValues setValue:@"Chris" forKey:@"fullName"]; [columnsAndValues setValue:@"99" forKey:@"theAge"]; [db insertIntoTable:@"myTableName" withColumnsAndValues: columnsAndValues]; //updates the records in the table for all entries where a certain column is equal to a specified value NSMutableDictionary *colsAndVals = [NSMutableDictionary new]; [colsAndVals setValue:@"34" forKey:@"theAge"]; NSMutableDictionary *filteredData = [NSMutableDictionary new]; [filteredData setValue:@"Chris" forKey:@"fullName"]; [db updateTable: @"myTableName" setColumnsAndValues: colsAndVals whereColumnsEqualValues: filteredData]; //returns an array of dictionaries of all data from the table. Dictionary will consist of: //columnname : value NSArray *theData = [db getAllDataFromTable:@"myTableName"]; for (int i=0; i<theData.count; i++) { NSDictionary *thisRow = [theData objectAtIndex:i]; NSLog(@"name=%@ and age=%@", [thisRow valueForKey:@"fullName"], [thisRow valueForKey:@"theAge"]); }
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/13/13 05:43 PM (12 years ago)
I should also add, I haven't tested this on the new iOS 7 beta project. Don't think it will break, but there might be a line of code in there somewhere that causes it to.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
11/13/13 06:11 PM (12 years ago)
Very nice donation, Chris! Thanks!! Are you playing with the BTv3 iOS core yet? Cheers! -- Smug
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/13/13 06:12 PM (12 years ago)
A little bit here and there. Been kinda busy with other projects to get too much into it.
 
Jlentriken
Aspiring developer
Profile
Posts: 246
Reg: Dec 26, 2012
Palm Beach, Gol...
12,110
like
11/13/13 06:41 PM (12 years ago)
Hi Chris &Smug, Thanks for the link I will take a good look at it Smug. Thanks for sharing your database class Chris. Will it work for non buzztouch apps? Thanks, Jeremy
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/13/13 06:48 PM (12 years ago)
Yep - it's just a standard objective-c class file.
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/13/13 06:49 PM (12 years ago)
Actually, I take that back. It does reference the BT_item and BT_debugger classes. So you'd need to drag those into your project if not using a Buzztouch project.
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
11/14/13 08:51 AM (12 years ago)
@chris Nice class. Thanks for sharing. Will test it out on the 3.0 core sometime. Just so I understand better - this class works with the BT Item and basically builds an array that is stored locally. This is not a SQLLite dbase, it's just a JSON array of items, correct? @Jeremy - work thru that FMDB tutorial. Its a good one and the concepts you'll learn there will help a ton. Load the sample project at the bottom of the tutorial and study it. You have to crawl before you walk with this stuff, it's complex but your knowledge builds over time.
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
11/14/13 08:56 AM (12 years ago)
It's a true SQLite database. I don't remember where it makes use of the BT_item class, but that's not a major component of the code. I may have just left the import in by mistake frankly. All data is saved locally on the device to the SQLite database as data types you define when you create the table.
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
11/14/13 09:33 AM (12 years ago)
Gotcha, thanks again for sharing it. Quite useful and would be great to see it packaged in the core. I feel like David hinted at it not long ago, so having working code to study already is spectacular :)
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.