我需要从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