Guest User

Untitled

a guest
Aug 16th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. how to updtae Sqlite last insert row id in iphone?
  2. -(void)submit
  3.  
  4. {
  5.  
  6. if( ([UserName.text isEqualToString:@""]) || ([Password.text isEqualToString:@""]) ||
  7.  
  8. ([ConfirmPassword.text isEqualToString:@""]) || ([Name.text isEqualToString:@""]) ||
  9.  
  10. ([Email.text isEqualToString:@""]) || ([ContactNO.text isEqualToString:@""]) ||
  11.  
  12. ([MobileNo.text isEqualToString:@""]) || ([Address.text isEqualToString:@""]) )
  13.  
  14. {
  15.  
  16. UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Error!!"
  17. message:@"Please fill in the details." delegate:nil
  18. cancelButtonTitle:@"OK"
  19. otherButtonTitles:nil, nil];
  20. [ErrorAlert show];
  21. [ErrorAlert release];
  22. }
  23. else
  24. {
  25. Confirmation_form *conForm = [[Confirmation_form alloc] initWithNibName:@"Confirmation_form" bundle:nil];
  26. conForm.data = UserName.text;
  27. conForm.data1 = Password.text;
  28. conForm.data2 = ConfirmPassword.text;
  29. conForm.data3 = Name.text;
  30. conForm.data4 = Email.text;
  31. conForm.data5 = ContactNO.text;
  32. conForm.data6 = MobileNo.text;
  33. conForm.data7 = Address.text;
  34.  
  35.  
  36. sqlite3_stmt *statement;
  37. const char *dbpath = [databasePath UTF8String];
  38. if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
  39. {
  40.  
  41.  
  42.  
  43. NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO test(UserName, Password, ConfirmPassword, Name, Email, ContactNO, MobileNo, Address) VALUES ("%@", "%@", "%@", "%@", "%@", "%@", "%@", "%@")",UserName.text, Password.text, ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text];
  44. const char *insert_stmt = [insertSQL UTF8String];
  45. sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
  46. if(sqlite3_step(statement) == SQLITE_DONE)
  47. {
  48. //status.text = @"Contact added";
  49. UserName.text = @"";
  50. Password.text = @"";
  51. ConfirmPassword.text = @"";
  52. Name.text = @"";
  53. Email.text = @"";
  54. ContactNO.text = @"";
  55. MobileNo.text = @"";
  56. Address.text = @"";
  57.  
  58.  
  59. sqlite3_last_insert_rowid;
  60. rowID = sqlite3_last_insert_rowid(test1DB);
  61. NSLog(@"last inserted rowId = %d",rowID);
  62.  
  63. sqlite3_reset(statement);
  64. sqlite3_finalize(statement);
  65. sqlite3_close(test1DB);
  66.  
  67.  
  68. [self.navigationController pushViewController:conForm animated:YES];
  69.  
  70. }
  71. }
  72.  
  73.  
  74. }
  75. }
  76.  
  77. -(IBAction)Update;
  78.  
  79. {
  80.  
  81. sqlite3_stmt *statement;
  82.  
  83. const char *dbpath = [databasePath UTF8String];
  84.  
  85. if (sqlite3_open(dbpath, &test1DB) == SQLITE_OK)
  86.  
  87. {
  88.  
  89.  
  90. NSString *insertSQL = [NSString stringWithFormat:@"UPDATE test SET UserName='%@',Password='%@',ConfirmPassword='%@',Name='%@',Email='%@',ContactNO='%@',MobileNO='%@',Address='%@'WHERE ID='%@'",UserName.text,Password.text,ConfirmPassword.text, Name.text, Email.text, ContactNO.text, MobileNo.text, Address.text, sqlite3_last_insert_rowid(test1DB)];
  91.  
  92. const char *insert_stmt = [insertSQL UTF8String];
  93. sqlite3_prepare_v2(test1DB, insert_stmt, -1, &statement, NULL);
  94. if(sqlite3_step(statement) == SQLITE_DONE)
  95. {
  96. sqlite3_step(statement);
  97. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  98. [alert show];
  99. [alert release];
  100. alert = nil;
  101. sqlite3_finalize(statement);
  102. sqlite3_close(test1DB);
  103. }
  104.  
  105. else {
  106. sqlite3_step(statement);
  107. UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record notadded" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
  108. [alert show];
  109. [alert release];
  110. alert = nil;
  111. }
  112. }
Add Comment
Please, Sign In to add comment