python - 通过 Python 连接 Google Cloud MySQL;如何访问表?
问题描述
遵循本教程后,我可以运行一个脚本来打印我的数据库的所有详细信息。但是,我不知道如何处理所述数据库!这是我的代码:
from google.oauth2 import service_account
import googleapiclient.discovery
import json
SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin']
SERVICE_ACCOUNT_FILE = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
sqladmin = googleapiclient.discovery.build('sqladmin', 'v1beta4', credentials=credentials)
response = sqladmin.instances().list(project='single-router-309308').execute()
print(json.dumps(
response, sort_keys=True, indent=2))
sqladmin.close()
打印所有信息。我尝试了各种方法来到达我的桌子,products
但我无法让它工作并不断收到AttributeError: 'Resource' object has no attribute 'execute'
(或'list'
)异常。我试过这样的东西:
response = sqladmin.projects().list().execute()
也可以查看我的表格,但它不起作用。我相信这是正确的方法,因为我可以连接,但我还没有弄清楚。有人知道答案吗?
解决方案
根据文档,您应该能够使用以下代码访问您的表格。
请注意,您有一个 sql 项目,然后是项目上的一个实例,然后是实例中的一个数据库,然后您的表嵌套在该数据库中。
from pprint import pprint
# Project ID of the project that contains the instance.
project = 'single-router-309308'
# Database instance ID. You should have this from the above printout
instance = 'my-instance'
# Name of the database in the instance. You can look this up if you arent sure by logging into google cloud for your project. Your table is inside this database.
database = 'my-database'
request = service.databases().get(project=project, instance=instance, database=database)
response = request.execute() #returns a dictionary with the data
pprint(response)
我建议您查看 CLoud SQL for MySQL 的 REST API 参考资料,以便进一步阅读 ( https://cloud.google.com/sql/docs/mysql/admin-api/rest/v1beta4/databases/get )。
推荐阅读
- c# - 如何转换列表
到 ISet - pyspark - 从 pyspark 中的数据框数组类型列中获取“名称”元素
- reactjs - 视频全屏显示时位置不起作用
- c# - 按下按钮单击将 .exe 文件放在另一台计算机上后程序崩溃
- coq - 逻辑基础错误
- javascript - React 输入类型更改在 Firefox 中触发模糊事件
- linux - Laravel - Linux(ubuntu)实时进程并在网页上显示输出(刀片)
- mysql - 在 SQL 中搜索后更新找到的行中的列值
- javascript - 如何根据 Angular 6 中 ng-template 中的 id 引用元素?
- android - 当我将 ScrollView 添加到 ConstraintLayout 时应用程序崩溃