带有关系数据库的iOS应用程序的后端

时间:2012-03-29 11:23:28

标签: ios relational-database backend

我目前正在尝试构建一个需要能够在远程关系数据库中检索和存储数据的ios应用程序。我已经调查过使用RestKit作为前端,这似乎已经足够了。我有使用mysql和php开发的经验,虽然我不知道从哪里开始获取Web服务器来发送不是html的信息。 (php增强的html,即)

我很感激我可以开始的任何输入。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我使用PHP查询服务器上的mySQL数据库,并将响应格式化为XML,然后在应用程序中解析。 这是我的服务器的一个PHP示例....这也发送一个大小响应,允许App跟踪下载完成百分比和文件名响应。此外,如果数据库为空,它将发送虚拟记录。

<?php
    header("Content-type: text/text"); 

//get the values from the url query string
$user = $_GET['user'];
$pass = $_GET['password'];
$db = $_GET['database'];
$dbhost = 'localhost';

//create connection to mysql or tell user something has gone wrong
$connection = mysql_connect( $dbhost, $user, $pass ) or die('Could not connect to MySQL');
//select database or tell user something has gone wrong
mysql_select_db( $db ) or die('Could not select database');


// Construct and run query.
    $sql = 'SELECT * FROM itemInfo WHERE itemStatus = "active" ORDER BY itemSortKey ASC';
    //echo $sql;
    $result = mysql_query($sql) or die('<result>fail</result>');

    $xml_output = "<?xml version=\"1.0\"?>\n"; 
    $xml_output .= "<bodyimage>\n"; 


    while($record = mysql_fetch_assoc($result)) {
        $xml_output .= "\t<storeitems>\n"; 
        $xml_output .= "\t\t<itemSortKey>" . $record['itemSortKey'] . "</itemSortKey>\n"; 
        $xml_output .= "\t\t<itemDesc>" . $record['itemDesc'] . "</itemDesc>\n"; 
        $xml_output .= "\t\t<itemPrice>" . $record['itemPrice'] . "</itemPrice>\n"; 
        $xml_output .= "\t\t<productID>" . $record['productID'] . "</productID>\n"; 
        $xml_output .= "\t\t<field1>" . $record['field1'] . "</field1>\n"; 
        $xml_output .= "\t\t<field2>" . $record['field2'] . "</field2>\n"; 
        $xml_output .= "\t\t<field3>" . $record['field3'] . "</field3>\n"; 
        $xml_output .= "\t\t<field4>" . $record['field4'] . "</field4>\n"; 
        $xml_output .= "\t\t<dateCreated>" . $record['dateCreated'] . "</dateCreated>\n"; 
        $xml_output .= "\t\t<dateModified>" . $record['dateModified'] . "</dateModified>\n"; 

    // Escaping illegal characters 
    //$row['text'] = str_replace("&", "&", $row['text']); 
    //$row['text'] = str_replace("<", "<", $row['text']); 
    //$row['text'] = str_replace(">", "&gt;", $row['text']); 
    //$row['text'] = str_replace("\"", "&quot;", $row['text']); 
    //$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n"; 

        $xml_output .= "\t</storeitems>\n"; 

        //add this record to the output
        //$output = '|facilityname|' .$record['facilityname'] . '|/facilityname||customername|' . $record['customername'] . '|/customername||imageurl|' . $record['imageurl'] . '|/imageurl|';
        //print $output;
    }
    $xml_output .= "</bodyimage>";

    header("HTTP/1.0 200 OK");
    header('Content-type: text/xml');
    header('Content-Disposition: attachment; filename="storeitems.xml"');
    header("Content-Length: ".strlen ( $xml_output )); 

    // redundent.....$xml_output .= "</bmrcontrol>";

    //echo strlen( $xml_output );
    //the next statement relates if there are NO lines in the table - it sends a message
    if ( strlen( $xml_output ) == 61 ) {
        $xml_output = "<?xml version=\"1.0\"?>\n"; 
        $xml_output .= "<bodyimage>\n"; 

        $xml_output .= "\t<storeitems>\n";
        $xml_output .= "\t\t<itemSortKey>aaaa</itemSortKey>\n"; 
        $xml_output .= "\t\t<itemDesc>Store Items Missing</itemDesc>\n"; 
        $xml_output .= "\t\t<itemPrice>0.0</itemPrice>\n"; 
        $xml_output .= "\t\t<productID>blank</productID>\n"; 
        $xml_output .= "\t\t<field1>.</field1>\n"; 
        $xml_output .= "\t\t<field2>.</field2>\n"; 
        $xml_output .= "\t\t<field3>.</field3>\n"; 
        $xml_output .= "\t\t<field4>.</field4>\n"; 
        $xml_output .= "\t\t<dateCreated>2012-02-13 09:03:49</dateCreated>\n"; 
        $xml_output .= "\t\t<dateModified>2012-02-13 09:03:49</dateModified>\n"; 

        // Escaping illegal characters 
        //$row['text'] = str_replace("&", "&", $row['text']); 
        //$row['text'] = str_replace("<", "<", $row['text']); 
        //$row['text'] = str_replace(">", "&gt;", $row['text']); 
        //$row['text'] = str_replace("\"", "&quot;", $row['text']); 
        //$xml_output .= "\t\t<text>" . $row['text'] . "</text>\n"; 

        $xml_output .= "\t</storeitems>\n"; 
        $xml_output .= "</bodyimage>";
    }

    echo $xml_output; 

    if(mysql_num_rows($result)) {
        //mysql_close($connection);
    }
    else
      die('<result>fail</result>');
      //mysql_close($connection);
?>