我正在尝试使用本地python将SQL Server
实例的数据(txt文件(从buckets
插入到Cloud SQL
。
我可以连接到bucket,但我在连接到SQL Server实例并从txt文件中插入数据时遇到问题
import os, sys
from google.cloud import storage
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'ServiceKey_GooglCloud.json'
storage_client = storage.Client()
mybucket= storage_client.get_bucket('my-bucket')
你知道python中在SQL Server实例的表中插入数据的方法吗?
您可以使用Cloud SQL Python connector
:
Github Cloud SQL Python连接器
PyPi Cloud SQL Python连接器
根据文档,Cloud SQL Python连接器是一个与数据库驱动程序一起使用的包。目前支持的驱动程序有:
pymysql (MySQL)
pg8000 (PostgreSQL)
asyncpg (PostgreSQL)
pytds (SQL Server)
Python
包可以安装在具有pip:的SQL server
上
pip install "cloud-sql-python-connector[pytds]"
示例
# insert statement
insert_stmt = sqlalchemy.text(
"INSERT INTO my_table (id, title) VALUES (:id, :title)",
)
with pool.connect() as db_conn:
# insert into database
db_conn.execute(insert_stmt, id="book1", title="Book One")
# query database
result = db_conn.execute("SELECT * from my_table").fetchall()
# Do something with the results
for row in result:
print(row)