It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Hello Friends,
I have an application that will do the synchronization with the database to web-service. I have one table list in that I want to update, delete and insert the list. When the user click on the synchronization button I want to send the data which will be updated or changed in the database. The web-service are different for add, update or delete I want send only updated records.
For that what can I do for that any suggestion.
Thanks in advance,
Hardik Patel
You can maintain an extra column for timestamp.
So whenever a record is changed you should update the timestamp. (record_timestamp)
You have to maintain the time at which you have sent the data successfully to your server. (last_updated_timestamp)
So at the time of sync, you will send only those records to server whose
record_timestamp > last_updated_timestamp
The last_updated_timestamp will be updated every time successful sync is performed.
I hope this helps.
This link will help you compare time stamps in sqlite Timestamp comparison
You use sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
- (NSNumber *)executeSQL:(NSString *)sql withCallback:(void *)callbackFunction context:(id)contextObject {
NSString *path = [self pathToDB];
sqlite3 *db = NULL;
int rc = SQLITE_OK;
NSInteger lastRowId = 0;
rc = sqlite3_open([path UTF8String], &db);
if(SQLITE_OK != rc) {
NSLog(#"Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return nil;
}else {
char *zErrMsg = NULL;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
rc = sqlite3_exec(db, [sql UTF8String], callbackFunction, (void*)contextObject, &zErrMsg);
if(SQLITE_OK != rc) {
NSLog(#"Can't run query '%#' error message: %s\n", sql, sqlite3_errmsg(db));
sqlite3_free(zErrMsg);
}
lastRowId = sqlite3_last_insert_rowid(db);
sqlite3_close(db);
[pool release];
}
NSNumber *lastInsertRowId = nil;
if(0 != lastRowId) {
lastInsertRowId = [NSNumber numberWithInteger:lastRowId];
}
return lastInsertRowId;
}
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have been getting constant memory leak at this line
> if (sqlite3_open([databasePath UTF8String], &databaseHandle) != SQLITE_OK)
. What could be the reason ?? I have even closed the database after opening it . But its of no avail.
My whole method is
-(void)copyCustomDatabase{
/** done - #todo Copy db file from app resources */
#try {
NGMobileCaptureSingleton * singleton = [NGMobileCaptureSingleton getSharedInstance];
NSString *documentsDirectory = [singleton getAppDocumentDirectory];
NSString *apkIdStr = [NSString stringWithFormat:#"%d", [NGMobileCaptureSingleton getSharedInstance].apkId];
NSString *databaseName = [[[[#"ngcapcust_" stringByAppendingString:apkIdStr] stringByAppendingString:#"_"] stringByAppendingString:[NSString stringWithFormat:#"%d", formId]] stringByAppendingString:#".db"];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:databaseName];
// NSLog(#"copyCustomDatabase Custom Database Path %#", databasePath);
bool databaseAlreadyExists = [[NSFileManager defaultManager] fileExistsAtPath:databasePath];
if (!databaseAlreadyExists)
{
NSError *error;
NSFileManager *fileManager = [[NSFileManager defaultManager] init];
NSString *srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:srcPath toPath:databasePath error:&error];
}
if (sqlite3_open([databasePath UTF8String], &databaseHandle) != SQLITE_OK)
{
[self closeDatabase];
NSLog(#"NGDefaultCustomHelper copyCustomDatabase Error in creating database handle");
// } else {
// NSLog(#"NGDefaultCustomHelper copyCustomDatabase Database handle created successfully");
}
} #catch (NSException *exception) {
NSLog(#"NGDefaultCustomHelper copyCustomDatabase exception : %#", exception);
}
}
- (void)closeDatabase
{
sqlite3_close(databaseHandle);
databaseHandle = NULL;
tableMap = NULL;
tableIdMap = NULL;
}
sqlite3_open([databasePath UTF8String], &databaseHandle) returns databaseHandle, which (according to the docs) "Whether or not an error occurs when it is opened, resources associated with the database connection handle should be released by passing it to sqlite3_close() when it is no longer required".
The reason is you don't close the database with sqlite_close().
U forgot to add sqlite_close()
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Here is my code:
It works for other methods but creating an issue here only.
Here I am unable to insert data...
What is the problem here?Code was working earlier perfectly.
-(void) insertleadership {
[self trunleadership];
// Setup the database object
sqlite3 *database;
NSLog(#"Inside leadership");
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
// NSString *query=[stringWithFormat: #"select name,password from customer where name=%# and password=%#",*login_name,*login_pass];
sqlite3_stmt *compiledStatement;
for(int i=0;i<ltitle.count;i++)
{
// NSString *insertSQL = [NSString stringWithFormat:#"insert into category values(\'aa\',\'bb\',\'cc\',\'cc\',\'cc\')"];
// NSLog(#"Inside leadership");
NSString *insertSQL = [NSString stringWithFormat:#"insert into leadership values(\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\',\'%#\')",[ltitle objectAtIndex:i],[ldate objectAtIndex:i],[lfunction objectAtIndex:i],[lservice objectAtIndex:i],[lsubservice objectAtIndex:i],[labstract objectAtIndex:i],[ldoctype objectAtIndex:i],[lcreated objectAtIndex:i],[lcorridor objectAtIndex:i],[lfeature objectAtIndex:i],[lindiastory objectAtIndex:i],[lpublish objectAtIndex:i],[ltopic objectAtIndex:i]];
// NSLog(#"SQL Query for leader:%#",insertSQL);
const char *insert_stmt = [insertSQL UTF8String];
// sqlite3_prepare_v2(databaseName, insert_stmt, -1, &statement, NULL);
sqlite3_prepare_v2(database, insert_stmt, -1, &compiledStatement, NULL);
if(sqlite3_step(compiledStatement) == SQLITE_DONE){
NSLog(#"Insert into leadership successful");
}
else {
NSLog(#"Unable to insert in leadership");
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
}
If you add some reporting to your code, sqlite itself will tell you why it's not opening:
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Do something
splite3_close(database);
}
else
{
NSLog(#"Failed to open database '%#': %s", databasePath, sqlite3_errmsg(database));
}
Also #HotLicks is correct in that you should be copying the database out of the app bundle before you access it, unless you open it readonly (by using sqlite3_open_v2() and passing in SQLITE_OPEN_READONLY as one of the flags).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i want to check if the old array has object or not if the old array has the object it should show me the button if the oldArray has zero object the button should be hidden the code is given below thanks...
-(void)viewWillAppear:(BOOL)animated
{
GET_DEFAULTS
NSMutableArray *array = [defaults objectForKey:kShouldResume];
NSData *dataRepresentingSavedArray = [defaults objectForKey:kShouldResume];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
{
array = [[NSMutableArray alloc] initWithArray:oldSavedArray];
if ([oldSavedArray containsObject])
{
btnResumeGame.hidden=NO;
}
else
{
btnResumeGame.hidden=YES;
}
}
else
{
array = [[NSMutableArray alloc] init];
}
}
}
Array has property count.
You can check weather count is zero or more than that as you require..
like
oldSavedArray.count
use this code:
if ( [oldSavedArray count]>0 ){
btnResumeGame.hidden=NO;
}
else{
btnResumeGame.hidden=YES;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am using QR code and after scanning getting username, eventtitle and ticketcode. I need to get these values separately in three different strings, so that i can save them in sqlite. I want to know only how to do this separation, saving is i can do.
Thanks in advance.
- (void)saveData {
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: #"INSERT INTO CONTACTS (name, address, phone) VALUES (\"%#\", \"%#\", \"%#\")", #"NameString", #"Address", #"Phone"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"Success");
} else {
NSLog(#"Failed");
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
}
Note
If you post what type of string you are getting, i will add how exactly you have to split it.
Below is example how you can split
NSArray arrResult = [str componentsSeparatedByString:#"-"];
Or By Character set
NSString* str = #"A string with newlines in it";
NSArray *arrTemp = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
you can use componentsSeparatedByString
NSString *str = #"userName:Password:etc";
NSArray* parts1 = [str componentsSeparatedByString:#":"];
NSLog(#" [[parts1 objectAtIndex:0] integerValue]=%d", [[parts1 objectAtIndex:0] integerValue]);
//the output will be: username for index 0, and so on
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I've been looking for this, but can't find the answer.
I have an NSMutableArray with values who_1, what_2, where_3 etc.
I want to transform this into who, what, where etc.
I already have the value of the integer as a variable, and _ is just a string.
What steps should I take to have all these arrayvalues transformed?
NSArray * arrB = [[NSArray alloc]initWithObjects:#"apple_a",#"ball_b",#"cat_c",#"doll_d",nil];
NSMutableArray * arrA = [[NSMutableArray alloc]init];
for(NSString *strData in arrB)
{
NSArray *arr = [strData componentsSeparatedByString:#"_"];
[arrA addObject:[arr objectAtIndex:0]];
}
and this would be your output
arrA:(
apple,
ball,
cat,
doll
)
You need to apply logic for that, You cant find answers to tricky Questions :)
You need to run a loop.
Separate string with '_'
Loop
for(NSString *s in ary)
{
NSArray *a = [s componentsSeparatedByString:#"_"];
[anotherArray addObject:[a objectAtIndex:0]];
}
and update your array..
Following might help you -
NSRange range = [string rangeOfString:#"_"];
NSString *finalString = [originalString substringToIndex:range.location];
you can have this in loop.
Or you can go for componentSeperatedByStrings.
This might help you
NSMutableArray *tmpAry = [[NSMutableArray alloc] init];
for(NSString *_string in _StringAry)
{
NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:#"_0123456789"];
_string = [[_string componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:#""];
[tmpAry addObject: [[_string copy] autorelease]];
}
NSLog(#"%#", tmpAry); // Gives the modified array
[tmpAry release];