Not saving data in SQLite - iphone

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,...)

Related

How to write the text data in to the sqlite3 database

I have create one sqlite3 database and in that database i am writing name text but when i write in to database it doesn't write only blank row create i am doing like this
if(sqlite3_step(statement)==SQLITE_DONE)
{
NSLog(#"ALERT>TEXT===%#",alert.alertText);
NSLog(#"....");
sqlite3_bind_text(statement, 1, [alert.alertText UTF8String], -1, NULL);
sqlite3_step(statement);
NSLog(#"insertion is done");
}
if(sqlite3_open(dbPath, &db)==SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO YourTable ('symbol') VALUES",%#];
const char *insertStmt=[insertSQL UTF8String];
char *errmsg=nil;
if(sqlite3_exec(db, insertStmt, NULL, NULL, &errmsg)==SQLITE_OK)
{
NSLog(#"Row ADDED!");
}
sqlite3_close(db);
}
Please vote and marked it as answered if it fulfills your requirement.
Thanks,
I am using this code to insert data in to my database.
NSString *sqlStatement = [NSString stringWithFormat:#"insert into TableName alertName values '%#'",[name stringWithPercentEscape]];
NSLog(#"%#",sqlStatement);
if (sqlite3_exec(database, [sqlStatement cStringUsingEncoding:NSUTF8StringEncoding], NULL, NULL, &error) == SQLITE_OK)
{
NSLog(#"record inserted successfully!!");
NSLog(#"%#",databasePath);
}
I recommend you to use the FMDB framework.It is a very light package.
With it you can manage your sqlite database just by SQL.
This is the link

How to delete "/private/var/mobile/Library/SMS/sms.db" in Data

I break the phone you want to delete /private/var/mobile/Library/SMS/sms.db" record,
Here is my source:
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: #"delete from message where rowid=4"];//SELECT address,text FROM message where ROWID=4
const char *query_stmt = [querySQL UTF8String];
const char *error;
int err = sqlite3_prepare_v2 (contactDB, query_stmt, -1, &statement, &error);
if (err != SQLITE_OK)
{
//NSAssert1(0, #"Error updating tables: %s", erroMsg);
}
}
and return "err=1" ,rowid=4 is exist record,"SELECT address,text FROM message where ROWID=4" is right.
please ,help me ,thx
delete a trigger in database.
delete the message
create the delete

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];
}

table value is not updated using update statement

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.. :)

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