SQLite data into simple UITextFields

Discussion in 'iOS Programming' started by brlapp, Apr 5, 2011.

  1. brlapp macrumors newbie

    Apr 5, 2011
    I am trying to connect to a SQLite database and have a method that specifies a specific row from the database (the first column in the database is “ID” and is a primary key) then extract the information from a few other columns in that row and display them in text fields.

    This will be used for a simple Trivia game I am making; I will later make a random method that will choose the row at random.

    I have been struggling with this problem for several weeks and I have been through loads of tutorials but all of them deal with displaying the data in a table view, I want to display it simply on text fields in a View based app.

    I am fairly confused at this point so any help starting from loading the database to displaying the data in the text fields would be GREATLY APPECIATED!

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    You need a model object that is used in-memory to represent your row of data. I often use an NSDictionary but it could be something else. Load the values from the columns into the dictionary. Then in your view controller read the values from the dictionary and set the text in your labels or other views.

    I recommend that you use FMDB for your database access. Also, keep your database access code separate from your view controller code. I usually have a single class that manages the database. It owns a FMDB database. It provides methods for reading or writing the database. Your view controller gets its data from this class.
  3. brlapp thread starter macrumors newbie

    Apr 5, 2011
    Thanks for the reply PhoneyDeveloper

    Your solution sounds like a new one to me, could you provide any example code so I can better understand how you are loading the database and extracting the data from it?

    Thanks again for your help!
  4. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I have a number of methods like this in my database class. The code that calls this gets back a dictionary that contains records for the various columns in the database and reads those records using the appropriate keys.

    - (NSDictionary*)buildRecord:(NSString*)inSourceID
    	NSMutableDictionary* source = [[[NSMutableDictionary alloc] init] autorelease];
    	BOOL			ok = YES;
    	FMResultSet*	results = [mDatabase executeQuery:@"SELECT identifier, data, author, title FROM Sources WHERE source_identifier = ?", inSourceID];
    	if (! results) ok = NO;
    	if (ok && [results next])
    		NSString*	t;
    		t = [results stringForColumnIndex:0];
    		if (t)
    			[source setObject:t forKey:kSourceIDKey];
    		t = [results stringForColumnIndex:1];
    		if (t)
    			[source setObject:t forKey:kSourceDataKey];
    		t = [results stringForColumnIndex:2];
    		if (t)
    			[source setObject:t forKey:kSourceAuthorKey];
    		t = [results stringForColumnIndex:3];
    		if (t)
    			[source setObject:t forKey:kSourceTitleKey];
    	[results close];	// we're done with it
    	return source;
  5. brlapp thread starter macrumors newbie

    Apr 5, 2011

    Anybody have any other ideas? I tried and tried PhoneyDevelopers solution, but it is full of erros and seems poorly written,

    any ideas/help would be great!
  6. chown33 macrumors 604

    Aug 9, 2009
    1. Post the text of the error messages you get.

    2. Explain why you think it's poorly written.

    I hope you realize he didn't hand you a complete ready-to-paste solution, with all variables predefined. He simply used the code as an example or outline of an approach you could use.

    If you want a complete ready-to-paste solution, with all variables predefined, then you have to ask for it. Before asking, however, you should be aware that you're asking for a completely customized tutorial on how to use FMDB. Maybe you can look for existing tutorials that show uses of FMDB first.


    And I'm still not sure exactly what problem you're having. Your first post said you wanted to put the data into text fields. So please explain exactly how you are unable to put the data into text fields.

    Do you have text fields defined as instance variables? Are they IBOutlets? Are they connected to a nib? Do you have code that retrieves data from the database? Post your code. Or is the problem that you are unable to figure out even one line of code that pulls data and puts it into text fields?
  7. brlapp thread starter macrumors newbie

    Apr 5, 2011
    Sorry Chown33

    Your right I was very unclear with my errors. I'm just beyond frustrated with this problem, I've tried literally a dozen different solutions from people's posts with no luck. I really do need a more complete answer. I simply want to connect to a sql database in my resources folder, specify a row and populate several uitextfields with the data from the columns. It really should be very simple I just don't know the syntax to do so.

    I have imported the sqlite3.dylib and made/tested all my outlets/actions.

    I would like to do something exactly like this tutorial but not have the database populate a table view but instead text fields in a view based app.

    I am truly desperate for a solution, any help would be GREATLY APPRECIATED!

  8. chown33 macrumors 604

    Aug 9, 2009
    Describe exactly where you're having the problem.

    Describe exactly which syntax you're unable to figure out.

    Since you're not using a table view, post your actual code, showing all your text field IBOutlets and controls. Specify which control or input field is used to specify the row to retrieve.

    The data from the database is retrieved once and stored as Animal objects in an NSMutableArray. So if you ignore all the places that use the data to set properties of a UITableViewCell, exactly where is your problem?

    Do you realize that the rows of the database have been read into Animal objects stored in an NSMutableArray?

    Do you know how to refer to a specific indexed Animal object in the NSMutableArray?

    Once you have the Animal object for a specific index, do you know how to get the Animal's properties?

    Once you have an Animal's property, do you know how to set a text field to that value?

    Have you completed the tutorial exactly as given, using the table view? In other words, do you understand all of what the tutorial does in its original form, because without that understanding, modifying it strikes me as rashly premature.

    What other tutorials or books have you completed on iOS programming, or programming in general? If it's few or none, you're probably in far over your head, and don't yet have the skills that would permit you to modify the tutorial's existing code. If you don't have the fundamental skills, then your estimation that this task "should be very simple" is based on little more than wishful thinking.

    If you don't have the fundamental skills, then you need to acquire them in the usual way: studying and practicing. That means following tutorials or books exactly as given, doing skill-appropriate exercises as given, and generally not biting off more than you can chew. Practicing with simpler tasks is very important. Making small changes to things that already work is good practice. Trying to create completely new things far beyond your skill level is a waste of time and effort. If you get totally stuck at some point, this should be a clue you haven't acquired some necessary skill, and you should go back to the study materials and acquire it before trying to proceed.

Share This Page