I am trying to read the data from a local database file . But I am getting following error ...
Failed to open database with message 'no such table: recentlyViewd'.
sqlite3_stmt *statement = NULL;
sqlite3 *database;
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:#"databaseFile"
ofType:#"db"];
NSLog(#"%s",[sqLiteDb UTF8String]);
if (sqlite3_open([sqLiteDb UTF8String], &database) != SQLITE_OK) {
NSLog(#"Failed to open database!");
}
//NSLog(#" %d, %d ",sqlite3_step(statement),SQLITE_ROW);
const char *sql = "SELECT ID FROM recentlyViewd";
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
int primaryKey = sqlite3_column_int(statement, 0);
NSLog(#"%d",primaryKey);
}
}
else {
NSLog(#"Failed to open database with message '%s'.", sqlite3_errmsg(database));
sqlite3_close(database);
}
if (statement) {
sqlite3_finalize(statement);
}
Here I am just trying to open and read the database file.
Related
I have a sqlite db and i want to insert row in it . I used this code .
-(void)InsertRecords{
NSString *insertSQL = [NSString stringWithFormat:
#"INSERT INTO notes (id, title, content,dataAdd,category) VALUES (\"%#\", \"%#\", \"%#\" ,\"%#\",\"%#\")",
#"1",
#"mountain",
#"Prahova",
#"12.09.2019",
#"public"];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_stmt *updateStmt = nil;
sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
if(sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK){
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"data inserted");
}
else{
NSLog(#"Error while creating update statement. '%s'", sqlite3_errmsg(contactDB));
}
}
else{
NSLog(#"Error while creating update statement. '%s'", sqlite3_errmsg(contactDB));
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
and it's log : data inserted .
When i do this :
querySQL1 = #"select id from notes where category LIKE 'public'";
const char *query_stmt = [querySQL1 UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);
NSString *idNr;
while (sqlite3_step(statement) == SQLITE_ROW)
{
idNr =[[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
}
NSLog(#"%#",idNr);
idNr is null .
ANy ideea what is happening ? Any help will be appreciate . Thanks.
The NSString allocation was wrong statement, There were no need to alloc NSString object.
I changed :
NSString *idNr=#"";
while (sqlite3_step(statement) == SQLITE_ROW)
{
idNr =[NSString stringWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
}
Try to use this snippet:
sqlite3 *database;
if (sqlite3_open(<#DBPath#>, &database)
!= SQLITE_OK) {
sqlite3_close(database);
NSAssert(0, #"Failed to open database");
return nil;
}
char *update = "INSERT OR REPLACE INTO <#Table name#> (<#key1#>,<#key2#>) VALUES (?,?);";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, update, -1, &stmt, nil)
== SQLITE_OK) {
sqlite3_bind_text(stmt, <#the index#>,<#The text value#> , -1, NULL);
sqlite3_bind_int(stmt, <#the index#>, <#the int value#>);
}
if (sqlite3_step(stmt) != SQLITE_DONE){
NSLog(#"Error inserting version in table <#tablename#>");
return;
}
sqlite3_finalize(stmt);
sqlite3_close(database);
It uses the sqlite3_bind* functions to bind the values to the keys.
I am very new to iOS.
I am trying to create login page using the utility application in IOS 7. I created one view controller with sqlite database and I need to open that database in another view controller. If I try means, I got a break point error or signal error
Here is my code:
(void)login {
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT username FROM contacts WHERE username=\"%#\"",
username.text ];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
loginstatus.text=#"login sucessful";
}
else {
loginstatus.text = #"Match not found";
}
}
sqlite3_finalize(statement);
}
else
{
loginstatus.text=#"no db";
}
sqlite3_close(contactDB);
}
#end
I have loaded some data in my tableview using SQLite database in the format 1) some value, 2) some value so on. 1 and 2 are primary key, this is my code for delete which I have put on the swipe on delete function of the table view
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self openDB];
NSString *idofTable = [NSString new];
NSScanner *scanner = [[NSScanner alloc] initWithString:[self.tableLogView cellForRowAtIndexPath:indexPath].textLabel.text];
[scanner scanUpToString:#")" intoString:&idofTable];
NSLog(#"id=%#, text=%#", idofTable, [self.tableLogView cellForRowAtIndexPath:indexPath].textLabel.text);
if(editingStyle==UITableViewCellEditingStyleDelete)
{
char *error;
NSString *sql = [NSString stringWithFormat:#"DELETE FROM History WHERE 'id' = '%#'", idofTable];
NSLog(#"%#",sql);
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL,&error)!=SQLITE_OK)
{
sqlite3_close(db);
NSAssert(0,#"Could not create table");
}
else
{
NSLog(#"Data Deleted");
sqlite3_exec(db, "COMMIT", NULL, NULL, &error);
}
}
[tableLogView reloadData];
}
The log "Data Deleted" gets printed but the data doesnt get deleted in database nor in tableview. Any help appreciated. I dont know where the error is occuring, the query seems to be correct.
Try This
if(editingStyle==UITableViewCellEditingStyleDelete)
{
NSString *dbFilePath =[DBclass getDBPath];
sqlite3_stmt *deleteStmt = nil;
if(sqlite3_open([dbFilePath UTF8String], &database)==SQLITE_OK)
{
NSString *deleteString=[NSString stringWithFormat:#"delete from table where ID =%d",idValue];
const char *sql =[deleteString UTF8String];
if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating delete view statement. '%s'", sqlite3_errmsg(database));
if(SQLITE_DONE != sqlite3_step(deleteStmt))
NSAssert1(0, #"Error while deleting data. '%s'", sqlite3_errmsg(database));
else
NSLog(#"Success in deleting the medicine.");
sqlite3_close(database);
}
}
[tableLogView reloadData];
You arent finalising nor closing the Database.
sqlite3_finalize(stmt);
sqlite3_close(DB);
This is the right way to delete a row:
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *sql = [NSString stringWithFormat:#"DELETE FROM History WHERE 'id' = '%#'", idofTable];
const char *del_stmt = [sql UTF8String];
sqlite3_prepare_v2(contactDB, del_stmt, -1, & deleteStmt, NULL);
if (sqlite3_step(deleteStmt) == SQLITE_DONE)
{
NSLog(#"Data Deleted");
sqlite3_exec(db, "COMMIT", NULL, NULL, &error);
}else {
NSLog( #"Error: %s", sqlite3_errmsg(database) );
}
sqlite3_finalize(deleteStmt);
sqlite3_close(contactDB);
}
I have 6MB memory leaks when I'm working with sqlite. Now I'm testing getBookWithIdTest method.
-(IBAction) onTest:(id)sender
{
NSAutoreleasePool *myPool = [[NSAutoreleasePool alloc] init];
for (int i=0; i<100; i++)
{
[DatabaseManager getBookWithIdTest:i];
}
[myPool drain];
}
I have 6 MB leaked memory. But why?
+ (BookSettings *)getBookWithIdTest:(int)abookId
{
BookSettings *book = [[[BookSettings alloc] init] autorelease];
sqlite3 *database;
if(sqlite3_open([DatabaseManager databasePath], &database) == SQLITE_OK)
{
sqlite3_stmt *compiledStatement;
//FIRST PART
const char *sqlStatementBook = [[NSString stringWithFormat:#"SELECT * FROM t_abooks"] cStringUsingEncoding:NSASCIIStringEncoding];
if(sqlite3_prepare_v2(database, sqlStatementBook, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
}
}
else NSLog(#"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));
sqlite3_reset(compiledStatement);
//END FIRST PART
// SECOND PART
const char *sqlStatementAuthors = [[NSString stringWithFormat:#"SELECT * FROM t_authors"] cStringUsingEncoding:NSASCIIStringEncoding];
if(sqlite3_prepare_v2(database, sqlStatementAuthors, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
}
}
else NSLog(#"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));
sqlite3_reset(compiledStatement);
//END SECOND PART
sqlite3_finalize(compiledStatement);
} else NSLog(#"sqlite3_open error");
sqlite3_close(database);
return book;
}
But If I removed FIRST PART or SECOND PART I'll have no leaks.
For example
+ (BookSettings *)getBookWithIdTest:(int)abookId
{
BookSettings *book = [[[BookSettings alloc] init] autorelease];
sqlite3 *database;
if(sqlite3_open([DatabaseManager databasePath], &database) == SQLITE_OK)
{
sqlite3_stmt *compiledStatement;
//FIRST PART -removed
// SECOND PART
const char *sqlStatementAuthors = [[NSString stringWithFormat:#"SELECT * FROM t_authors"] cStringUsingEncoding:NSASCIIStringEncoding];
if(sqlite3_prepare_v2(database, sqlStatementAuthors, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
}
}
else NSLog(#"sqlite3_prepare_v2 error %s", sqlite3_errmsg(database));
sqlite3_reset(compiledStatement);
sqlite3_finalize(compiledStatement);
} else NSLog(#"sqlite3_open error");
sqlite3_close(database);
return book;
}
I solved the problem. I need to add
sqlite3_finalize(compiledStatement);
to the END of FIRST PART.
When I try to delete a row from my iphone app,I could correctly fetch the id but the row is not deleting.
-(void)delete_profile:(NSString *)mID {
NSLog(#"menuID: %#",mID);
sqlite3 *database;
sqlite3_stmt *deleteStmt=nil;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
if(deleteStmt == nil) {
const char *sql = "delete from item where menuid = ?";
if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
NSAssert1(0, #"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
}
//When binding parameters, index starts from 1 and not zero.
sqlite3_bind_int(deleteStmt, 1, [mID integerValue]);
if (SQLITE_DONE != sqlite3_step(deleteStmt))
NSAssert1(0, #"Error while deleting. '%s'", sqlite3_errmsg(database));
sqlite3_reset(deleteStmt);
}
sqlite3_close(database);
}
Please check your menuid Datatype . and bind before prepare or use like below example
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *sql = [NSString stringWithFormat:#"delete from item where menuid =%d",[mID intValue]];
const char *del_stmt = [sql UTF8String];
sqlite3_prepare_v2(contactDB, del_stmt, -1, & deleteStmt, NULL);
if (sqlite3_step(deleteStmt) == SQLITE_DONE)
{
} else {
}
sqlite3_finalize(deleteStmt);
sqlite3_close(contactDB);
}
must check your dbPath and db name (case sensitivity)
-(void)delete_profile:(NSString *)mID {
{
NSString *sql_str=[NSString stringWithFormat:#"DELETE FROM item where menuid = %d",[mID intValue]];
const char *sql = [sql_str UTF8String];
sqlite3 *database;
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
sqlite3_stmt *deleteStmt;
if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) == SQLITE_OK)
{
if(sqlite3_step(deleteStmt) != SQLITE_DONE )
{
NSLog( #"Error: %s", sqlite3_errmsg(database) );
}
else
{
// NSLog( #"row id = %d", (sqlite3_last_insert_rowid(database)+1));
NSLog(#"No Error");
}
}
sqlite3_finalize(deleteStmt);
}
sqlite3_close(database);
}