Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pymysql
- import gspread
- from oauth2client.service_account import ServiceAccountCredentials
- # Google Sheets API setup
- def google_sheet_setup(sheet_name, worksheet_name):
- scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/spreadsheets",
- "https://www.googleapis.com/auth/drive.file", "https://www.googleapis.com/auth/drive"]
- creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
- client = gspread.authorize(creds)
- sheet = client.open(sheet_name)
- worksheet = sheet.worksheet(worksheet_name)
- return worksheet
- # MySQL database connection
- def mysql_connection():
- connection = pymysql.connect(host='localhost',
- user='your_username',
- password='your_password',
- db='your_database_name')
- return connection
- # Fetch data from MySQL and insert into Google Sheet
- def transfer_data(worksheet, connection):
- cursor = connection.cursor()
- query = "SELECT * FROM your_table_name"
- cursor.execute(query)
- rows = cursor.fetchall()
- for i, row in enumerate(rows):
- worksheet.append_row(list(row))
- # Main function
- if __name__ == '__main__':
- worksheet = google_sheet_setup("Your Google Sheet Name", "Your Worksheet Name")
- connection = mysql_connection()
- transfer_data(worksheet, connection)
- connection.close()
Advertisement
Add Comment
Please, Sign In to add comment