[英]How to connect to MS-SQL server without database name using pyodbc python?
我需要連接到 ms-sql 數據庫,然后使用 python 腳本在那里創建一個新數據庫。 我有登錄的用戶憑據。 那么如何使用 python 創建到 ms-sql 服務器的連接。
如果您沒有數據庫名稱,請使用下面代碼中提到的連接字符串。 連接后創建數據庫,最終使用數據庫。
import pyodbc
# if you have user id and password then try with this connection string
connection_string = f"DRIVER={SQL Server};SERVER={server_name};UID={user_id};PWD={password}"
# if using in the local system then use the following connection string
connection_string = f"DRIVER={SQL Server};SERVER={server_name}; Trusted_Connection=True;"
connection= pyodbc.connect(connection_string)
cursor = connection.cursor()
sql_create_database = f"CREATE DATABASE {database_name}"
cursor.execute(sql_create_database)
set_database = f"USE {database_name}"
cursor.execute(set_database)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.