how to check in database for new row insert or not in iphone and simulator
hi friend please help some one i want to see in my data base in table were my new row was add or not how can i see that
please some one give me demo code or chek my code in proper what my mistak
here my appdeleget class method
-(void)checkAndCreateDB {
NSString* databaseName = @"demo.sqlite";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"demo.sqlite"];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success) return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void)insertData{
sqlite3 *database;
sqlite3_stmt *statement=nil;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"demo.sqlite"];
if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
{
sqlite3_close(database); NSAssert(0, @"Failed to open database");
}
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (uname,id,password,message) VALUES (\"%@\",\"%d\", \"%@\", \"%@\")",Gmessage,1 ,Glocation,Gjourney];
if(sqlite3_prepare_v2(database, [insertSQL UTF8String], -1, &statement, NULL)!= SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [Gjourney UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [Glocation UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [Gmessage UTF8String], -1, SQLITE_TRANSIENT);
if (SQLITE_DONE !=sqlite3_step(statement))
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Reco开发者_Python百科rd" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert=nil;
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
alert=nil;
}
sqlite3_reset(statement);
}
sqlite3_finalize(statement);
}
Here is my controller class button code
-(IBAction)Start:(id)sender{
Gjourney=mTextJourney.text;
Glocation=mTextLocation.text;
Gmessage=mDescription.text;
AddListAppDelegate *appDelegate =(AddListAppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate insertData];
}
You can use the sqlite3_last_insert_rowid function to check if the row was added or not, as long as you're not accessing the database from multiple threads, etc.
精彩评论