Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iPad Application Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning iPad Application Development
This is the forum to discuss the Wrox book Beginning iPad Application Development by Wei-Meng Lee; ISBN: 9780470641651
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iPad Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 22nd, 2010, 03:38 PM
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 10: Deleting rows from a table

Hi all,
I was successfully able to implement inserting rows and selecting rows. Update query worked well when wrote instead of select query.
BUt now having problems with DELETE query. I implemented in the same format for retrieving records, but with the delete query. But not working.
The code I wrote is the same as retrieving the records, but with the Delete query

NSString *qsql = @"Delete From Tablename Where S = 0";

Also even I tried NSString *qsql = @"Delete From Tablename";

But nothing worked . Can any one please tell me what will be the problem?

Thanks in advance!!!!
sdm
Reply With Quote
  #2 (permalink)  
Old September 23rd, 2010, 02:25 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The examples in the book are my first experience with sqlite3. This is my working implementation of the delete command
Code:
-(void)deleteRecordFromTableNamed:(NSString *)tableName
					   withField1:(NSString *)field1
					  field1Value:(NSString *)field1Value
						andField2:(NSString *)field2
					  field2Value:(NSString *)field2Value
					 
{
	NSString *sql = [NSString stringWithFormat:@"DELETE FROM '%@' WHERE %@ = '%@' AND %@ = '%@'",tableName,field1,field1Value,field2,field2Value];
	
	NSLog(@"The sql string is %@",sql);
	
	char *err;
	
	if (sqlite3_exec(db, [sql UTF8String],NULL, NULL, &err) != SQLITE_OK) {
		sqlite3_close(db);
		NSAssert(0, @"Error updating table");
	}
}
To test I simply added a delete call in -(void)viewDidLoad after the -(void)getAllRowsFromTable, and then re-listed the rows.

Note the absence of the single quotes around the tokens for the column names. It does not work if there are single quotes. When comparing the book examples to the tutorial webpage the author referred to, it seems that the book goes single quote crazy. Every token is written as '%@'. The preceding works with the book code. I also redid the example removing all single quotes from all tokens in every method except those representing values. This syntax was consistent with the reference page, and the program ran correctly. I don't know why the insert and replace works with both the single quotes present and absent, but the delete only worked when the single quotes were not present.

Last edited by thepianoguy; September 23rd, 2010 at 03:21 PM.
Reply With Quote
  #3 (permalink)  
Old September 23rd, 2010, 06:40 PM
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default One more doubt regarding deleting a row

Thanks thepianoguy..
I changed my code to the one you gave me and its works fine, but doesnt deletes the row in the sqlite3 database physically. After executing the delete query, if I try to access the deleted row from the code, it shows that the data is not found, which is good. But when I open the db, the record is still there. Why is it like that?
stevedm.
Reply With Quote
  #4 (permalink)  
Old September 24th, 2010, 12:14 AM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Think of the text file in the same manner as your hard drive. Data deleted from the hard drive is not "gone" unless deleted securely, it still exists but the path to locate it is gone. Over time other data will be written over it and it will be unrecoverable. Data deleted in the .sql file is marked as deleted, so the space can be reused, and in time will be overwritten, but it still exists in the file. The fact that it is still present, though over time in a more and more fragmented fashion is what allows recovery tools to work. To eliminate the records entirely you could rebuild the directory using the following in the
-(void)viewDidLoad method after
[self getAllRowsFromTableNamed:…];
Code:
char *err;

	if (sqlite3_exec(db, "VACUUM;", NULL, NULL, &err) != SQLITE_OK) {
		sqlite3_close(db);
		NSAssert(0, @"Error updating table");
	}
	else
	sqlite3_close(db);
	    [super viewDidLoad];
The file will have all deleted records wiped clean.

Last edited by thepianoguy; September 24th, 2010 at 08:01 PM. Reason: corrected 'all records' to all 'deleted records'
Reply With Quote
  #5 (permalink)  
Old September 24th, 2010, 04:00 AM
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Sorry, my doubts are growing and growing

Thanks for the reply. As the code you wrote will have all records wiped clean, If your could please tell me how to just delete/wipe few records from the database(I mean wipe the datas which satisfies the condition)
Example : Delete from tablename Where a = 1;
Here Not only I need to delete the rows with a = 1, but also want to wipe those rows with a = 1 from table. how can I do that?
Thanks a lot for helping me. Really appriciated!!!
stevem
Reply With Quote
  #6 (permalink)  
Old September 24th, 2010, 02:37 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

Not sure if I follow the question. You want some deleted data to be wiped, and some deleted data to not be wiped?
Reply With Quote
  #7 (permalink)  
Old September 24th, 2010, 02:52 PM
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply.
I just want all deleted data to be wiped. Suppose I have 10 records and I deleted 5 records. I want to wipe just those 5 records deleted.
Thanks and really appriciated.
stevedm.
Reply With Quote
  #8 (permalink)  
Old September 24th, 2010, 02:56 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

The code that I provided removes the deleted records. The other records are intact.
Reply With Quote
  #9 (permalink)  
Old September 27th, 2010, 03:21 PM
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks you very much thepianoguy. The code works. Thanks for helping me, really really appreciated!!!!!!.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 12 - Displaying 10 rows at a time bladejay BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 August 5th, 2010 06:06 PM
Chapter 10, listing 10-10-app kiwibrit BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 2 August 18th, 2009 05:21 AM
Deleting rows from a data table in datagrid ann03 ASP.NET 1.x and 2.0 Application Design 1 October 23rd, 2007 07:23 AM
Deleting rows in Winforms Datagrid livehed General .NET 0 May 4th, 2006 02:23 AM



All times are GMT -4. The time now is 06:54 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.