Python Forum
MySQL SSL Connection, Need help
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MySQL SSL Connection, Need help
#2
I have found out that xlrd is for older excel docs so Im now using openpyxl

Im still getting errors though: AttributeError: 'Worksheet' object has no attribute 'nrows'

#import xlrd
import openpyxl
import MySQLdb

# Open the workbook and define the worksheet
wb = openpyxl.load_workbook("MINFOS.DailyTTL.xlsx")
ws = wb.get_sheet_by_name("PivotEx")

# Establish a MySQL connection
ssl = {"cert":"/home/xxxx/certs/client-cert.pem", "key":"/home/xxxx/certs/client-key.pem", "ca":"/home/xxxx/certs/server-ca.pem"}
database = MySQLdb.connect(host="xxxx", user="root", passwd="xxxx", db="manila_data", ssl=ssl)

# Get the cursor, which is used to traverse the database, line by line
cursor = database.cursor()

# Create the INSERT INTO sql query
query = """INSERT INTO test (StoreID, Date, StoreName, CreateDate, Category, Scripts, Gross, Gst, CoPayDiscount, Net, CustomerPayments, GovernmentPayments, PremiumFreeIncentive, IncentiveOnline, EPF, rdPartyPayments, COGS, Gpdollar, Gppercent) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""

# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
for r in range(1, ws.nrows):
    StoreID = ws.cell(r, 0).value
    Date = ws.cell(r, 1).value
    StoreName = ws.cell(r, 2).value
    CreateDate = ws.cell(r, 3).value
    Category = ws.cell(r, 4).value
    Scripts = ws.cell(r, 5).value
    Gross = ws.cell(r, 6).value
    Gst = ws.cell(r, 7).value
    CoPayDiscount = ws.cell(r, 8).value
    Net = ws.cell(r, 9).value
    CustomerPayments = ws.cell(r, 10).value
    GovernmentPayments = ws.cell(r, 11).value
    PremiumFreeIncentive = ws.cell(r, 12).value
    IncentiveOnline = ws.cell(r, 13).value
    EPF = ws.cell(r, 14).value
    rdPartyPayments = ws.cell(r, 15).value
    COGS = ws.cell(r, 16).value
    Gpdollar = ws.cell(r, 17).value
    Gppercent = ws.cell(r, 18).value

    # Assign values from each row
    values = (StoreID, Date, StoreName, CreateDate, Category, Scripts, Gross, Gst, CoPayDiscount, Net, CustomerPayments, GovernmentPayments, PremiumFreeIncentive, IncentiveOnline, EPF, rdPartyPayments, COGS, Gpdollar, Gppercent)

    # Execute sql Query
    cursor.execute(query, values)

# Close the cursor
cursor.close()

# Commit the transaction
database.commit()

# Close the database connection
database.close()

# Print results
print ""
print "All Done! Bye, for now."
print ""
#columns = str(ws.ncols)
#rows = str(ws.nrows)
#print "I just imported " %2B columns %2B " columns and " %2B rows %2B " rows to MySQL!"
Reply


Messages In This Thread
MySQL SSL Connection, Need help - by LordVaderXIII - Oct-15-2017, 10:51 PM
RE: MySQL SSL Connection, Need help - by LordVaderXIII - Oct-16-2017, 06:28 AM
RE: MySQL SSL Connection, Need help - by buran - Oct-16-2017, 07:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with MySql Connection EMG2024 2 151 May-29-2024, 03:11 PM
Last Post: EMG2024
  Virtual Env changing mysql connection string in python Fredesetes 0 445 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Mysql and mysql.connector error lostintime 2 815 Oct-03-2023, 10:25 PM
Last Post: lostintime
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,621 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Python MYSQL connection does not work after 1h idle zazas321 9 7,168 Oct-07-2021, 12:02 PM
Last Post: ndc85430
  Serial connection connection issue Joni_Engr 15 8,432 Aug-30-2021, 04:46 PM
Last Post: deanhystad
  mysql connection pool? MuntyScruntfundle 0 2,091 Oct-20-2018, 07:36 PM
Last Post: MuntyScruntfundle
  mysql db connection using python sunstar20 13 12,267 Aug-04-2018, 09:08 AM
Last Post: vishalhule
  ssh + mysql connection python 3.4.3 desudesu 9 13,286 May-25-2017, 03:19 PM
Last Post: desudesu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020