Share via


快速入門:使用 Ruby 連線和查詢 適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器中的數據

適用於:適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器

重要

適用於 PostgreSQL 的 Azure 資料庫 - 單一伺服器位於淘汰路徑上。 強烈建議您升級至 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器。 如需移轉至 適用於 PostgreSQL 的 Azure 資料庫 - 彈性伺服器的詳細資訊,請參閱單一伺服器 適用於 PostgreSQL 的 Azure 資料庫 發生什麼事?

本快速入門示範如何使用 Ruby 應用程式連線到 適用於 PostgreSQL 的 Azure 資料庫。 它會顯示如何使用 SQL 陳述式來查詢、插入、更新和刪除資料庫中的資料。 本文中的步驟假設您已熟悉使用 Ruby 進行開發,而且不熟悉 適用於 PostgreSQL 的 Azure 資料庫。

必要條件

本快速入門使用在以下任一指南中建立的資源作為起點︰

您也需要安裝:

取得連線資訊

取得連線至 適用於 PostgreSQL 的 Azure 資料庫 所需的連線資訊。 您需要完整的伺服器名稱和登入認證。

  1. 登入 Azure 入口網站
  2. 從 Azure 入口網站的左側功能表中,選取 [所有資源],然後搜尋您所建立的伺服器 (例如 mydemoserver)。
  3. 選取伺服器名稱。
  4. 從伺服器的 [概觀] 面板,記下 [伺服器名稱] 和 [伺服器管理員登入名稱]。 如果您忘記密碼,您也可以從此面板重設密碼。 適用於 PostgreSQL 的 Azure 資料庫 伺服器名稱

注意

@ Azure Postgres 用戶名稱中的符號已編碼為%40所有 連接字串。

連線及建立資料表

使用下列程序代碼,使用 CREATE TABLE SQL 語句連接及建立數據表,後面接著 INSERT INTO SQL 語句,以將數據列加入數據表中。

程序代碼會使用 具有建PG::Connection構函式new的對象來連線到 適用於 PostgreSQL 的 Azure 資料庫。 然後,其會呼叫 exec() 方法來執行 DROP、CREATE TABLE 和 INSERT INTO 命令。 此程式碼會使用 PG::Error 類別來檢查錯誤。 然後,其會呼叫 close() 方法,以在終止前關閉連線。 如需這些類別和方法的詳細資訊,請參閱 Ruby Pg 參考檔。

以您自己的值取代 hostdatabaseuserpassword 字串。

require 'pg'

begin
	# Initialize connection variables.
	host = String('mydemoserver.postgres.database.azure.com')
	database = String('postgres')
    user = String('mylogin%40mydemoserver')
	password = String('<server_admin_password>')

	# Initialize connection object.
    connection = PG::Connection.new(:host => host, :user => user, :dbname => database, :port => '5432', :password => password)
    puts 'Successfully created connection to database'

    # Drop previous table of same name if one exists
    connection.exec('DROP TABLE IF EXISTS inventory;')
    puts 'Finished dropping table (if existed).'

    # Drop previous table of same name if one exists.
    connection.exec('CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);')
    puts 'Finished creating table.'

    # Insert some data into table.
    connection.exec("INSERT INTO inventory VALUES(1, 'banana', 150)")
    connection.exec("INSERT INTO inventory VALUES(2, 'orange', 154)")
    connection.exec("INSERT INTO inventory VALUES(3, 'apple', 100)")
	puts 'Inserted 3 rows of data.'

rescue PG::Error => e
    puts e.message

ensure
    connection.close if connection
end

讀取資料

使用下列程序代碼,使用 SELECT SQL 語句連接和讀取數據。

程序代碼會使用 PG::Connection 具有建構函式new的對象來連線到 適用於 PostgreSQL 的 Azure 資料庫。 然後它會呼叫 方法來 exec() 執行 SELECT 命令,並將結果保留在結果集中。 結果集集合會使用 resultSet.each do 迴圈逐一查看,並將目前的數據列值保留在變數中 row 。 此程式碼會使用 PG::Error 類別來檢查錯誤。 然後,其會呼叫 close() 方法,以在終止前關閉連線。 如需這些類別和方法的詳細資訊,請參閱 Ruby Pg 參考檔。

以您自己的值取代 hostdatabaseuserpassword 字串。

require 'pg'

begin
	# Initialize connection variables.
	host = String('mydemoserver.postgres.database.azure.com')
	database = String('postgres')
    user = String('mylogin%40mydemoserver')
	password = String('<server_admin_password>')

	# Initialize connection object.
    connection = PG::Connection.new(:host => host, :user => user, :dbname => database, :port => '5432', :password => password)
    puts 'Successfully created connection to database.'

    resultSet = connection.exec('SELECT * from inventory;')
    resultSet.each do |row|
        puts 'Data row = (%s, %s, %s)' % [row['id'], row['name'], row['quantity']]
    end

rescue PG::Error => e
    puts e.message

ensure
    connection.close if connection
end

更新資料

使用下列程序代碼,使用 UPDATE SQL語句連接及更新資料。

程序代碼會使用 具有PG::Connection建構函式new的對象來連線到 適用於 PostgreSQL 的 Azure 資料庫。 然後它會呼叫 方法來 exec() 執行UPDATE命令。 此程式碼會使用 PG::Error 類別來檢查錯誤。 然後,其會呼叫 close() 方法,以在終止前關閉連線。 如需這些類別和方法的詳細資訊,請參閱 Ruby Pg 參考檔

以您自己的值取代 hostdatabaseuserpassword 字串。

require 'pg'

begin
	# Initialize connection variables.
	host = String('mydemoserver.postgres.database.azure.com')
	database = String('postgres')
    user = String('mylogin%40mydemoserver')
	password = String('<server_admin_password>')

	# Initialize connection object.
    connection = PG::Connection.new(:host => host, :user => user, :dbname => database, :port => '5432', :password => password)
    puts 'Successfully created connection to database.'

    # Modify some data in table.
    connection.exec('UPDATE inventory SET quantity = %d WHERE name = %s;' % [200, '\'banana\''])
    puts 'Updated 1 row of data.'

rescue PG::Error => e
    puts e.message

ensure
    connection.close if connection
end

刪除資料

使用下列程序代碼,使用 DELETE SQL 語句連接和讀取數據。

程序代碼會使用 具有PG::Connection建構函式new的對象來連線到 適用於 PostgreSQL 的 Azure 資料庫。 然後它會呼叫 方法來 exec() 執行UPDATE命令。 此程式碼會使用 PG::Error 類別來檢查錯誤。 然後,其會呼叫 close() 方法,以在終止前關閉連線。

以您自己的值取代 hostdatabaseuserpassword 字串。

require 'pg'

begin
	# Initialize connection variables.
	host = String('mydemoserver.postgres.database.azure.com')
	database = String('postgres')
    user = String('mylogin%40mydemoserver')
	password = String('<server_admin_password>')

	# Initialize connection object.
    connection = PG::Connection.new(:host => host, :user => user, :dbname => database, :port => '5432', :password => password)
    puts 'Successfully created connection to database.'

    # Modify some data in table.
    connection.exec('DELETE FROM inventory WHERE name = %s;' % ['\'orange\''])
    puts 'Deleted 1 row of data.'

rescue PG::Error => e
    puts e.message

ensure
    connection.close if connection
end

清除資源

若要清除在此快速入門期間使用的所有資源,請使用下列命令刪除資源群組:

az group delete \
    --name $AZ_RESOURCE_GROUP \
    --yes

下一步