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 create a database. In my view i am checking provided dat is already exist or not. If exist then retrun Yes otherwise no. for this i have use this code..
+(BOOL)compareDataInDB:(NSString *)Start_text EndAddress:(NSString *)End_text Categories:(NSString *)Category_text{
BOOL flag=FALSE;
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
//int LastId="select Max(place_id) from Places";
// NSLog(#"%i",LastId);
const char *sql = "select * from Places where Start_text = Start_text and End_text = End_text and Category_text = Category_text";
sqlite3_stmt *stmt;
int rtnVal = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if( rtnVal == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
flag=TRUE;
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
if(flag==TRUE)
return YES;
else
return NO;
}
on button action i have use this code
-(IBAction)Find{
BOOL tmp=[GlobalClass compareDataInDB:txtstart.text EndAddress:txtend.text Categories:txtCategory.text];
if(tmp)
[GlobalClass storeDataInDB:txtstart.text EndAddress:txtend.text Categories:txtCategory.text];
else
NSLog(#"exsit data in database");
}
Now problem is that it always return no. What is error in this code? And How make compression in data from data base?
Thanks in advance...
You have to use following code for SQL Statement.
NSString *tempSQL = [[NSString alloc] initWithFormat:#"select * from Places where Start_text = '%#' and End_text = '%#' and Category_text = '%#'", Start_text, Start_text, Category_text];
const char *sql = [tempSQL cStringUsingEncoding:NSUTF8StringEncoding];
[tempSQL release];
+(BOOL)compareDataInDB:(NSString *)Start_text EndAddress:(NSString *)End_text Categories:NSString *)Category_text
{
BOOL flag=NO;
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK) {
//int LastId="select Max(place_id) from Places";
// NSLog(#"%i",LastId);
const char *sql = "select * from Places where Start_text = Start_text and End_text = End_text and Category_text = Category_text";
sqlite3_stmt *stmt;
int rtnVal = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
if( rtnVal == SQLITE_OK)
{
sqlite3_bind_text(stmt, 1, [Start_text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 2, [End_text UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(stmt, 3, [Category_text UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(stmt) == SQLITE_ROW) {
flag=YES;
}
}
sqlite3_finalize(stmt);
}
sqlite3_close(db);
return flag;
}
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.
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).
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
Please find the below function.it works fine for the first time.
I mean , when i add a text it works for the first time , then when i try to add a new text again , it give me like "DB is locked".
I am not sure how it works for the first not again?
Please let me know what is the problem in my code.
//==================================================================
- ( BOOL ) addNewSimpleTemplates:(NSString*)dbPath:(NSString*)title{
//==================================================================
BOOL returnVal = NO;
NSString *maxValuePosition;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *selectSQL = [NSString stringWithFormat:#"select MAX(pr.position) FROM phrase_reference pr inner join storyboard_main_categories smc on smc.id = pr.main_category_id where smc.category_name = %#", #"'Simple Templates'"];
const char *sql = [selectSQL UTF8String];
sqlite3_stmt *selectStmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
//
char *localityChars = (char*)sqlite3_column_text(selectStmt, 0);
if (localityChars == NULL)
maxValuePosition = nil;
else
maxValuePosition = [NSString stringWithUTF8String: localityChars];
// increment the postion value
int postion = [maxValuePosition intValue] + 1;
NSLog(#"%d look here %# gtitle", postion , title);
selectStmt = nil;
//saving a new simple phrase is started here
sql = "insert into storyboard_phrases(phrase) Values(?)";
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
sqlite3_bind_text(selectStmt, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(selectStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s just here itself", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
///
int phrase_id = sqlite3_last_insert_rowid(database);
int sub_category_id = 0;
selectStmt = nil;
NSString *selectSQL = [NSString stringWithFormat:#"select id from storyboard_main_categories where category_name = %#", #"'Simple Templates'"];
sql = [selectSQL UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectStmt) == SQLITE_ROW)
{
char *localityChars = (char*)sqlite3_column_text(selectStmt, 0);
NSString* main_category_id = [NSString stringWithUTF8String: localityChars];
sql = "insert into phrase_reference (phrase_id, sub_category_id,main_category_id, position) Values(?,?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &selectStmt, NULL) == SQLITE_OK){
sqlite3_bind_int(selectStmt, 1, phrase_id);
sqlite3_bind_int(selectStmt, 2, sub_category_id);
sqlite3_bind_int(selectStmt, 3, [main_category_id intValue]);
sqlite3_bind_int(selectStmt, 4, postion);
}
if(sqlite3_step(selectStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
}else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
returnVal = YES;
}
}
}
sqlite3_finalize(selectStmt);
selectStmt = nil;
/////
}
}
}
sqlite3_finalize(selectStmt);
}
NSLog(#"sdsdfsdsd closed");
sqlite3_close(database);
return returnVal;
}
I am not going deep within your code but make sure that -
Dont forget to finalize the statements before close the database.
And do reset after every execution of the sqlite3 statement
You are not using reset statement any where.. Try using reset statement..
I am using the below function in my app,and i have started using the sq-lite recently and i would like to get your opinion that i am going with that correctly or not.
Since i am facing db locked issue in my app when searched i found that i need to use sqlite3 finalise statement.
what i am not sure is do i need to place one finalise statement for each sqlite3 prepare statement
Please let me know
- ( BOOL ) addNewCate:(NSString*)dbPath:(NSString*)title:(NSString*)tierOneID:(NSString*)tierTwoID{
BOOL returnVal = NO;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "insert into storyboard_phrases(phrase) Values(?)";
sqlite3_stmt *addStmt;
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
sqlite3_bind_text(addStmt, 1, [title UTF8String], -1, SQLITE_TRANSIENT);
}
if(sqlite3_step(addStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
int ph_id = sqlite3_last_insert_rowid(database);
int sub_category_id = [tierTwoID intValue];
int main_category_id = [tierOneID intValue];
addStmt = nil;
sql = "insert into phrase_reference(phrase_id, sub_category_id,main_category_id) Values(?,?,?)";
if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) == SQLITE_OK){
sqlite3_bind_int(addStmt, 1, ph_id);
sqlite3_bind_int(addStmt, 2, sub_category_id);
sqlite3_bind_int(addStmt, 3, main_category_id);
}
if(sqlite3_step(addStmt) != SQLITE_DONE ) {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
} else {
NSLog( #"Insert into row id = %d", sqlite3_last_insert_rowid(database));
returnVal = YES;
}
}
sqlite3_finalize(addStmt);
}
sqlite3_close(database);
return returnVal;
}
hii you get the locked issues in log right, that means your database is open and you doing some changes in that database so close database and try again to run application and insert in table...
hope this will help you..
I'm using two methods to r/w sqlite table:
+ (NSString *) getWeatherData:(int)rowID:(int)columnID {
NSString *savedWeatherData = [[NSString alloc] init];
if (sqlite3_open([[DBController getDBPath] UTF8String], &database) == SQLITE_OK) {
const char *sql = "select * from TABLE where id=?";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(selectstmt, 1, rowID);
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
Weather *weatherObj = [[Weather alloc] initWithPrimaryKey:primaryKey];
weatherObj.weatherData = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, columnID)];
savedWeatherData = weatherObj.weatherData;
[weatherObj release];
}
}
}
return [savedWeatherData autorelease];
}
And to save some data:
+ (void) saveDataToDataBase:(NSString *)columnToUpdate:(int)rowID:(NSString *)value {
if (sqlite3_open([[DBController getDBPath] UTF8String], &database) == SQLITE_OK) {
updateStmt = nil;
NSString *sqlString = [[#"update TABLE set " stringByAppendingString:columnToUpdate] stringByAppendingString:#"=? where id=?"];
const char *sql = [sqlString UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK) {
NSAssert1(0, #"Error while creating update statement. '%s'", sqlite3_errmsg(database));
} else { // select statement ok
sqlite3_bind_text(updateStmt, 1, [value UTF8String], -1, SQLITE_TRANSIENT); // replace first ? with value
sqlite3_bind_int(updateStmt, 2, rowID); // replace second ? with rowID
if(SQLITE_DONE != sqlite3_step(updateStmt)) {
NSAssert1(0, #"Error while updating. '%s'", sqlite3_errmsg(database));
} else {
// NSLog(#"Update completed !!!");
}
sqlite3_reset(updateStmt);
}
sqlite3_finalize(updateStmt);
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
With the Instruments, I see pretty big memory consumption for sqlite.
Info: during app startup, cca 100 different kind of data is stored in DB - for each data, this saveDataToDataBase method is called. (In case of no internet connection - getWeatherData would be used - and again cca 100 different kind of data would be read)
Please, can you instruct me - is it possible to optimize memory consumption.
Thanks a lot!
Kind regards!