Friday, April 12, 2019

Re: [google-cloud-sql-discuss] Insert data to a table in Google Cloud SQL from Google Compute Engine using python

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  


On Thu, Apr 11, 2019, 3:30 PM Roberto Catalán <roberto@juus.to> wrote:
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 ="email1@mail.com"?

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






--
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-discuss+unsubscribe@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.

--
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-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-cloud-sql-discuss/CAOTThPnhBPADiwNvedEgbob3RVErOW1hH7uVtSmT6zuFdwYpaA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment