Insert data to a table in Google Cloud SQL from Google Compute Engine using python

22 views
Skip to first unread message

Roberto Catalán

unread,
Apr 11, 2019, 4:30:29 PM4/11/19
to Google Cloud SQL discuss
Hello everyone,

Reading the documentation link1 and link2 I did:

from pprint import pprint
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()
service     = discovery.build('sqladmin', 'v1beta4', credentials=credentials)

# Project ID of the project that contains the instance.
project     = 'xxx1'  # TODO: Update placeholder value.
# Database instance ID. This does not include the project ID.
instance    = 'xxx2'  # TODO: Update placeholder value.
# Database name.
database    = 'xxx3' # TODO: Update placeholder value.

#I can get the instance detail
instance    = service.instances().get(project=project, instance=instance).execute()
pprint(instance)

#I can get the detail of each user created in the Project
users       = service.users().list(project=project, instance=instance).execute()
pprint(users)

#I can get the detail of each database created in the proyect
all_databases   = service.databases().list(project=project, instance=instance).execute()
pprint(all_databases)


So the question 1 is: How can i insert data to a specific Table in the Database?

If my table named "table1" is:

id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
email VARCHAR(50)

2.- How can i insert firstname = "name1" and email ="ema...@mail.com"?

3.- Also, How can I send a query and get the result?






diogoa...@google.com

unread,
Apr 12, 2019, 11:10:43 PM4/12/19
to Google Cloud SQL discuss
You are using the Cloud SQL Admin API which is meant for administering your instances programmatically. It is not supposed to be used to save data to Cloud SQL databases.

I did not find sample code designed to save data to a specific table. However, you can start by testing the sample code in this document.

Note that Google Groups is not the best place to post this type of question. If you still have questions please open a post in Stack Overflow.

John Scott Ertel

unread,
Apr 13, 2019, 9:51:30 AM4/13/19
to google-cloud...@googlegroups.com
You may need to reference the below docs on how to connect to cloud SQL from compute. 

Once you choose an option then it's just like connecting to any other MySQL database  


--
You received this message because you are subscribed to the Google Groups "Google Cloud SQL discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-cloud-sql-d...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/c89036ea-ddb8-4a54-a2bf-80f22a202d47%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages