Image is not fetching from database

巧了我就是萌 提交于 2019-12-02 07:16:47

Now that you've included your JSON sample, I can see another problem: Your JSON is an array of three arrays, an array of categories, an array of products, and an array of versions. That is a questionable structure (usually an array would be of homogeneous items of the same type). Rather than an array of arrays, I'd make it a dictionary of arrays.

This confusing structure in your JSON manifests itself in your Objective-C code. You're trying to iterate through the userdata array, but you can't, because each of the three item is different.

So let me suggest two possible fixes. First you if you stick with your array of arrays (which I'm not crazy about), you cannot iterate through that array with a for loop, but you could extract the three arrays it contains like so:

NSArray *categories = userdata[0];
NSArray *products = userdata[1];
NSArray *versions = userdata[2];

Now you can iterate through each of those three arrays.

Personally, I'd take this a step further and change that PHP that generates the JSON to generate a top level dictionary, e.g.

<?php 
    require_once('database_connection.php');
    $i = 0;
    $j = 0;
    $k = 0;
    $l = 0;
    mysql_query('SET CHARACTER SET utf8') or die("MYSQL character set error: ".mysql_error());
    $result = array();
        $sql=mysql_query("SELECT * FROM categories ORDER BY id ASC") or die(mysql_error());
        if(mysql_num_rows($sql) > 0) {
            while($res=mysql_fetch_array($sql, MYSQL_ASSOC)){
                $result['categories'][$i] = $res;

                $art_sql=mysql_query("SELECT * FROM product WHERE cat_id=" .$res['id']. " ORDER BY id ASC") or die(mysql_error());
                if (mysql_num_rows($art_sql) > 0){
                    while($art_res=mysql_fetch_array($art_sql, MYSQL_ASSOC)){
                        $result['products'][$k] = $art_res;
                        $k = $k+1;  
                    }   
                }
                $i= $i+1;
            }
            $version_sql = mysql_query("SELECT * FROM version_app order by product_id desc limit 1") or die(mysql_error());
            $row = mysql_fetch_array($version_sql);
            $last_version = $row['product_id'];
            $result['versions'][$l] = array('product_id' => $last_version);
            $l = $l+1;
        }

    $str_enc = json_encode($result);

    // note, these str_replace lines are not needed

    // $str=str_replace('\r','',$str_enc);
    // $str=str_replace('\t','',$str);
    // $str=str_replace('\n','',$str);

    // this stripslashes is a really bad idea, though

    // $str = stripslashes($str);

    // you echoed `$str`, but I'll obviously echo @str_enc    
    echo $str_enc;

    mysql_close();
?>

And if you did that, you could then retrieve your three arrays with

NSArray *categories = userdata[@"categories"];
NSArray *products = userdata[@"products"];
NSArray *versions = userdata[@"versions"];

It's not a mission critical change, but it's a more logical representation for three heterogeneous items. But the idea is the same: Extract your three arrays from your JSON, and now you can iterate through them, respectively.


A couple of SQLite-related issues leap out at me

  1. One issue is that you have a line of SQL that says:

    SELECT id FROM categories where cat_id = '%@'
    

    But you then proceed to try to read four columns worth of data (even though you only returned one). And even if you changed your SQL to actually return four columns of data, you should really check the sqlite3_column_blob and sqlite3_column_bytes calls to make sure there was something to populate the NSData.

    As an aside, it's generally safer to not use stringWithFormat when building a SQL statement, but rather use ? placeholders instead of printf-formatters and then use sqlite3_bind_xxx functions.

  2. You getMysofas is repeatedly opening the database, but never closing it. Have one sqlite3_close for every sqlite3_open call. Likewise, you should have one sqlite3_finalize statement for each sqlite3_prepare_v2 line.


Beyond that you have to identify the source of the crash by either using an exception breakpoint, single-stepping through this code in the debugger, or putting a bunch of NSLog statements in there. See Ray Wenderlich's series My App Crashed, Now What?. But you really need to identify the line of code that is causing the problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!