Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert, delete, and update in google spreadsheet: issue#5 #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

JagritiG
Copy link

@JagritiG JagritiG commented Jan 16, 2021

Hi, I am using your package for my client. They also want to have INSERT/DELETE/UPDATE support (issue #5). I have made some changes and add some functions to your codebase and tested them in my system using PyCharm. The modified version is working in my system correctly. The files I have modified are auth.py, url.py, db.py, sqlite.py.

Required packages:

  1. sqlparse (version 0.4.1)
  2. google-api-python-client (version 1.12.8)

How it works:

  1. For 'SELECT', it executes the code as you have implemented.

  2. For 'INSERT', it dumps Google spreadsheet data into SQLite memory and inserts new data, then append updated data from SQLite memory to the same spreadsheet.

  3. For 'DELETE' and 'UPDATE', it dumps the spreadsheet data into SQLite memory, performs 'query' in SQLite memory, then creates a new worksheet in google spreadsheet and upload updated data into this new worksheet keeping the original worksheet unmodified. Some examples are given below.

  4. INSERT:

from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
from sqlalchemy import Table

# Connection and authentication
service_account_file = "service_account_file.json"    # your service account credential file
engine = create_engine('gsheets://', service_account_file=service_account_file, subject='your_business_domain@xx.com')
inspector = inspect(engine)
url = "google_spread_sheet_url"

# Processing
table = Table(
    url,
    MetaData(bind=engine),
    autoload=True)

# insert into google spreadsheet

# Method: 1
# engine.execute(table.insert(), column1=value1, column2=value2, ...)
# Example:
engine.execute(table.insert(), country='USA', cnt=3000)

# Method: 2
# stmt = (
#     insert(table).
#     values(column1=value1, column2=value2)
# )
# engine.execute(stmt)
  1. UPDATE:
from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
from sqlalchemy import Table

# Connection and authentication
service_account_file = "service_account_file.json"    # your service account credential file
engine = create_engine('gsheets://', service_account_file=service_account_file, subject='your_business_domain@xx.com')
inspector = inspect(engine)
url = "google_spread_sheet_url"

# Processing
table = Table(
    url,
    MetaData(bind=engine),
    autoload=True)

# Update in Google spreadsheet
# method-1
engine.execute(table.update().where(table.c.country == "USA").values(cnt=55))
# engine.execute(table.update().where(table.c.country == "IND").values(country='DEN'))
# engine.execute(table.update().where(table.c.country == "DEN").values(cnt=1500, quantity=2500))
# engine.execute(table.update().where(table.c.country == "GER").values(country='IND', cnt=8000, quantity=85))
  1. DELETE:
from sqlalchemy import *
from sqlalchemy.engine import create_engine
from sqlalchemy.schema import *
from sqlalchemy import Table

# Connection and authentication
service_account_file = "service_account_file.json"    # your service account credential file
engine = create_engine('gsheets://', service_account_file=service_account_file, subject='your_business_domain@xx.com')
inspector = inspect(engine)
url = "google_spread_sheet_url"

# Processing
table = Table(
    url,
    MetaData(bind=engine),
    autoload=True)

# delete from Google spreadsheet
# Method: 1
# ex:1:
engine.execute(table.delete().where(table.c.cnt < 3000))

# ex2:
# engine.execute(table.delete().where(table.c.country == "DEN"))

# Method: 2
# ex1:
# stmt = (
#     delete(table).
#     where(table.c.country == 'GER')
# )
# engine.execute(stmt)

# ex2:
# stmt = (
#     delete(table).
#     where(table.c.cnt >= 400)
# )
# engine.execute(stmt)

I would appreciate it if you have time to review this.
Thanks,
JG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant