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

  • Home
  • SEARCH
  • 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 5929175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:23:16+00:00 2026-05-22T14:23:16+00:00

I’m getting data from a mysql database using the C api. I can log

  • 0

I’m getting data from a mysql database using the C api. I can log the data so I know the query is working fine but I need to take it and put it into a dictionary and The Google is not being helpful with that. Can anyone give me a pointer, snippet or link to get me going in the right direction?

- (IBAction)dbConnect:(id)sender {


NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MYSQL mysql;
mysql_init(&mysql);

if (!mysql_real_connect(&mysql, "10.1.1.99", "*******", "********", "oldphotoarchive", 0, NULL, 0)) {
    NSLog(@"%@", [NSString stringWithUTF8String:mysql_error(&mysql)]);
} else { 

    MYSQL_RES *result;
    MYSQL_ROW row;
    unsigned int num_fields;
    unsigned int num_rows;
    unsigned long *lengths;

    if (mysql_query(&mysql,"SELECT * FROM photorecord")) {
        // error
    } else { // query succeeded, process any data returned by it  

        result = mysql_store_result(&mysql);
        if (result)  {
            num_fields = mysql_num_fields(result);
            while ((row = mysql_fetch_row(result))) {
                lengths = mysql_fetch_lengths(result);

                lengths = mysql_fetch_lengths(result);
                for(int i = 0; i < num_fields; i++) {
                    printf("[%.*s] ", (int) lengths[i],  row[i] ? row[i] : "NULL");

                    if (row[i]) { 
                                                    //AT THIS POINT I WANT TO DO THE CONVERSION, THIS ISN'T WORKING, INCOMPATIBLE POINTER TYPE
                        NSArray* thisRow = [[NSArray alloc] initWithString: row[i]];
                    }
                }
                printf("\n");

            }


        } else  {// mysql_store_result() returned nothing; should it have?
            if (mysql_errno(&mysql)) {
                NSLog(@ "Error: %s\n", mysql_error(&mysql));
            } else if (mysql_field_count(&mysql) == 0) {
                // query does not return data
                // (it was not a SELECT)
                num_rows = mysql_affected_rows(&mysql);
            }
        }

    }

}

[pool release];
}

Here’s the revised code in case other folks are running into the same wall. Feel free to critique:

#import "AppController.h"

#include "mysql.h"
#include "unistd.h"


@implementation AppController
- (IBAction)dbConnect:(id)sender {


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

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
MYSQL mysql;
mysql_init(&mysql);

if (!mysql_real_connect(&mysql, "10.1.1.99", "mysqladmin", "m3r!0n", "oldphotoarchive", 0, NULL, 0)) {
    NSLog(@"%@", [NSString stringWithUTF8String:mysql_error(&mysql)]);
} else { 

    MYSQL_RES *result;
    MYSQL_ROW row;
    unsigned int num_fields;
    unsigned int num_rows;
    //unsigned long *lengths;

    NSString* itemID = [[NSString alloc] init];

    if (mysql_query(&mysql,"SELECT * FROM photorecord WHERE logNum > 10000")) {
        // error
    } else { // query succeeded, process any data returned by it  

        result = mysql_store_result(&mysql);
        if (result)  {
            num_fields = mysql_num_fields(result);

            while ((row = mysql_fetch_row(result))) {

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

                for(int i = 0; i < num_fields; i++) {
                    if (row[i]) { 
                        char* cString = row[i];
                        NSString* dataString  = [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
                        if ([dataString isNotEqualTo: @"NULL"]) { 
                            //NSLog(@"%@", dataString);

                            if (i==0) { 
                                itemID = dataString;
                            }

                            [tmpDict setObject: dataString forKey: [NSString stringWithFormat: @"item_%i", i] ];
                        }
                    }
                }

                [results setObject:tmpDict forKey:itemID];
                NSLog(@"%@", results);

            }

        } else  {// mysql_store_result() returned nothing; should it have?
            if (mysql_errno(&mysql)) {
                NSLog(@ "Error: %s\n", mysql_error(&mysql));
            } else if (mysql_field_count(&mysql) == 0) {
                // query does not return data
                // (it was not a SELECT)
                num_rows = mysql_affected_rows(&mysql);
            }
        }

    }

}


[pool release];

}
@end

You have to do a small bit of set up in order to get MySQL to work in your project:

  1. Copy the two framework files libmysqlclient.a and libmysqlclient_r.a (should be in /usr/local/mysql/lib) to your project (place them in the frameworks group/folder). Include all sub folders or whatever that message is when you copy stuff in to a project.

  2. Expand Targets, right click and then select Add->Add New Build Phase->New Copy Files Build Phase. Change destination to frameworks. This puts the two frameworks into the build of your project.

  3. Copy the /usr/local/mysql/include folder to your project. These are the MySQL header files.

  • 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-22T14:23:16+00:00Added an answer on May 22, 2026 at 2:23 pm

    NSArray doesn’t have a ‘initWithString’ method. .

    Probably what you want is something like this…

    NSString *s = [NSString alloc] initWithBytes: row[i] length: strlen(row[i]) encoding: DefaultCstring]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want to construct a data frame in an Rcpp function, but when I
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.