Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将swift连接到sql server_Ios_Swift - Fatal编程技术网

Ios 将swift连接到sql server

Ios 将swift连接到sql server,ios,swift,Ios,Swift,Swift 3/Xcode 8.3.3 我有一个运行在phpmyadmin上的SQL server,我想直接从我的Swift代码发送一个SQL请求(比如:SELECT*FROM database)。我做了很多研究,但所有的答案都使用PHP 最后,我想将此Java代码转换为Swift: try (Connection connection = DriverManager.getConnection(url, username, password)) { System.out.println

Swift 3/Xcode 8.3.3

我有一个运行在phpmyadmin上的SQL server,我想直接从我的Swift代码发送一个SQL请求(比如:SELECT*FROM database)。我做了很多研究,但所有的答案都使用PHP

最后,我想将此Java代码转换为Swift:

try (Connection connection = DriverManager.getConnection(url, username, password)) {
    System.out.println("Database connected!");

    // the mysql insert statement
    String query = "SELECT * FROM dbName";

    Statement st = connection.createStatement();
    st.executeUpdate(query);

} catch (SQLException e) {
    throw new IllegalStateException("Cannot connect the database!", e);
}

首先,必须编写一个web服务(PHP、JAVA)来查询数据库

创建一个php代码来处理ios查询

    <?php

// Create connection
$con=mysqli_connect("localhost","username","password","dbname");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}



// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Locations";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();

    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}

// Close connections
mysqli_close($con);
?>

虽然您可以使用RESTAPI从任何服务器获取数据,但是如果您的项目需要直接从swift项目连接sql server,那么您可以使用“SQLClient”进行连接。您将从这里得到解决方案-

不,您不能这样做。在服务器上使用PHP或Java时,客户端/您的iOS应用程序决不能知道数据库凭据。@anderskev您的回答可能是错误的,因为该库似乎只支持macOS!=你想把Java代码翻译成Swift,还是想知道如何用Swift编写SQL查询?您的问题不清楚。Let us.task.resume()丢失,因此该任务将永远无法执行。您从何处复制了第一个代码块?例如,它与2014年两个不同问题中使用的代码块相同。和
    @IBAction func sendQuery(_ sender: Any) {


        let url = URL(string: "https://wanagetout1.000webhostapp.com/db_copy.php")!

        var request = URLRequest(url: url)

        request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
        request.httpMethod = "POST"
        let postString = "your query"
        request.httpBody = postString.data(using: .utf8)
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {
                // check for fundamental networking error
                print(error!)
                return
            }
}