Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:57:12+00:00 2026-05-29T04:57:12+00:00

I want to save some data into sqlite database and the read it, there

  • 0

I want to save some data into sqlite database and the read it, there is no problem while saving the data but i am not able to retrieve data from database.

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);
    }
}

Please provide the solution.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:57:15+00:00Added an answer on May 29, 2026 at 4:57 am

    Create the database at runtime, and use the below logic,

    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);
    

    To read data from sqlite

         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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to save some XML formatted data into the Application Support folder, but
I want to save some custom data into application configuration file and I need
I want to read and save some data from this rss feed to in
I am a beginner in cakePHP , i want to save some data into
In my application I want to save some string data into an array. This
I want to provide the ability to save some rendered data/charts (with the titles
i've got some binary data which i want to save as an image. When
In a java serialization problem, I want to save some classes name and I
So I'm trying to import some sales data into my MySQL database. The data
I am new to C, and want to read some data from a file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.