objective c - How to insert data into a SQLite database in iPhone -
I'm new to iPhone development. I want to include some data in my database and want to retrieve it and I want to display it in a table. I've created database data. Sqlite
with table 'user' in the table there are two values, id (varchar) and name (varchar). I have inserted 3 rows of data in the table "Enter user value ('1', 'xxxx');" In the terminal window I "testDb" made a navigation-based application, I have two ways
- (void) Make editableCopyOfDatabaseIfNeeded {NSLog (@ "make editable copy of the database"); // First, test of survival success success; NSFileManager * fileManager = [NSFileManager defaultManager]; NSError * error; NSArray * path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, yes); NSString * document directory = [path item overindexx: 0]; NSString * writableDBPath = [Document Directory stringBapensitting path comparison: @ "Dataciclit"]; Success = [file manager file extensivity: writeable db path]; If (success) return; // writeable database is not present, so copy the default to the appropriate location. NSString * defaultDBPath = [[[NSBundle main bundle] resourcepath] stringbappingpingscompany: @ "datacyclite"]; Success = [file manager copy pastepath: default to dbipith path: writeable dbpext error: & amp; Error]; If (! Success) {NSAssert1 (0, @ "Failed to create writable database file with message '% @'", [error localization]]; }}
and
+ (sqlite3 *) getNewDBConnection {sqlite3 * newDBconnection; NSArray * path = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, yes); NSString * document directory = [path item overindexx: 0]; NSString * path = [document directory stringbappingpath company: @ "datacetylite"]; // Database Open was prepared outside the database application. If (sqlite3_open ([path UTF8String], & amp; newDBconnection) == SQLITE_OK) {NSLog (@ "Database successfully opened :)"); } Else {NSLog (@ "database :( Error opening");} newDBconnection return;}
In rootviewcontroller
- (minus) initializeTableData { sqlite3 * db = [DatabaseTestAppDelegate getNewDBConnection]; sqlite3_stmt * statement = nil; const char * sql = "select user *", if (sqlite3_prepare_v2 (db, SQL, -1, & amp ;! statement, zero) = SQLITE_OK) NSAssert1 (0, @ "" to prepare error Staement, Sqlite3_errmsg (database)); else {while (Sqlite3_step (statement) == SQLITE_ROW) [tableData addObject: [NSString stringWithFormat: @ "% s", (char *) sqlite3_column_text ( statements, 1)]];} sqlite3_finalize (statement);} - (void) viewDidLoad {[super viewDidLoad]; tableData = [[NSMutableArray all oc] init]; [self addMyObjectIntoDatabase]; [self initializeTableData]; self.title = @ "database test";}
< code> - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtInd Akspeth: (Ansaianaksapth *) Indekspeth {static Ansstiing * Selaidentifayr = "cell"; UITableViewCell * cell = [tableview] dequeueReusableCellWithIdentifier: CellIdentifier]; If (cell == blue) {cell = [[[UITWebWeblog ALLOC] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cell identifier] autorelease]; } NSLog (@ "Before displaying data"); Cell.textLabel.text = [tableData objectAtIndex: indexPath.row]; NSLog (@ "After displaying data"); // Configure cell return; }
This is working fine to display the contents of the table. But now I want to insert one more line in my table and I want to display the contents included with the previous material. Since I'm new to the iPhone so I learned to read the database of tutorials and display its contents. How do I proceed to do my work in how to insert and display again?
How you're really interested in learning to use to manage data in an iPhone app SQLite I have to meet the following tutorial, as it is a very good introduction would recommend you:
handles choose
tutorial, has been updated to include And on removing data in an iPhone app, using SQLite.
Comments
Post a Comment