Python Forum
Create a function for writing to SQL data to csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a function for writing to SQL data to csv
#3
Recently there was another question about a 60GB csv. Are you guys in the same class? What is a 60GB csv for?

How many rows would such a file have? Must be millions and millions! How many columns?

Not many people have 64GB ram. Get little bits at a time

Assuming your database table has a column id, a unique key, you can use that to get chunks of the table and write to a text file. If you get interrupted, or want to stop when the number of lines reaches a very large number, you can. Next time open it again append, or create another text file

This just gets 1 row at a time, but you could change that for fetchmany(size=1000) or whatever. You can directly open the result text file in Libre Office Calc as a spreadsheet.

import pymysql

savepath = '/home/pedro/myPython/pymysql/results.txt'

def mysqlRemoteHW(clas, weeknr, idnum): 
    # To connect remote MySQL database 
    conn = pymysql.connect( 
        host='123.456.789.123', 
        user='me',  
        password = 'secret', 
        db='allstudentsdb', 
        ) 
      
    cur = conn.cursor()

    # Select query 
    #cur.execute(f"SELECT studentnr, score FROM allstudentsAnswers{clas} WHERE weeknr = '{weeknr}'")
    #cursor.execute("SELECT spam FROM eggs WHERE lumberjack = ?", (lumberjack,))
    sql = f"SELECT id, studentnr, score FROM allstudentsAnswers{clas}HW WHERE weeknr = %s AND id = %s"
    cur.execute(sql, (weeknr, idnum,))
    output = cur.fetchone() 
      
    #for i in output: 
        #print(i) 
      
    # To close the connection 
    conn.close()
    return output

clas = input('Enter 21BE  ')
print('What week number scores are we getting from MySQL?')
weeknumber = input('Just enter a week number, like: Week1 or Week4 or Week11 ')

with open(savepath, 'a') as res:
    for i in range(510, 521):
        results = mysqlRemoteHW(clas, weeknumber, i)
        string = str(results[0]) + ',' + str(results[1]) + ',' + str(results[2]) + '\n'
        res.write(string)
Reply


Messages In This Thread
RE: Create a function for writing to SQL data to csv - by Pedroski55 - Oct-01-2022, 12:48 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 387 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Create X Number of Variables and Assign Data RockBlok 8 1,060 Nov-14-2023, 08:46 AM
Last Post: perfringo
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,239 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Function parameter not writing to variable Karp 5 1,044 Aug-07-2023, 05:58 PM
Last Post: Karp
Bug New to coding, Using the zip() function to create Diret and getting weird results Shagamatula 6 1,524 Apr-09-2023, 02:35 PM
Last Post: Shagamatula
  Create simple live plot of stock data dram 2 2,994 Jan-27-2023, 04:34 AM
Last Post: CucumberNox
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 3,177 Dec-06-2022, 11:09 AM
Last Post: mg24
  python create function validation mg24 1 884 Nov-15-2022, 01:57 AM
Last Post: deanhystad
  create my exception to my function korenron 2 848 Nov-09-2022, 01:50 PM
Last Post: korenron
  Create SQL connection function and validate mg24 1 1,000 Sep-30-2022, 07:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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