sqlite3_prepare not ok in xcode - ios

I have the following code inside a .m file and i can get to where NSLog says OK BEFORE PREPARE but i cant get to NSLog OK INSIDE PREPARE. What is wrong with the if statement line that is not working? I know i have an sqlite database. I do have a table called school. It has one row of data in the school table.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
arrayOfSchools = [[NSMutableArray alloc]init];
[[self schoolTableView]setDelegate:self];
[[self schoolTableView]setDataSource:self];
sqlite3_stmt *statement;
if(sqlite3_open([dbPathString UTF8String], &eduLogDB)==SQLITE_OK){
[arrayOfSchools removeAllObjects];
NSString *querySql = [NSString stringWithFormat:#"SELECT * FROM school"];
const char *query_sql = [querySql UTF8String];
NSLog(#"OK BEFORE PEPARE");
if(sqlite3_prepare(eduLogDB, query_sql, -1, &statement, NULL)==SQLITE_OK){
NSLog(#"OK INSIDE PREPARE");
while (sqlite3_step(statement)==SQLITE_ROW){
NSString *schoolName = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement,1)];
School *school = [[School alloc]init];
[school setSchoolName:schoolName];
[arrayOfSchools addObject:school];
}
}
}
[[self schoolTableView]reloadData];
}

Related

SQlite insert data in iOS App

