English 中文(简体)
无法从欧斯数据库检索数据
原标题:Not able to retrieve data from database in ios
  • 时间:2012-01-12 06:07:30
  •  标签:
  • iso

我想将一些数据节省到qlite数据库,并读到该数据库,在节省数据时没有问题,但我无法从数据库中检索数据。

Here is the code that I m using.

//>>>>>> to save in sqlite Database

-(void)saveData
{
    NSString *strTxtFldValue = txtFldName.text;

    sqlite3_stmt *statement;
    const char *dbpath = [appDel.databasePath UTF8String];

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSString *insertSQL = [NSString stringWithFormat:@"INSERT INTO TESTTABLE (NAME) VALUES ("%@")",strTxtFldValue];

        const char *insert_stmt = [insertSQL UTF8String];
        sqlite3_prepare_v2(database, insert_stmt, 
                           -1, &statement, NULL);
        if (sqlite3_step(statement) == SQLITE_DONE)
        {
            NSLog(@"Data is added succesfully ");

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"Name is added successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
        } else {

            NSLog(@"Failed to add data");
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }
}

//>>>>>> to read from database 

//here the control comes till second NSLog and the "sqlite3_prepare_v2" statement doesn t get execute.

-(void)readData{

    const char *dbpath = [appDel.databasePath UTF8String];
    sqlite3_stmt *statement;
    NSLog(@"^^^^^^^ 1");

    if (sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        NSLog(@"^^^^^^^ 2");

       // NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM TESTTABLE"];
        NSString *querySQL = [NSString stringWithFormat:@"SELECT NAME FROM TESTTABLE"];

    const char *query_stmt = [querySQL UTF8String];

        **if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)**
        {    
            NSLog(@"^^^^^^^ 3");            
            while(sqlite3_step(statement) == SQLITE_ROW)
            {
        NSLog(@"^^^^^^^ 4");

        NSString *cName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
                NSLog(@"~~~~### cName = %@",cName);

                Names *name = [[Names alloc] initWithName:cName];
                [names addObject:name];
                [name release];             
            }

            sqlite3_finalize(statement);
        }
        sqlite3_close(database);
    }
}

请提供解决办法。

Thanks in advance

最佳回答

1. 建立数据库,使用以下逻辑:

to write the data in sqlite.

    sqlite3 *pDb; 
char *databaseName; 

databaseName = @"TempDatabase.sql";

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
//databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

databasePath =[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TempDatabase.sql"];

//[self copyDatabaseIfNeeded];


if(sqlite3_open([databasePath UTF8String], &pDb) == SQLITE_OK) 
{

    const char *sqlStatement = "INSERT INTO tablename (name) VALUES(?)";

    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(pDb, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) 
    {


         sqlite3_bind_text( compiledStatement, 1, [strTxtFldValue UTF8String], -1, SQLITE_TRANSIENT);
    }
    if(sqlite3_step(compiledStatement) != SQLITE_DONE ) 
    {
         NSLog( @"~~~~~~~~~~~ 1 Error: %s", sqlite3_errmsg(pDb) );
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Name name is not added." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

    } else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Confirmation" message:@"name is added successfully." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];

            }
    sqlite3_finalize(compiledStatement);
}

sqlite3_close(pDb);

将数据改为qlite

     sqlite3 *database;
const char *dbpath = [appDel.databasePath UTF8String];
sqlite3_stmt *statement;


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

if (sqlite3_open(dbpath, &database) == SQLITE_OK) 
{


    const char *sqlStatement = "select name from tablename";


    if (sqlite3_prepare_v2(database, sqlStatement, -1, &statement, NULL) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) 
        {
            NSString *cName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)];
            //NSLog(@"cName = %@",cName);

            Names *name = [[Names alloc] initWithName:cName];
            [names addObject:name];
            [name release];
        }

    }
    sqlite3_finalize(statement);

}
sqlite3_close(database);

Try out this and vote me if it works fine for you.

问题回答

暂无回答




相关问题
ISO/IEC Website and Charging for C and C++ Standards

The ISO C Standard (ISO/IEC 9899) and the ISO C++ Standard (ISO/IEC 14882) are not published online; instead, one must purchase the PDF for each of those standards. I am wondering what the rationale ...

Maven plugin for generating ISO file

Is there a maven plugin capable of generating ISO images? I need to take the output of some modules (mostly zip files containing jars) and combine them into a single ISO image. Thanks

What alternatives to __attribute__ exist on 64-bit kernels?

Is there any alternative to non-ISO gcc specific extension __attribute__ on 64-bit kernels ? Three types that i ve noticed are: function attributes, type attributes and variable attributes. eg. i d ...

Full statement from ISO 8583

I would like to know if it is possible to do a full statement (between a date range) through ISO 8583, I have seen ATMs which do full statements and was wondering what method they used. I know ...

python write CD/DVD iso file

I m making a cross-platform (Windows and OS X) with wxPython that will be compiled to exe later. Is it possible for me to create ISO files for CDs or DVDs in Python to burn a data disc with? Thanks, ...

How do ISO standards work (particularly ISO 9126) [closed]

I m taking a course in software project management and right now I m writting a "quality plan"... We ve discussed ISOs in class, and I would particularly like my fake company to say the project ...

热门标签