How to delete data from sqlite - iphone

I have created a table with three entries name,adress and phone no.
Now i want to delete data based on the name i type in the name text field. I successfully doing this but the problem is that if I type a name which is not in my database label still show 'contact deleted '... Here is my code
-(void)deleteContact{
const char *dbPath=[databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbPath, &contactDB)==SQLITE_OK)
{
NSString *querySQL=[NSString stringWithFormat:#"delete from
contacts where name=\"%#\"",name.text];
const char *query_stmt=[querySQL UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt,-1,&statement, NULL);
(sqlite3_step(statement)==SQLITE_OK);
status.text=#"Contact Deleted";
name.text=#"";
address.text=#"";
phone.text=#"";
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
[name resignFirstResponder];
}

Delete is OK even if there is nothing to delete.
If you need to know if there is anything to delete you should check this.
NSString *sql = [NSString stringWithFormat:#"select count (*) from contacts where name=\"%#\"", name.text];
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
if (res != SQLITE_OK) {
NSLog(#"sqlite3_prepare_v2() failed");
return;
}
int count = 0;
if (sqlite3_step(stmt) == SQLITE_ROW) {
count = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
if (count == 0) {
// nothing to delete, do whatever you like
} else {
// do real delete
// your code for that seems to be OK
}

delete from YourTable where name = "NameToDelete"
will delete all records with field name equal to NameToDelete from table YourTable.

Related

iOS sqlite will return NULL when using SELECT statement

I'm very confused why the SELECT statement doesn't work correctly. It doesn't give me any errors, just returns null. I know it is writing the string correctly and the right string is there, it's just not reading it correctly. Everything as far as I know is correct because I use the same SQLstmt "method" for many other methods/functions similar to this. This one just doesn't make sense on why it shouldn't work.
- (NSString *)returnNote {
selStmt=nil;
NSLog(#"Reading note");
NSString *SQLstmt = [NSString stringWithFormat:#"SELECT 'Notes' FROM '%#' WHERE Exercises = '%#';", currentRoutine, currentExercise];
// Build select statements
const char *sql = [SQLstmt UTF8String];
if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK) {
selStmt = nil;
}
// Building select statement failed
if (!selStmt) {
NSAssert1(0, #"Can't build SQL to read Exercises [%s]", sqlite3_errmsg(database));
}
NSString *note = [NSString stringWithFormat:#"%s", sqlite3_column_text(selStmt, 0)];
sqlite3_reset(selStmt); // reset (unbind) statement
return note;
}
You're not calling sqlite3_step. The statement is never executed.
NSString *querySQLS1 = [NSString stringWithFormat: #"SELECT Notes FROM \"%#\" where Exercises=\"%#\"", currentRoutine, currentExercise];
sqlite3_stmt *statements;
const char *query_stmts1 = [querySQLS1 UTF8String];
if(sqlite3_prepare_v2(UsersDB, query_stmts1, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(#"in prepare");
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(#"Query executed");
}
else {
NSLog(#"in else");
}
sqlite3_finalize(statement);
}

Getting error while executing SQLite command from application

I have this simple function in my application :
-(NSMutableArray *)SelectProductID:(NSMutableArray *)arr
{
NSLog(#"----------------");
sqlite3_stmt *statement;
NSMutableArray *arrPordID = [[NSMutableArray alloc]init];
#try
{
//Get productID
for(NSString *strSubProductID in arr)
{
NSString *s = [NSString stringWithFormat:#"SELECT ProductID FROM SubProducttable where SubProductID=%#",strSubProductID];
const char *sql = [s cStringUsingEncoding:NSASCIIStringEncoding];
if (sqlite3_prepare_v2(database, [s cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW){
char *dbString;
dbString = (char *)sqlite3_column_text(statement, 0);
NSString *pID = (dbString) ? [NSString stringWithUTF8String:dbString] : #"";
[arrPordID addObject:pID];
}
}
}
}
#catch (NSException *exception) {
#throw exception;
}
#finally {
sqlite3_finalize(statement);
}
return arrPordID;
}
I am encountering a strange problem here. When application reaches while (sqlite3_step(statement) == SQLITE_ROW){, loop is never entered. I don't know why. I executed the same query in SQLite manager (when application is not running). And I get result as a single one. The result I get is 2. But here I am getting nothing.
And yes, I always close the database in SQLite manager whenever I run my application. I have also cleaned the application, restarted XCode, and removed the application from simulator. But no success.
Also I saw a strange thing during debugging. While debugging, sqlite3_stmt *statement is always skipped. Is this the reason I am not getting any result?
Have you tried subproductId in single quotes?
NSString *s = [NSString stringWithFormat:#"SELECT ProductID FROM SubProducttable where SubProductID='%#'",strSubProductID];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlQuery = #"SELECT ProductID FROM SubProducttable where SubProductID=%#",strSubProductID;
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, statement, -1, &sqlQuery, NULL) == SQLITE_OK) {
while(sqlite3_step(sqlQuery ) == SQLITE_ROW) {
// Read the data and add to your array object
}
}
// Release the compiled statement from memory
sqlite3_finalize(statement);
}
sqlite3_close(database)

iphone: delete row from sqlite database

I have tried many ways to achieve this biut unable to delete row from sqlite, please help me to correct following piece of code
dbPath = [self applicationDocumentsDirectory];
NSLog(#"%#", dbPath);
dbPath=[dbPath stringByAppendingPathComponent:#"database"];
dbPath=[dbPath stringByAppendingPathComponent:#"OFFENDERSDB.sqlite"];
NSLog(#"database path --> %#", dbPath);
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatement = ("DELETE * from OFFENDERS_LIST where ID=%d ",2);
sqlite3_stmt *compiledStatement;
if (sqlite3_prepare(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(#"offender deleted");
}
sqlite3_finalize(compiledStatement);
}
Thanx in advance
have you tried using:
sqlite3_exec(database, sqlStatement...
with your DELETE statement?
ALSO.... it's DELETE FROM.... not DELETE * FROM....
Snagged the following code from another place out on the internet for a more complete example...
sqlite3 *db;
int rc;
rc = sqlite3_open( "C:\\MyDatabase", &db );
if ( rc )
{
sqlite3_close(db);
}
else //Database connection opened successfuly
{
char *zErrMsg = 0;
rc = sqlite3_exec( db, "DELETE FROM yourTable", NULL, NULL, &zErrMsg );
if( rc != SQLITE_OK )
{
sqlite3_free( zErrMsg );
}
sqlite3_close(db);
}

SQLite3 COUNT incorrect

Say i've got a table called Incidents, and a table called Devices.
Each device has a CI-code.
I want to create a tableView, with in the sections the names of the devices (CI), and each row to represent an incident.
To get the correct number of rows under each section, I use the following code:
NSMutableArray *lijstDevices = [[NSMutableArray alloc] init];
const char *dbpath = [appDelegate.databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:#"SELECT DISTINCT CI FROM Devices"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
// NSLog(#"SQLite ROW for filling in cell");
NSString *addDev = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
[lijstDevices addObject:addDev];
}
sqlite3_finalize(statement);
}
NSString *ciNaam = [lijstDevices objectAtIndex:section];
NSString *querySQL2 = [NSString stringWithFormat:#"SELECT COUNT(ID) FROM Incidents WHERE CI='%#'", ciNaam];
const char *query_stmt2 = [querySQL2 UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt2, -1, &statement, NULL) == SQLITE_OK)
{
//NSLog(#"SQLite ok in rowforindexpath");
if(sqlite3_step(statement) == SQLITE_ROW) {
// NSLog(#"SQLite ROW for filling in cell");
teller = sqlite3_column_int(statement, 0);
}
else
{
NSLog(#"Not.. good...");
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
Here comes the problem: The sections are filled correctly. Some sections need 1 row, other sections need 2 rows. Whenever I run the program, each section which needs 1 row ends up with 0 rows, while each section with 2 rows needed end up with only 1 row.
Besides that, I had an error before which didn't clean the tables. Therefore, if I ran it twice, the database got doubled up on rows. In that case, each one which needed 1, has 2 rows. Each one which needed 2, had 4 rows. This is obviously not the case anymore but it might help understanding what's wrong.
Anyone having any idea???
u see this example and check it
-(void)ChekInDataBase{
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStatement = [NSString stringWithFormat:#"SELECT count(id) as countRow FROM password"];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding], -1, &compiledStatement, NULL) == SQLITE_OK)
{
NSLog(#"%#",sqlStatement);
if (sqlite3_step(compiledStatement) == SQLITE_ROW)
{
countRow = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0 )]intValue] ;
}
}
sqlite3_finalize(compiledStatement);
if (countRow > 0)
{
[yesBtnPressed setImage:[UIImage imageNamed:#"yes_tab_over.png"]forState:UIControlStateNormal];
[noBtnPressed setImage:[UIImage imageNamed:#"no_tab.png"]forState:UIControlStateNormal];
[noBtnPressed setUserInteractionEnabled:TRUE];
[yesBtnPressed setUserInteractionEnabled:FALSE];
}
else
{
[yesBtnPressed setImage:[UIImage imageNamed:#"yes_tab.png"]forState:UIControlStateNormal];
[noBtnPressed setImage:[UIImage imageNamed:#"no_tab_over.png"]forState:UIControlStateNormal];
[noBtnPressed setUserInteractionEnabled:FALSE];
[yesBtnPressed setUserInteractionEnabled:TRUE];
}
sqlite3_close(database);
}
}
There was no error in this line. Apparently, the CI-code didn't get added in the table correctly. Sometimes it shows the CI-code correct (the ones I can see in the list), but the others just have (null) or 1 as code.

Problem with SQL statement

I have the following code:
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID %#", mapID;
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the Route Name array
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
// Read the data from the result row
if((char*)sqlite3_column_text(compiledStatement, 0) != NULL)
{
NSString *xCoordinate = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 0)];
NSString *yCoordinate = [NSString stringWithUTF8String:(char*)sqlite3_column_text(compiledStatement, 1)];
NSLog(#"xCoordinate: %#", xCoordinate);
NSLog(#"yCoordinate: %#", yCoordinate);
CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate};
MapPin *pin = [[MapPin alloc]initWithCoordinates:coordinate
placeName:#"Keenan Stadium"
description:#"Tar Heel Football"];
[self.mapView addAnnotation:pin];
[pin release];
}
}
}
else
{
NSLog(#"Error: failed to select details from database with message '%s'.", sqlite3_errmsg(database));
}
I have a few questions:
in my SQL statement how do I include the variable mapID as part of the SQL statement
the line
CLLocationCoordinate2D coordinate = {xCoordinate, yCoordinate};
gives me the following warning "Incompatible types in initialization"
Thanks
You should use a parameterized statement and bind values, like this:
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID = ?";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
sqlite3_bind_text( compiledStatement, 1, [mapID UTF8String], -1, SQLITE_TRANSIENT );
(Note that when binding parameters, you start at 1, but when reading columns from result rows, you start at 0...). I'm assuming your mapID is an NSString since you tried to stick it in your query using %#. If it's something else, you'll have to use a different bind function and obviously skip the UTF8String.
To include MapID,
Change this:
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y from ZWAYPOINT where ZMAP_ID %#", mapID;
To this:
const char *sqlStatement = "select ZWAYPOINT_X, ZWAYPOINT_Y, MapID from ZWAYPOINT where ZMAP_ID %#", mapID;
You get a warning creating that struct since the values are not NSString * but double.
http://developer.apple.com/iphone/library/documentation/CoreLocation/Reference/CLLocation_Class/CLLocation/CLLocation.html#jumpTo_17