table value is not updated using update statement - iphone

i need to update a column value into table.
for that my code is,
NSString *sql_str = [NSString stringWithFormat:#"update %# Set Quantiry = %# Where ItemName = %#", tableName,quantity,itemname];
const char *sqlStatement = (char *)[sql_str UTF8String];
NSLog(#"query %s",sqlStatement);
sqlite3_stmt *compiledStatement;
sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
sqlite3_close(database);
displayed query in console is update allcategories Set Quantiry = 11 Where ItemName = Bananas
but the value in table is not updated.
what the wrong,can any one please help me.
Thank u in advance.

you are missing one line after sqlite3_prepare_v2. you have to use sqlite3_step() after this. use following:
NSString *sql_str = [NSString stringWithFormat:#"update %# Set Quantiry = %# Where ItemName = %#", tableName,quantity,itemname];
const char *sqlStatement = (char *)[sql_str UTF8String];
NSLog(#"query %s",sqlStatement);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
sqlite3_step(compiledStatement);
}
sqlite3_close(database);
hope helps.. :)

Related

Iinsert/Replace data in SQLite

Why can't I update data to SQLite eventhought it print "yes yes", please help, here my code, it called from viewdidload:
-(void)updateData{
CryptTestAppDelegate *delegate=(CryptTestAppDelegate *)[[UIApplication sharedApplication] delegate];
sqlite3_stmt *stmt=nil;
NSString *dd = #"testing";
const char *sql = "Update ProvPuzz set desc2=?";
sqlite3_prepare_v2(delegate.db, sql, 1, &stmt, NULL);
sqlite3_bind_text(stmt, 1, [dd UTF8String], -1, SQLITE_TRANSIENT);
if(sqlite3_step(stmt)){
NSLog(#"yes yes");
}else{
NSLog(#"no no");
}
sqlite3_finalize(stmt);
sqlite3_close(delegate.db);
}
It seems to pass every step, but my data not change at all. Any help please...
How should I call/code to be able to insert/replace these record in SQLite?
NSString *UpdateSql = [NSString stringWithFormat:#"Update ProvPuzz set desc2 = something"];
const char *sqlStatement = [UpdateSql UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(filesDB, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
if(sqlite3_step(compiledStatement) == SQLITE_DONE)
{
sqlite3_reset(compiledStatement);
}
}
// Release the compiled statement from memory
sqlite3_reset(compiledStatement);
sqlite3_finalize(compiledStatement);
sqlite3_close(filesDB);
and also check you db connection opened properly or not

Not saving data in SQLite

Every thing goes in code, but not saving data in SQLite db. Can any one help on this?
Find my code below:
-(IBAction)signin:(id)sender{
NSLog(#"%#",perDas.string1);
nameString=[[NSString alloc]initWithString:perDas.string1];
statusString=[[NSString alloc]initWithFormat:#"IN"];
{
NSLog(#"passed");
sqlite3_stmt *statement;
const char *dbpath = [perDas.databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSLog(#"pass");
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO status VALUES (\"%#\", \"%#\", datetime(), \"%#\")", nameString, statusString,empString];
NSLog(#"%#",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL);
sqlite3_finalize(statement);
sqlite3_close(database);
}
}}
Thanks in advance
-(IBAction)signin:(id)sender{
NSArray *dirPath;
NSString *docDir;
NSString *databasePath;
dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docDir=[dirPath objectAtIndex:0];
databasePath=[docDir stringByAppendingPathComponent:#"YourDB.sql"];
const char *dbPath=[databasePath UTF8String];
if(sqlite3_open(dbPath, &db)==SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO status VALUES (\"%#\", \"%#\", datetime(), \"%#\")", nameString, statusString,empString];
const char *insertStmt=[insertSQL UTF8String];
char *errmsg=nil;
if(sqlite3_exec(db, insertStmt, NULL, NULL, &errmsg)==SQLITE_OK)
{
NSLog(#"ADDED!");
}
sqlite3_close(db);
}
}
you should check if your sql statement is correct:
if (sqlite3_prepare_v2(database, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {
...
}
and if you only INSERT a subset of your row set, use this INSERT Statement form:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

Problems storing data in iPhone SQLite help!

I just started looking into SQLite for the iPhone SDK. I have been looking at this tutorial and I am wondering how can I find an ID and store the information in that field of the ID? (the tutorial increments an ID every time I press save).
Also, how can I loop though the whole database e.g., add all the id numbers up? Is there a command?
Not much changes in the database access in the methods. Only the SQLite query changes. It should be something on these lines.
- (NSInteger) sumOfIds
{
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
NSString *result;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = #"SELECT sum(id) FROM contacts";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *result = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
}
sqlite3_finalize(statement);
}
sqlite3_close(contactDB);
}
return [result floatValue];
}

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