开发者

Getting error while trying to fetch and insert data into SQLite database

I am creating an application where I am using SQLite database to save data. But when I run my application I get the following errors:

@interface TDatabase : NSObject {
    sqlite3 *database;
}

+(TDatabase *) shareDataBase;

-(BOOL) createDataBase:(NSString *)DataBaseName;

-(NSString*) GetDatabasePath:(NSString *)database;

-(NSMutableArray *) getAllDataForQuery:(NSString *)sql forDatabase:(NSString *)database;
-(void*) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1; 

@end

#import "TDatabase.h"
#import <sqlite3.h>

@implementation TDatabase

static TDatabase *SampleDataBase =nil;

+(TDatabase*) shareDataBase{

    if(!SampleDataBase){
        SampleDataBase = [[TDatabase alloc] init];
    }

    return SampleDataBase;
}

-(NSString *)GetDatabasePath:(NSString *)database1{

    [self createDataBase:database1];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:database1];

}

-(BOOL) createDataBase:(NSString *)DataBaseName{ 

    BOOL success;

    NSFileManager *fileManager = [NSFileManager defaultManager];
开发者_运维问答    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DataBaseName];

    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) return success;
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DataBaseName];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];

    if (!success) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!!!" message:@"Failed to create writable database" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];

    }
    return success;
}


-(NSMutableArray *) getAllDataForQuery:(NSString *)sql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    NSMutableArray *alldata;
    alldata = [[NSMutableArray alloc] init];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        NSString *query = sql;

        if((sqlite3_prepare_v2(database,[query UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            while(sqlite3_step(statement) == SQLITE_ROW)
            {   

                NSMutableDictionary *currentRow = [[NSMutableDictionary alloc] init];

                int count = sqlite3_column_count(statement);

                for (int i=0; i < count; i++) {

                    char *name = (char*) sqlite3_column_name(statement, i);
                    char *data = (char*) sqlite3_column_text(statement, i);

                    NSString *columnData;
                    NSString *columnName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];


                    if(data != nil)
                        columnData = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];
                    else {
                        columnData = @"";
                    }

                    [currentRow setObject:columnData forKey:columnName];
                }

                [alldata addObject:currentRow];
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

    return alldata;

}


-(void*) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1{

    sqlite3_stmt *statement = nil ;

    NSString *path = [self GetDatabasePath:database1];

    if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
    {
        if((sqlite3_prepare_v2(database,[insertSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
        {
            if(sqlite3_step(statement) == SQLITE_OK){
            }
        }
        sqlite3_finalize(statement); 
    }
    sqlite3_close(database);

    return insertSql;
}



NSString *sql = @"select * from Location"; 
const location = [[TDatabase shareDataBase] getAllDataForQuery:sql forDatabase:@"journeydatabase.sqlite"];//1

NSString* insertSql = [NSString stringWithFormat:@"insert into Location values ('city','name','phone')"];//2
const insert =[[TDatabase shareDataBase] inseryQuery:insertSql forDatabase:@"journeydatabase.sqlite"];//3

in line no 1,2,3 I get the same error:

initializer element is not constant

What might be the problem?


@rani writing your own methods to deal with sqlite database is very painstaking. You should use fmdb wrapper class or use core data. I personally prefer fmdb. Initially I was doing the same way you were. I found about fmdb here. After using it I had to write very little code whenever I have to deal With sqlite db.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