クイック スタート:Python を使用して Azure SQL Database に照会するQuickstart: Use Python to query an Azure SQL database
このクイック スタートでは、Python を使って Azure SQL データベースに接続した後、Transact-SQL ステートメントを使ってデータを照会する方法について説明します。This quickstart demonstrates how to use Python to connect to an Azure SQL database and use Transact-SQL statements to query data. SDK の詳細については、リファレンス ドキュメント、pyodbc GitHub リポジトリ、pyodbc サンプルを確認してください。For further SDK details, check out our reference documentation, the pyodbc GitHub repository, and a pyodbc sample.
前提条件Prerequisites
このクイック スタートを完了するには、以下のものが必要です。To complete this quickstart, make sure you have the following:
Azure SQL Database。An Azure SQL database. 以下のいずれかのクイック スタートを使用して、Azure SQL Database でデータベースを作成し、構成できます。You can use one of these quickstarts to create and then configure a database in Azure SQL Database:
単一データベースSingle database マネージド インスタンスManaged instance CreateCreate ポータルPortal ポータルPortal CLICLI CLICLI PowerShellPowerShell PowerShellPowerShell 構成Configure サーバーレベルの IP ファイアウォール規則Server-level IP firewall rule VM からの接続Connectivity from a VM オンサイトからの接続Connectivity from on-site データを読み込むLoad data クイック スタートごとに読み込まれる Adventure WorksAdventure Works loaded per quickstart Wide World Importers を復元するRestore Wide World Importers github の BACPAC ファイルから Adventure Works を復元またはインポートするRestore or import Adventure Works from BACPAC file from github 重要
この記事のスクリプトは、Adventure Works データベースを使用するように記述されています。The scripts in this article are written to use the Adventure Works database. マネージド インスタンスの場合は、Adventure Works データベースをインスタンス データベースにインポートするか、Wide World Importers データベースを使用するようにこの記事のスクリプトを修正する必要があります。With a managed instance, you must either import the Adventure Works database into an instance database or modify the scripts in this article to use the Wide World Importers database.
ご使用のオペレーティング システムに対応した Python とそれに関連するソフトウェア:Python and related software for your operating system:
MacOS:Homebrew と Python をインストールし、ODBC ドライバーと SQLCMD をインストールした後、SQL Server 用の Python ドライバーをインストールします。MacOS: Install Homebrew and Python, install the ODBC driver and SQLCMD, and then install the Python driver for SQL Server. SQL Server を使用した Python アプリの作成に関する記事の手順 1.2、1.3、および 2.1 を参照してください。See Steps 1.2, 1.3, and 2.1 in Create Python apps using SQL Server on macOS. 詳しくは、「Linux および macOS に Microsoft ODBC Driver インストールする」をご覧ください。For more information, see Install the Microsoft ODBC Driver on Linux and macOS.
Ubuntu:
sudo apt-get install python python-pip gcc g++ build-essential
を使用して、Python とその他の必要なパッケージをインストールします。Ubuntu: Install Python and other required packages withsudo apt-get install python python-pip gcc g++ build-essential
. ODBC ドライバー、SQLCMD、および SQL Server 用 Python ドライバーをダウンロードしてインストールします。Download and install the ODBC driver, SQLCMD, and the Python driver for SQL Server. 手順については、「pyodbc Python 開発用に開発環境を構成する」をご覧ください。For instructions, see Configure a development environment for pyodbc Python development.Windows:Python、ODBC ドライバー、SQLCMD、および SQL Server 用 Python ドライバーをインストールします。Windows: Install Python, the ODBC driver and SQLCMD, and the Python driver for SQL Server. 手順については、「pyodbc Python 開発用に開発環境を構成する」をご覧ください。For instructions, see Configure a development environment for pyodbc Python development.
SQL サーバーの接続情報を取得するGet SQL server connection information
Azure SQL データベースに接続するために必要な接続情報を取得します。Get the connection information you need to connect to the Azure SQL database. 後の手順で、完全修飾サーバー名またはホスト名、データベース名、およびログイン情報が必要になります。You'll need the fully qualified server name or host name, database name, and login information for the upcoming procedures.
Azure Portal にサインインします。Sign in to the Azure portal.
[SQL データベース] または [SQL マネージド インスタンス] ページに移動します。Navigate to the SQL databases or SQL managed instances page.
[概要] ページで、単一データベースの場合は [サーバー名] の横の完全修飾サーバー名を確認し、マネージド インスタンスの場合は [ホスト] の横の完全修飾サーバー名を確認します。On the Overview page, review the fully qualified server name next to Server name for a single database or the fully qualified server name next to Host for a managed instance. サーバー名またはホスト名をコピーするには、名前をポイントして [コピー] アイコンを選択します。To copy the server name or host name, hover over it and select the Copy icon.
コードを作成して、SQL データベースのクエリを実行するCreate code to query your SQL database
任意のテキスト エディターで新しいファイル (sqltest.py) を作成します。In a text editor, create a new file named sqltest.py.
次のコードを追加します。Add the following code. <server>、<database>、<username>、<password> を独自の値に置き換えます。Substitute your own values for <server>, <database>, <username>, and <password>.
重要
この例のコードでは、サンプル データ AdventureWorksLT を使用します。これは、データベースの作成時にソースとして選択できます。The code in this example uses the sample AdventureWorksLT data, which you can choose as source when creating your database. データベースに別のデータがある場合は、SELECT クエリで独自のデータベースからのテーブルを使用します。If your database has different data, use tables from your own database in the SELECT query.
import pyodbc server = '<server>.database.windows.net' database = '<database>' username = '<username>' password = '<password>' driver= '{ODBC Driver 17 for SQL Server}' cnxn = pyodbc.connect('DRIVER='+driver+';SERVER='+server+';PORT=1433;DATABASE='+database+';UID='+username+';PWD='+ password) cursor = cnxn.cursor() cursor.execute("SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName FROM [SalesLT].[ProductCategory] pc JOIN [SalesLT].[Product] p ON pc.productcategoryid = p.productcategoryid") row = cursor.fetchone() while row: print (str(row[0]) + " " + str(row[1])) row = cursor.fetchone()
コードの実行Run the code
コマンド プロンプトで、次のコマンドを実行します。At a command prompt, run the following command:
python sqltest.py
上位 20 カテゴリ/製品の行が返されることを確認し、コマンド ウィンドウを閉じます。Verify that the top 20 Category/Product rows are returned, then close the command window.
次の手順Next steps
フィードバック
お客様のご意見をお寄せください。 お寄せいただく内容の種類を選択:
このフィードバック システムは、GitHub Issues を利用して構築されています。 詳しくは、ブログをご覧ください。
フィードバックを読み込んでいます...