我需要從csv文件中加載大型數據集(現在20GB,但將來100GB)。我在Python中使用MySQLdb模塊(PyCharm)。我也只需要選擇一些特定的列。到目前爲止,我已經嘗試過:如何從Python中的csv文件創建SQL數據庫
import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost',
user='root',
passwd='',
db='mydb')
cursor = mydb.cursor()
csv_data = csv.reader(file('collected_quotes_sample.csv'))
for row in csv_data:
cursor.execute('INSERT INTO testcsv(RIC, Date, Time, Ask, Bid, BAS, window) VALUES(%s, %s, %s, %s, %s, %s, %s)', row)
#close the connection to the database.
mydb.commit()
cursor.close()
print "Done"
但它提供了很多錯誤。我是python和SQL數據庫的新手,所以我不熟悉命令/代碼。所以我有幾個問題:
1. with MySQLdb.connect(host,user,passwd,db etc), what are host, user, password etc? to my understanding, they are credentials for my account in the computer. so do I need to put in my user account and password?
2. What does mydb.cursor do?
3. How to upload a csv file into a SQL database? and after the database is created, I can write a python script to work on it and there is no need to re read/create the database?
非常感謝!
你真的有本地主機上的MySQL服務器設置? – Keatinge
蟒蛇MySQLdb只是一個客戶端,可以連接到真正的MySQL服務器 – YOU
@Keatinge我不認爲我有MySQL服務器設置。我怎樣才能做到這一點?謝謝 – duckman