What am i doing wrong here?? i have three table in database, and i need to delete all data from them.
-(void)deleteAllDataFromTables
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSLog(#"%#", appDelegate.databasePath);
int result = sqlite3_open([appDelegate.databasePath UTF8String], &db);
if(result == SQLITE_OK)
{
NSLog(#"deleteCalled");
const char *deleteData = "delete from CustomerNumberTable";
int outcome = sqlite3_prepare_v2(db, deleteData, -1, &statement, NULL);
if(outcome == SQLITE_OK)
{
int res = sqlite3_step(statement);
if(res == SQLITE_DONE)
{
NSLog(#"deleted from Customer Table");
}
else
{
NSLog(#"not able to execute step statement");
}
}
else
{
NSLog(#"not deleted from Customer Table");
NSLog(#"the error is %s", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
const char *deleteData2 = "delete from KeyCodeTable";
int outcome2 = sqlite3_prepare_v2(db, deleteData2, -1, &statement, NULL);
if(outcome2 == SQLITE_OK)
{
int res = sqlite3_step(statement);
if(res == SQLITE_DONE)
{
NSLog(#"deleted from KeyCodeTable");
}
else
{
NSLog(#"not able to execute step statement");
}
}
else
{
NSLog(#"not deleted from KeyCodeTable");
NSLog(#"the error is %s", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
const char *deleteData3 = "delete from SRNumberTable";
int outcome3 = sqlite3_prepare_v2(db, deleteData3, -1, &statement, NULL);
if(outcome3 == SQLITE_OK)
{
int res = sqlite3_step(statement);
if(res == SQLITE_DONE)
{
NSLog(#"deleted from SRNumberTable");
}
else
{
NSLog(#"not able to execute step statement");
}
}
else
{
NSLog(#"not deleted from SRNumberTable");
NSLog(#"the error is %s", sqlite3_errmsg(db));
}
sqlite3_finalize(statement);
}
else
{
NSLog(#"Not able to open database for deleting");
}
}
On console, i am getting
Application[54974:a0b] deleteCalled
2014-01-03 16:12:55.442 Application[54974:a0b] deleted from Customer Table
2014-01-03 16:12:55.443 Application[54974:a0b] deleted from KeyCodeTable
2014-01-03 16:12:55.445 Application[54974:a0b] deleted from SRNumberTable
But when i am opening the database in on mac, the data is intact there..!! What is wrong with my code?? why isn't the data getting deleted??
I think you are copying your database from bundle to documents directory. So, the updated database will be present in documents directory. You seems to look the database from your workspace which is never going to be of updated one.
Your code opens database with sqlite3_open(), but does not close it using sqlite3_close(). This means that your database is probably still in open transaction, and quite possibly has 2 files: one is main database.db, and journal file database.db-journal which contains uncommitted intent to delete your tables.
You should make sure that your application closes database before you copy database file from the device to the host. At the very least, copy both main database AND journal file as one set. It is still likely to have consistency issues, but you should see your changes.
Try instead of sqlite3_prepare_v2 to sqlite3_exec. This working fine for me.
Just Ref:
char *errMsg;
const char *dbPath = [databasePath UTF8String];
if (sqlite3_open(dbPath, &database) == SQLITE_OK) {
NSLog(#"Db OPened ");
NSString *queryLists = [NSString stringWithFormat:#"delete from Table_name"];
const char *query_stmt = [queryLists UTF8String];
if (sqlite3_exec(database, query_stmt, NULL, NULL, &errMsg)
!= SQLITE_OK){
NSLog(#"Error %s", errMsg);
NSLog(#"Table Not Deleted");
}
}
1st thing Don't write same functionality code repeatedly in place create one function that delete all data from given table name,
2nd make sure you are looking at correct sqlite file in mac.
look this,
-(void)deleteAllData
{
[self deleteTable: CustomerNumberTable];
[self deleteTable: SRNumberTable];
[self deleteTable: KeyCodeTable];
}
-(void)deleteTable:(NSString *)table;
{
const char *dbpath=[appDelegate.databasePath UTF8String];
if (sqlite3_open(dbpath, &dtdb)==SQLITE_OK)
{
sqlite3_stmt *stmt;
const char *qry=[[NSString stringWithFormat:#"delete from %#",table] UTF8String];
sqlite3_prepare_v2(dtdb, qry, -1, &stmt, NULL);
if (sqlite3_step(stmt)==SQLITE_DONE)
{
NSLog(#"%# Table data deleted",table);
}
sqlite3_finalize(stmt);
sqlite3_close(dtdb);
}
}
Related
I'm starting to develop a small iOS application just for learning, and I'm attempting to use SQLite to store my app data. I'm using a simple CRUD operations, but when I compile the app (I want to try it in the device emulator) i've got this error
I've added libsqlite3.dylib to my project dependences.
I've tried many solutions, bun no one fix my problem.
Any idea about what i'm doing wrong?
Thank you so much
ps. The code are the following:
-(int) getPushCount {
int count = 0;
const char *dbpath = [databasePath UTF8String];
if(sqlite3_open(dbpath, &database) == SQLITE_OK) {
NSString *query = [NSString stringWithFormat:#"SELECT COUNT(*) FROM recived_push WHERE read = 1"];
const char *getQuery = [query UTF8String];
if(sqlite3_prepare_v2(database, getQuery, -1, &statement, NULL) == SQLITE_OK) {
while(sqlite_step(statement) == SQLITE_ROW) {
count = sqlite_column_int(statement,0);
}
} else {
NSLog(#"Failed from sqlite3_prepare_v2.");
NSLog(#"Error is: %s", sqlite3_errmsg(database));
}
sqlite3_finalize(statement);
sqlite3_close(database);
} else {
NSLog(#"Oops! Something went terribly wrong...");
NSLog(#"%s",sqlite3_errmsg(database));
return nil;
}
NSLog(#"**Count: %d",count);
return count;
}
change your code like this..
while (sqlite3_step(statement)== SQLITE_ROW) {
count = sqlite3_column_int(statement, 0);
}
in my application I have stored the data in my sqlite3 database and uploading all my sqlite3 database to my online sever, once it copied to my server, I want to delete all my records I have stored in my local database.
I have two view controller first view controller, where I store the data to sqlite3 second view controller I have used to load the data to my server I have given like if user connected to network it has to upload the data.
I want like after uploading data to my server I want to delete all the records in my local database.
I have already tried some method its not working please tell me how to resolve this.
- (void) Delete_LoginData
{
if(sqlite3_open([self.filePath UTF8String], &db) == SQLITE_OK)
{
const char *sql = "DELETE * FROM reg";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, sql,-1, &statement, NULL) == SQLITE_OK)
{
sqlite3_reset(statement);
}
}
}
I called this function in my Reachability.
- (BOOL)reachable
{
Reachability *reach = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [reach currentReachabilityStatus];
if(internetStatus == NotReachable)
{
}
else
{
[self Delete_LoginData];
}
return YES;
}
My file path.
- (NSString *) filePath
{
NSArray *paths = filepath, YES);
return [[paths objectAtIndex:0] stringByAppendingPathComponent:#"bp.sqlite"];
}
Code open the db.
- (void)openDB
{
if (sqlite3_open([[self filePath] UTF8String], &db)!=SQLITE_OK)
{
sqlite3_close(db);
NSAssert(0, #"Database failed to open");
}
else
{
NSLog(#"database opened");
}
}
I have used this above method to delete record from my local database its not working please to where I'm doing wrong.
Thanks.
Use sqlite3_step instead of sqlite3_reset to execute the query
if(sqlite3_open([self.filePath UTF8String], &db) == SQLITE_OK)
{
const char *sql = "DELETE FROM reg";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(db, sql,-1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE){
// executed
}else{
//NSLog(#"%s",sqlite3_errmsg(db))
}
}
sqlite3_finalize(statement);
}
sqlite3_close(db);
sqlite3_prepare_v2 doesn't act the execution You need to take a step into it
if(sqlite3_open([self.filePath UTF8String], &db) == SQLITE_OK){
NSString *sql = "DELETE FROM reg";
const char *query_st = [sql UTF8String];
if(sqlite3_prepare_v2(database, query_st, -1, &statement, NULL) == SQLITE_OK){
// Loop through the results and add them to the feeds array
if(sqlite3_step(statement) == SQLITE_DONE) {
// Deleted records,....
}else{
NSAssert1(0,#"Error when counting rows %s",sqlite3_errmsg(database));
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
You can do like this,
sqlite3_stmt *statement;
- (void) Delete_LoginData
{
// Open database first
if(sqlite3_open([self.filePath UTF8String], &db) == SQLITE_OK)
{
// Sql query
const char *sql = "DELETE FROM reg";
// We are preparing the statement here
if(sqlite3_prepare_v2(db, sql,-1, &statement, NULL) == SQLITE_OK)
{
// If that prepared statement is OK then we can execute that statement
if(sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#" Records deleted successfully..");
}
else
{
NSLog(#" Opppsss..... Unable to delete records..");
}
}
// Finalizing the statement
sqlite3_finalize(statement);
// Closing the database
sqlite3_close(db);
}
Hope this work for you...
Thank you...
I am deleting records from the database.and it is working fine .But it is crashing when there is no data in the database. The code is given Below
int noOfRecordsDeleted;
[self openDatabaseConnection];
// query = [NSString stringWithFormat:#"delete from %# where %#",query];
NSLog(#"del query=%#",query);
const char *sql = [query cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *statement = nil;
if(sqlite3_prepare_v2(dataBaseConnection,sql, -1, &statement, NULL)!= SQLITE_OK)
{
NSAssert1(0,#"error preparing statement",sqlite3_errmsg(dataBaseConnection));
}
else
{
int success = sqlite3_step(statement);
NSLog(#"%d",success);
}
sqlite3_finalize(statement);
noOfRecordsDeleted = sqlite3_changes(dataBaseConnection);
[self closeDatabaseConnection];
return noOfRecordsDeleted;
It is working fine. But if i am adding data i empty database it is crashing
[self openDatabaseConnection];
NSString *query;
query = [NSString stringWithFormat:#"insert into Userdetail(aboutMe,anniversary,areacode,birthdate,device_id,chat_id,emailid,gender ,image,last_login,latitute,longitude,Looking_For,mobilenumber,mobilenumber1 ,mobilenumber2,mood,name,password,place,profileviews,statusmessage) values ('%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#','%#')",aboutMe,anniversarydate,areacode,birthdate,
device_id,chat_id,email_id,gender,image,last_login,latitude,longitude,looking_for,mobilenumber,mobilenumber1,mobilenumber2,mood,name,password,place,profileviews,statusmessage];
NSLog(#"saveNewTemplateData query=%#",query);
const char *sql = [query cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *statement = nil;if(sqlite3_prepare_v2(dataBaseConnection,sql , -1, &statement, NULL)!= SQLITE_OK)
{
NSAssert1(0,#"error preparing statement",sqlite3_errmsg(dataBaseConnection));
}
else
{
sqlite3_step(statement);
}
sqlite3_finalize(statement);
[self closeDatabaseConnection];
above is the code
First of all check the Database is empty or not by using the following statement
if(sqlite3_column_text(statement, 0) != nil){//Data exists}
Then in the if method add the code to delete the dataBase.
It works fine.
query = [NSString stringWithFormat:#"delete from %# where %#",query];
what does this query means I think its the mistake of your query please doublecheck it
if the error persists programmatically check
by using if(sqlite3_column_text(statement, 0) != nil)
One issue I saw that you are calling sqlite3_finalize(statement); regardless the state of sqlite3_prepare_v2. You only need to call sqlite3_finalize if the sqlite3_prepare_v2 executed successfully.
Change that to:
if(sqlite3_prepare_v2(dataBaseConnection,sql, -1, &statement, NULL)!= SQLITE_OK)
{
NSAssert1(0,#"error preparing statement",sqlite3_errmsg(dataBaseConnection));
}
else
{
int success = sqlite3_step(statement);
NSLog(#"%d",success);
sqlite3_finalize(statement);
}
if (condition for check database empty (select * from table)){
}else{
//Perform Delete operation
}
I have sqlite database in iPhone app and there I store questions.
I get one question, user answers YES or NO, result is showing on second viewController and when second viewController is closing I get another question.
Everything works perfectly and suddenly after 30-40 questions, program cannot open database.
if(sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK) - it fails.
Any answers?
This function:
// get Question from Database
- (void)getQuestion:(int)getQ
{
NSLog(#"getQuestion Started");
sqlite3 *database;
sqlite3_stmt *compiledStatement = NULL;
if(sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK)
{
NSLog(#"sqlite opened");
const char *sql = "Select QuestionID, Question from cQuestions WHERE ExerciseLinID = ? ORDER BY QuestionID LIMIT ?, 1";
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating detail view statement. '%s'", sqlite3_errmsg(database));
int curExID = [[listOfExID objectAtIndex:ExSel] integerValue];
sqlite3_bind_int(compiledStatement, 1, curExID);
sqlite3_bind_int(compiledStatement, 2, getQ);
if(SQLITE_DONE != sqlite3_step(compiledStatement))
{
NSLog(#"Got one record");
selectedQues = sqlite3_column_int(compiledStatement, 0);
NSLog(#"selectedQues = %i", selectedQues);
const char *QuNam = (char *)sqlite3_column_text(compiledStatement, 1);
if(QuNam == nil)
NSLog(#"!!! No data found.");
else
{
if( iWhat == YES )
_labQuestion.text = [[NSString alloc] initWithCString:QuNam encoding:NSASCIIStringEncoding];
else
_labQuestionPad.text = [[NSString alloc] initWithCString:QuNam encoding:NSASCIIStringEncoding];
}
}
}
else
{
NSLog(#"!!! Open error. %s", sqlite3_errmsg(database));
NSLog(#"!!! Open error. %d", sqlite3_errcode(database));
}
sqlite3_close(database);
}
Add some NSLog to understand which is the error.
if(sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK) {
// Your Code
}
else {
NSLog(#"sqlite3_open failed. Error:%d. %s", sqlite3_errcode(database), sqlite3_errmsg(database));
}
One potential problem is the fact that you aren't closing your database, are you closing it?
sqlite3_close(database);
I am trying to update one of my table's column upon button click.
The issue is that code isn't giving any exception still it is not updating the table.
When the view controller load again , it shows the old value rather then updated one.
Code for update db is attached below.
#try {
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *sqlStatement = #"UPDATE messagesTable SET Favorite = 1 where MessageID=";
sqlStatement = [sqlStatement stringByAppendingString:msgId];
NSLog(#"sql statement: %#", sqlStatement);
const char *sql = [sqlStatement UTF8String];
int result = sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL);
if(result != SQLITE_OK) {
NSLog(#"Prepare-error #%i: %s", result, sqlite3_errmsg(database));
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
#catch (NSException *exception) {
NSLog(#"Name: %#",exception.name);
NSLog(#"Reason: %#",exception.reason);
}
#finally {
}
Any suggestions are welcome. :-)
You are not calling sqlite3_step, which performs the update. After the sqlite3_prepare and before the sqlite3_finalize, you need something like:
if (sqlite3_step(compiledStatement) != SQLITE_DONE)
NSLog(#"%s: step error: %s", __FUNCTION__, sqlite3_errmsg(database));