我需要从csv文件加载大型数据集(目前为20gb,但将来将为100gb)。我在python(PyCharm)中使用MySQLdb模块。我还只需要选择一些特定的列。到目前为止,我已经尝试过了:
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?
非常感谢!
回答您的问题
- 与MySQLdb.connect(主机,用户,passwd,db等)一起使用,什么是主机,用户,密码等?据我了解,它们是我在计算机中帐户的凭据。所以我需要输入我的用户帐户和密码吗?
localhost
(127.0.0.1),并且端口号是您在创建服务器时定义的端口号。服务器启动后,可以将一个或多个客户端连接到数据库服务器。然后,您需要拥有一个root
具有所有特权的超级用户(如)和其他几个普通用户(可能具有较少的特权)。
- mydb.cursor是做什么的?
cursor
对象,但是由于您将MySQLdb用作Python包装器,因此您需要使用它,因为DB-API要求您连接到这样(光标对象是Python DB-API 2.0中指定的抽象)。
- 如何将csv文件上传到SQL数据库?创建数据库后,我可以编写一个python脚本来处理它,而无需重新读取/创建数据库?
read
从数据库中获取。但是您可以将常用的SQL(或调用一些存储的函数/过程)放入Python代码中,这样您就可以调用一个函数以所需的方式检索数据。通常,我认为您在急于使用MySQL基础之前应该了解更多有关MySQL基础的知识。以及如何在Windows上安装MySQL
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句