I'm following some tutorials from youtube for sqlite crud operations. I have textfields with name, contact and address , when i hit save button it show message in console that data is saved but when i look at database it nothing appears there. My database path is correct and no errors in code but 'm confused why it isn't going. My path code is this,
-(void)copyandpaste{
NSArray *arr1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *str1 = [arr1 objectAtIndex:0];
strpath = [str1 stringByAppendingPathComponent:#"personinfo.sqlite"];
if (![[NSFileManager defaultManager]fileExistsAtPath:strpath]) {
NSString *local = [[NSBundle mainBundle]pathForResource:#"personinfo" ofType:#"sqlite"];
[[NSFileManager defaultManager]copyItemAtPath:local toPath:strpath error:nil];
}
NSLog(#"%#",strpath);
}
this method is called in appdelegate.m file,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self copyandpaste];
return YES;
}
I have mad NSObject class for database connection. the connection code is this,
-(id)init{
appdel = (AppDelegate *)[[UIApplication sharedApplication]delegate]; //get all methods in AppDelegate
strmain = appdel.strpath;
return self;
}
-(NSMutableArray *)getalluser:(NSString *)query{
arrdata = [[NSMutableArray alloc]init];
if (sqlite3_open([strmain UTF8String], &(database))==SQLITE_OK) {
sqlite3_stmt *connection;
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &connection, nil)==SQLITE_OK) {
while (sqlite3_step(connection)==SQLITE_ROW) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
NSString *str12 = [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(connection, 0)]; //name in first column
NSLog(#"Checking");
NSString *str13 = [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(connection, 1)]; //contact in second column
NSString *str14 = [[NSString alloc]initWithUTF8String:(char *)sqlite3_column_text(connection, 2)]; //address in third column
[dic setObject:str12 forKey:#"name"];
[dic setObject:str13 forKey:#"contact"];
[dic setObject:str14 forKey:#"address"];
[arrdata addObject:dic];
}
}
sqlite3_finalize(connection);
}
sqlite3_close(database);
return arrdata;
}
My save button code with query is this,
- (IBAction)btnSave:(id)sender {
NSString *saveDATA=[[NSString alloc]initWithFormat:#"insert into stuInfo values('%#','%#','%#')",txtName,txtContact,txtAddress];
dboperations *db = [[dboperations alloc ]init];
BOOL ds = [db getalluser:saveDATA];
if (ds) {
NSLog(#"Data Saved in database");
}else{
NSLog(#"Data is not Saved in database");
}
}
- (NSString*) saveData:(NSString *)name cont:(NSString *)contacts add:(NSString *)address
{
const char *dbpath = [YOUR_DATA_BASE_PATH UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSLog(#"=============saveData open==============");
const char * query="insert into YOUR_TABLE_NAME(name,contacts,address) values(?,?,?);";
sqlite3_stmt *inset_statement;
char *errMsg;
if (sqlite3_open(dbpath, &database)!=SQLITE_OK) {
NSLog(#"Error to Open");
return nil;
}
if (sqlite3_prepare_v2(database, query , -1,&inset_statement, NULL) != SQLITE_OK )
{
NSLog(#"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(database), sqlite3_errcode(database));
NSLog(#"Error to Prepare");
return nil;
}
//No of data to insert
sqlite3_bind_text(inset_statement,1,[name UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(inset_statement,2,[contacts UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(inset_statement,3,[address UTF8String], -1, SQLITE_TRANSIENT);
if(sqlite3_step(inset_statement)== SQLITE_DONE)
{
long long lastRowId = sqlite3_last_insert_rowid(database);
NSString *rowId = [NSString stringWithFormat:#"%d", (int)lastRowId];
NSLog(#"Row ID of Last Inserted row %#",rowId);
sqlite3_finalize(inset_statement);
sqlite3_close(database);
NSLog(#"=============saveData Close==============");
return rowId;
}
if (sqlite3_exec(database, query, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
NSLog(#"Failed to Insert msg in message table Error = %s",errMsg);
sqlite3_finalize(inset_statement);
sqlite3_close(database);
NSLog(#"=============saveData Close==============");
return nil;
}
else
{
sqlite3_finalize(inset_statement);
}
}
NSLog(#"=============saveData Close==============");
sqlite3_close(database);
return nil;
}
You are not handling if there are any errors in the SQLite statements you are preparing.
The function will always return true because arrdata is never null
You need to add anelse statements to your
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &connection, nil)==SQLITE_OK)
to show any errors...
NSLog(#"%s",sqlite3_errmsg(database));

How to fetch values from sqlite database and stored into array using objective c?

- (IBAction)submit:(id)sender {
NSString *sql = [NSString stringWithFormat:#"INSERT INTO employeedetails('employee_name','employee_mailid','employee_username','employee_password')VALUES('%#','%#','%#','%#')",self.textname.text,self.textmailid.text,self.textusername.text,self.textpassword.text];
NSLog(#"%#The inserted values are",sql);
char *err;
if (sqlite3_exec(db,[sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
sqlite3_close(db);
NSAssert(0,#"sdsadadsderererwrcould not update the tabel");
}
else{
NSLog(#"nbbbbbbbbbbbbtable updated");
}
}
-(void)selectfn
{
const char *sqlStatement = "SELECT * FROM employeedetails"; // Your Tablename
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(db, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
[name removeAllObjects];
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
[name addObject:[NSString stringWithFormat:#"%s",(char *) sqlite3_column_text(compiledStatement, 0)]];
NSLog(#"The array value is %#",name);
}
}
sqlite3_finalize(compiledStatement);
sqlite3_close(db);
}
Already I have insert values into table...Now I want to fetch values from the table and want to store one nsarray and display into tableview....help me plz
You can create Model class something like this
Header file
#import "Model.h"
#implementation Model
#synthesize _firstValue ,_secondValue ;
#end
Implementation file
#import <Foundation/Foundation.h>
#interface Model: NSObject
{
NSString *_firstValue ;
NSString *_secondValue ;
}
#property (nonatomic,retain) NSString *_firstValue ;
#property (nonatomic,retain) NSString *_secondValue ;
#end
Now fetching data using below lines...
//Cretae a array to store values
NSMutableArray *retArr = [[NSMutableArray alloc]init];
if ([self dbOpen]) // check db is open
{
const char *sqlStatement = [YOUR_QUERY UTF8String];
sqlite3_stmt *statement;
if( sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK )
{
while( sqlite3_step(statement) == SQLITE_ROW )
{
// used model to fill the set of data
Model *obj = [[Model alloc] init];
obj._firstValue = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 0)];
obj._secondValue = [NSString stringWithUTF8String:(char *) sqlite3_column_text(statement, 1)];
[retArr addObject:obj];
obj = nil;
}
}
else { NSLog( QRY_EXEC_ERROR, sqlite3_errmsg(database) ); } // excute if any error
sqlite3_finalize(statement); // finalizing
sqlite3_close(database); // clossing the DB
}
Finally use retArr which holds your DB values.....

Tableview app delete from sqlite database

I have a tableview app where I am trying to delete data stored in a sqilte browser. When I delete the data in the simulator, the data does not reappear after closing and re-running the simulator. However, when I open the database where the original data was kept using sqlite browser, the deleted data still appears in the sqlite browser. Can someone advise on what I'm missing?
#import "DataAccess.h"
#import "sqlite3.h"
#import "Product.h"
#import "Company.h"
#implementation DataAccess
NSString *dbPathString;
sqlite3 *companyDB;
sqlite3 *productDB;
-(void) setCompanyListFromDB
{
NSLog(#"setCompanyListFromDB");
dbPathString = #"/Users/user/Desktop/telecom.db";
self.companyList = [self readCompanyDataFromDB];
NSLog(#"read all company data");
}
-(void) deleteCompany:(Company *)company andDeleteProduct:(NSIndexPath*)indexPath{
//pass the company from the tableview, then pass the row from the tablewview
Product *product = [company.products objectAtIndex:indexPath.row];
[self deleteProductFromDB:product.name];
[company.products removeObjectAtIndex:indexPath.row];
}
-(NSMutableArray*)readCompanyDataFromDB //returning companyList bc we want to use this for displaying in tableview
{
NSLog(#"readDataFromDB");
NSMutableArray *companyList = [[NSMutableArray alloc]init];
sqlite3_stmt *statement ;
if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) { //when we do this process for products, we can skip opening companyDB bc it is already open - just do it once
NSLog(#"sqlite3_open");
// Reading Companies ....................................Start
NSLog(#"Reading Companies");
NSString *querySQL = [NSString stringWithFormat:#"SELECT * FROM company"];
NSLog(#"Company SQL: %#", querySQL);
const char *query_sql = [querySQL UTF8String]; //converting to type of string C understands
if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(#"sqlite3_prepare");
while (sqlite3_step(statement)== SQLITE_ROW) //while stepping through database, if it returns a row it should keep going, cuts out if it returns something other than SQLITE_ROW
{
// 1|Apple|AAPL|593.1|apple.jpeg
NSString *companyID = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
NSString *name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSString *stockSymbol = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
NSString *stockPrice = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
NSString *logo = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)];
//Company *company = [Company initWithName:name logo:logo symbol:stockSymbol price:stockPrice];
Company *company = [[Company alloc]init];
company.companyID = companyID;
company.name = name;
company.logo = logo;
company.stockSymbol = stockSymbol;
company.stockPrice = stockPrice;
[company print];
[companyList addObject:company];
}
}
// Reading Companies ....................................Done
// Reading Products for each Company ....................................Start
sqlite3_close(companyDB);
}
for(Company *company in companyList){
NSMutableArray *productList = [self readProductDataFromDBForCompanyID:company.companyID ];
company.products = productList;
}
return companyList;
}
-(NSMutableArray*) readProductDataFromDBForCompanyID:(NSString*)companyId {
NSLog(#"readProductDataFromDB");
NSMutableArray *productList = [[NSMutableArray alloc]init];
sqlite3_stmt *statement ;
if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) { //when we do this process for products, we can skip opening companyDB bc it is already open - just do it once
NSLog(#"sqlite3_open");
// Reading Products ....................................Start
NSLog(#"Reading Products");
NSString *querySQL = [NSString stringWithFormat:#"select * from product where productid = %#", companyId];
NSLog(#"Product SQL: %#", querySQL);
const char *query_sql = [querySQL UTF8String]; //converting to type of string C understands
if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(#"sqlite3_prepare");
while (sqlite3_step(statement)== SQLITE_ROW) //while stepping through database, if it returns a row it should keep going, cuts out if it returns something other than SQLITE_ROW
{
NSString *name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
NSString *website = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
NSString *productID = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
Product *product = [[Product alloc]init];
product.productID = productID;
product.name = name;
product.website = website;
[product print];
[productList addObject:product];
}
}
// Reading Products ....................................Done
sqlite3_close(companyDB);
}
// insert into product values('iPad','0','http://www.apple.com/ipad/',1);
return productList;
}
-(void) deleteProductFromDB:(NSString*)productname{
if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) { //when we do this process for products, we can skip opening companyDB bc it is already open - just do it once
NSLog(#"sqlite3_open");
NSString *querySQL = [NSString stringWithFormat:#"delete from product where productname = '%#'", productname];
NSLog(#"Product Delete SQL: %#", querySQL);
const char *deleteQuery = [querySQL UTF8String]; //converting to type of string C understands
if (sqlite3_exec(companyDB, deleteQuery, NULL, NULL, nil)==SQLITE_OK)
{
NSLog(#"Product Deleted");
}
sqlite3_close(companyDB);
}
}
#end
As it was mentioned in the comments - when your app will run in iOS it will be sandboxed. Moreover, if you will include sqlite database as a resource file with your application - it will get into /Resources folder inside your application folder. And everything in that folder is not writable.
So if you need a database that you want to read/write you need to put it under /Documents folder inside your application folder.

Store String from SQLite to NSMutableArray

I'm working on an app that displays various feeds using ASIHTTPRequest. The user can add new sources to the app that are stored in an SQLite database. Currently I give the feeds to the app in the viewDidLoad method of my FeedsViewController but i want to be able to retrieve the data that contains the link for the source and store it in an array to use it.
Currently the app looks like and below the current code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title =#"Lajmet";
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
//self.tabBarController.tabBar.tintColor = [UIColor blackColor];
self.allEntries = [NSMutableArray array];
self.queue = [[NSOperationQueue alloc] init];
self.feeds = [NSMutableArray arrayWithObjects:#"http://pcworld.al/feed",#"http://geek.com/feed", #"http://feeds.feedburner.com/Mobilecrunch",#"http://zeri.info/rss/rss-5.xml",
nil];
[self.tableView reloadData];
[self refresh];
}
My view that contains the function (AddSourcesViewController) to add and delete new sources looks like this and here the code:
- (void)viewDidLoad
{
[super viewDidLoad];
arrayOfSource = [[NSMutableArray alloc]init];
[[self myTableView]setDelegate:self];
[[self myTableView]setDataSource:self];
[self createOrOpenDB];
// Display sources in table view
sqlite3_stmt *statement;
if (sqlite3_open([dbPathString UTF8String], &sourceDB)==SQLITE_OK) {
[arrayOfSource removeAllObjects];
NSString *querySql = [NSString stringWithFormat:#"SELECT * FROM SOURCES"];
const char* query_sql = [querySql UTF8String];
if (sqlite3_prepare(sourceDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
NSString *name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSString *link = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
NSString *category = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
SourceDB *source = [[SourceDB alloc]init];
[source setName:name];
[source setLink:link];
[source setCategory:category];
[arrayOfSource addObject:source];
}
}
}
[[self myTableView]reloadData];
}
- (void)createOrOpenDB
{
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent:#"sources.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dbPathString]) {
const char *dbPath = [dbPathString UTF8String];
//creat db here
if (sqlite3_open(dbPath, &sourceDB)==SQLITE_OK) {
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS SOURCES (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, LINK TEXT, CATEGORY TEXT)";
sqlite3_exec(sourceDB, sql_stmt, NULL, NULL, &error);
sqlite3_close(sourceDB);
}
}
}
- (IBAction)addSourceButton:(id)sender
{
char *error;
if (sqlite3_open([dbPathString UTF8String], &sourceDB)==SQLITE_OK) {
NSString *inserStmt = [NSString stringWithFormat:#"INSERT INTO SOURCES(NAME,LINK,CATEGORY) values ('%s', '%s','%s')",[self.nameField.text UTF8String], [self.linkField.text UTF8String],[self.categoryField.text UTF8String]];
const char *insert_stmt = [inserStmt UTF8String];
if (sqlite3_exec(sourceDB, insert_stmt, NULL, NULL, &error)==SQLITE_OK) {
NSLog(#"Source added");
SourceDB *source = [[SourceDB alloc]init];
[source setName:self.nameField.text];
[source setLink:self.linkField.text];
[source setCategory:self.categoryField.text];
[arrayOfSource addObject:source];
}
sqlite3_close(sourceDB);
}
}
- (IBAction)deleteSourceButton:(id)sender
{
[[self myTableView]setEditing:!self.myTableView.editing animated:YES];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
SourceDB *s = [arrayOfSource objectAtIndex:indexPath.row];
[self deleteData:[NSString stringWithFormat:#"Delete from sources where name is '%s'", [s.name UTF8String]]];
[arrayOfSource removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(void)deleteData:(NSString *)deleteQuery
{
char *error;
if (sqlite3_exec(sourceDB, [deleteQuery UTF8String], NULL, NULL, &error)==SQLITE_OK) {
NSLog(#"Source deleted");
}
}
- (IBAction)showSourceButton:(id)sender
{
sqlite3_stmt *statement;
if (sqlite3_open([dbPathString UTF8String], &sourceDB)==SQLITE_OK) {
[arrayOfSource removeAllObjects];
NSString *querySql = [NSString stringWithFormat:#"SELECT * FROM SOURCES"];
const char* query_sql = [querySql UTF8String];
if (sqlite3_prepare(sourceDB, query_sql, -1, &statement, NULL)==SQLITE_OK) {
while (sqlite3_step(statement)==SQLITE_ROW) {
NSString *name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
NSString *link = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)];
NSString *category = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)];
SourceDB *source = [[SourceDB alloc]init];
[source setName:name];
[source setLink:link];
[source setCategory:category];
[arrayOfSource addObject:source];
}
}
}
[[self myTableView]reloadData];
}
Now i created a method in my FeedsViewConrtoller:
-(void)ReadData
{
[self createOrOpenDB];
feedsArray = [[NSMutableArray alloc] init];
const char *dbPath = [dbPathString UTF8String];
if (sqlite3_open(dbPath, &sourceDB)==SQLITE_OK)
{
const char *sqlstmt = "SELECT LINK FROM SOURCES";
sqlite3_stmt *completedstmt;
if (sqlite3_prepare_v2(sourceDB, sqlstmt, -1, &completedstmt, NULL)==SQLITE_OK)
{
while(sqlite3_step(completedstmt)==SQLITE_ROW)
{
NSString *link = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(completedstmt, 2)];
SourceDB *source = [[SourceDB alloc]init];
[source setLink:link];
[feedsArray addObject:link];
}
}
}
}
And when i change
self.feeds = [NSMutableArray arrayWithObjects:#"http://pcworld.al/feed",#"http://geek.com/feed", #"http://feeds.feedburner.com/Mobilecrunch",#"http://zeri.info/rss/rss-5.xml",
nil];
to:
self.feeds = [NSMutableArray arrayWithArray:feedsArray];
i get the following error:
2013-08-14 15:03:55.328 ShqipCom[3128:c07] (null)
What am i doing wrong here?
Thanks a lot!
Granit
In ReadData, you're returning one column, but then retrieving the third column with:
NSString *link = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(completedstmt, 2)];
That should be:
NSString *link = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(completedstmt, 0)];
A couple of other thoughts:
Part of the reason it's hard for us to answer this is that you aren't reporting SQLite errors. I'd suggest logging when a SQLite test failed, e.g.:
if (sqlite3_prepare_v2(sourceDB, sqlstmt, -1, &completedstmt, NULL)==SQLITE_OK
{
while(sqlite3_step(completedstmt)==SQLITE_ROW)
{
NSString *link = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(completedstmt, 2)];
SourceDB *source = [[SourceDB alloc]init];
[source setLink:link];
[feedsArray addObject:link];
}
}
whereas you might want something like:
if (sqlite3_prepare_v2(sourceDB, sqlstmt, -1, &completedstmt, NULL)!=SQLITE_OK
{
NSLog(#"%s: prepare error: %s", __FUNCTION__, sqlite3_errmsg(sourceDB));
return;
}
int rc;
while ((rc = sqlite3_step(completedstmt)) == SQLITE_ROW)
{
NSString *link = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(completedstmt, 2)];
SourceDB *source = [[SourceDB alloc]init];
[source setLink:link];
[feedsArray addObject:link];
}
if (rc != SQLITE_DONE)
{
NSLog(#"%s: step error: %s", __FUNCTION__, sqlite3_errmsg(sourceDB));
return;
}
You really should be checking the result of every SQLite call, and reporting an error message if not successful. Otherwise you're flying blind.
On your INSERT statement, are you 100% sure that those three fields will never have an apostrophe in them? (Also, if the user is entering any of this data, you have to be concerned about SQL injection attacks.) Generally you'd use ? placeholders in your SQL, you'd prepare the SQL with sqlite3_prepare_v2, and then use sqlite3_bind_text to bind text values to those placeholders, and then you'd sqlite3_step and confirm a return value of SQLITE_DONE.
Note, checking each and every sqlite3_xxx function call return result and doing all of the necessary sqlite3_bind_xxx calls is a little cumbersome. In your next project, if you want to streamline your SQLite code, you might want to consider using FMDB.
Your can add any object in NSMutableArray by
[myArray name addObject:mYString]; // add whatever object here;
if you want to add Array to you NSMutableArray
[myArray addObjectsFromArray:anOtherArray];

Memory leak in method

I've got a memory leak in method that reads data from db. If I do understand right, the whole evil lives in this particular string:
whole method listing:
-(NSMutableArray*)returnNominals:(int)subCountryID
{
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path =
[documentsDirectory stringByAppendingPathComponent:databaseName];
NSMutableArray *nominals=[[[NSMutableArray alloc]init]autorelease];
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
const char* sqlNominals=sqlite3_mprintf("SELECT noms.nominalID, noms.nominal,noms.nominalName,rel.nominalImg,noms.priority\
FROM nominals AS noms\
INNER JOIN NominalsAndSubCountriesRelation as rel\
ON noms.nominalID=rel.NominalID\
WHERE rel.SubcountryID=%i\
ORDER BY noms.priority",subCountryID);
sqlite3_stmt *statement;
int sqlResult = sqlite3_prepare_v2(database, sqlNominals, -1, &statement, NULL);
if ( sqlResult== SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
Nominal *nom=[[Nominal alloc]init];
nom.nominalID=sqlite3_column_int(statement, 0);
char *nominal=(char *)sqlite3_column_text(statement, 1);
char *nominalName=(char*)sqlite3_column_text(statement, 2);
char *nominalImg=(char*)sqlite3_column_text(statement, 3);
nom.nominal=(nominal)?[NSString stringWithUTF8String:nominal]: #"";
nom.nominalName=(nominalName)?[NSString stringWithUTF8String:nominalName]: #"";
nom.nominalImg=(nominalImg)?[NSString stringWithUTF8String:nominalImg]: #"noimg";
[nominals addObject:nom];
[nom release];
}
sqlite3_finalize(statement);
}
}
else
{
[self dbConnectionError];
}
return nominals;
}
And finally when viewDidLoad in another class uses this method:
.h
#interface Nominals : UIViewController
{
NSMutableArray *nominalsArr;
NSInteger subCountryID;
}
#property(nonatomic,retain)NSMutableArray *nominalsArr;
#property(nonatomic)NSInteger subCountryID;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
[[self navigationController]setToolbarHidden:YES animated:YES];
DBAccess *dbAccsess=[[DBAccess alloc]init];
self.nominalsArr=[dbAccsess returnNominals:subCountryID];
[dbAccsess closeDataBase];
[dbAccsess release];
}
- (void)dealloc
{
[tableView release];
[searchBar release];
[_toolBar release];
[nominalsArr release];
[searchController release];
[filteredItems release];
[super dealloc];
}
I've checked my code with analyzer and it says I've got no issues.
Please help me to solve this leak.
You have a leak on sqlNominals.
The sqlite3_mprintf() and sqlite3_vmprintf() routines write their results into memory obtained from sqlite3_malloc(). The strings returned by these two routines should be released by sqlite3_free().

Resources