4

I am attempting to write an application in which I have two text fields that I will be using for input. The input taken from the text fields are being initialized as a string. So what I want to accomplish is:

  • Create an SQLite database (file) that will be saved within the Xcode project (I'm not sure where it can be saved to, but I would need to be able to read it and write to it).
  • I then want to place the strings (from the text field inputs) into the SQLite table, which will only have two fields (for simplicity). I'm using a button to accept the input from the text fields, place the input into strings, and then put the value of the strings into a label(s).
  • I ultimately will want to place the strings from the text fields into the table, and then read the table to "grab" the strings and display them into the labels. All of this would be done by the click of a button.

I realize this is very specific, but I have been having a difficult time finding anything simple to accomplish this. I am very new to iOS development and I appreciate if anyone could be as specific and detailed as possible, or at least point me to some resources that would teach me how to accomplish this.

I am using Xcode v4.3.2 and I do not use Storyboard or ARC. I tried to be as detailed as possible, but as you can, my goal is really simple.

I'm tapped out and I appreciate all the help I can get.

Thanks! -Matt

alex
2,48424 silver badges32 bronze badges
asked May 3, 2012 at 19:29
4
  • so where is your problem in detail? do you know sql in general? is it just getting started in ios? Commented May 3, 2012 at 19:35
  • I generally know SQL. Well enough that I could likely figure things out. I am really new to iOS still, yes. my main focus, as stated, is that I want to use a button to create a table. I don't know where it will save the table in the App/Xcode, but I need to read from and write to the table. Ultimately, when I get the values back from the table (only 2 rows for simplicity) I want to put the values I get back from the table into a couple of labels. Let me know if I'm not clear enough. Thanks again! Commented May 3, 2012 at 19:41
  • 2
    This is a little vague. You need to outline what you've tried so far, what you've got working, where precisely you're having troubles, post sample code that you've tried so far, etc. If you don't know how to do SQLite programming or some other aspect of iOS programming, that's beyond the scope of a simple SO question and you might want to google for online resources (e.g. "ios sqlite tutorial"). If you have problem with some specific code that you've written, we're happy to help, but for general "how do I" questions, SO is not the place. See the FAQ. Commented May 4, 2012 at 4:05
  • @Rob, I did a search for "ios programming sqlite tutorial" and ended up here. SO is great sometimes, but more and more often I've recently had to sift through a bunch of SO related search results to find what I'm looking for. Commented Nov 5, 2013 at 17:49

2 Answers 2

8
  • creating a database file: simply drop your data.sqlite file in your xcode project, as you drop any other class or image resource. Please note, that you will have to copy the file to writeable directory after app installation. take a look at createeditablecopyofdatabaseifneeded.
  • open your database with sqlite3_open with respect to your new database location.
  • query the database as you like with sqlite3_prepare_v2

a simple query snippet looks like:

NSString *querystring;
// create your statement
querystring= [NSString stringWithFormat:@"SELECT text1, text2 FROM datatable;"]; 
const char *sql = [querystring UTF8String];
NSString *text1 = nil;
NSString *text2 = nil;
if (sqlite3_prepare_v2(db, sql, -1, &statement, NULL)!=SQLITE_OK){
 NSLog(@"sql problem occured with: %s", sql);
 NSLog(@"%s", sqlite3_errmsg(db));
}
else
{
 // you could handle multiple rows here
 while (sqlite3_step(statement) == SQLITE_ROW) { 
 text1 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
 text2 = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
 } // while 
}
sqlite3_finalize(statement);
// go on with putting data where you want
answered May 3, 2012 at 19:55
Sign up to request clarification or add additional context in comments.

1 Comment

You are the man! This is essentially exactly what I was looking for. The step by step instructions were excellent for a newbie. I really appreciate you taking the time out to whip up these instructions and explain how to go about this process. Very High Quality member. Thanks so much!
0

There is an example project for using SQLite here you can refer to: https://github.com/AaronBratcher/ABSQLite

It has classes for accessing SQLite in a more traditional database way. The sample project uses SQL to create the tables and shows how you can change the schema over different versions of your app.

answered Jul 11, 2013 at 14:55

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.