Python Mysql在文件中加载数据(Python Mysql Load data in file)
我有一个像这样的列的数据库
Col1 Col2 Col3 Col4 Col5
我有两个csv文件,其中包含这样的数据
File-1 Column1, Column2, Column4, Column5 1,2,4,5 1,2,4,5 1,2,4,5 1,2,4,5 1,2,4,5 File-2 Column1, Column3, Column5 1,3,5 1,3,5 1,3,5 1,3,5 1,3,5 1,3,5
请告诉我使用infile命令加载数据以在表中的各列中加载file1和file2数据。 即,从file1开始,.csv中的column4应该进入表的COl4。
使用此命令,Column4进入Col3,Column5进入Col4。
import MySQLdb import os import string import warnings print "File Loader Started : QT" output_path="F:/TestData_SD/QT_Files/" # Open database connection db = MySQLdb.connect (host="localhost",port=3307,user="root",\ passwd="gamma123",db="db_schema") sql = """LOAD DATA LOCAL INFILE '{}' INTO TABLE struct_pqrst FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\\r\\n' IGNORE 1 LINES;;""" l = os.listdir(output_path) for file_name in l: if file_name.endswith('.csv'): try: cursor = db.cursor() cursor.execute(sql.format(output_path+file_name)) db.commit() print "Loading file:"+file_name except Exception: print "Exception" # Rollback in case there is any error db.rollback() # disconnect from server db.close() print "File Loader Ended : QT"
请帮忙
I have a database with columns like this
Col1 Col2 Col3 Col4 Col5
And i have two csv files which has data like this
File-1 Column1, Column2, Column4, Column5 1,2,4,5 1,2,4,5 1,2,4,5 1,2,4,5 1,2,4,5 File-2 Column1, Column3, Column5 1,3,5 1,3,5 1,3,5 1,3,5 1,3,5 1,3,5
Please tell me the Load data with infile command to load file1 and file2 data in respective columns in table. i.e. from file1 the column4 from .csv shouldo go into the COl4 of the table.
With this command the Column4 goes in Col3 and Column5 into Col4.
import MySQLdb import os import string import warnings print "File Loader Started : QT" output_path="F:/TestData_SD/QT_Files/" # Open database connection db = MySQLdb.connect (host="localhost",port=3307,user="root",\ passwd="gamma123",db="db_schema") sql = """LOAD DATA LOCAL INFILE '{}' INTO TABLE struct_pqrst FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\\r\\n' IGNORE 1 LINES;;""" l = os.listdir(output_path) for file_name in l: if file_name.endswith('.csv'): try: cursor = db.cursor() cursor.execute(sql.format(output_path+file_name)) db.commit() print "Loading file:"+file_name except Exception: print "Exception" # Rollback in case there is any error db.rollback() # disconnect from server db.close() print "File Loader Ended : QT"
please help
满意答案
在第二个文件中,您可以定义列。
LOAD DATA LOCAL INFILE '{}' INTO TABLE struct_pqrst (Column1, Column3, Column5) FIELDS TERMINTED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
看看我发布的链接。
In the second file you could define the columns.
LOAD DATA LOCAL INFILE '{}' INTO TABLE struct_pqrst (Column1, Column3, Column5) FIELDS TERMINTED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
Have a look in the link i posted.
Ref: How to insert selected columns from a CSV file to a MySQL database using LOAD DATA INFILE
相关问答
更多有什么数据库专业书籍介绍?
MySQL从CSV数据中加载NULL值(MySQL load NULL values from CSV data)
Python中的MySQL LOAD DATA INFILE无法使用变量(MySQL LOAD DATA INFILE in Python not working using variable)
如何在MySQL中加载文件(How to load file in MySQL)
帮助MySQL LOAD DATA INFILE(Help with MySQL LOAD DATA INFILE)
MySQL在文件中加载数据不插入所有值(MySQL LOAD DATA IN FILE Not Inserting all the values)
在文件错误中加载数据(LOAD DATA IN FILE ERROR)
Python将一组.csv文件加载到MySQL中(Python loading a set of .csv files into MySQL)
Python Mysql在文件中加载数据(Python Mysql Load data in file)
LOAD DATA LOCAL INFILE上的Python2.7 MySQL连接器错误(Python2.7 MySQL Connector Error on LOAD DATA LOCAL INFILE)
相关文章
更多form load 的问题
Becoming a data scientist
《Big Data Glossary》笔记
探索 Python,第 1 部分: Python 的内置数值类型
python top project of 2013
实例讲解Hadoop中的map/reduce查询(Python语言实现)
又拍网架构-又一个用到python的网站
又拍网架构-又一个用到python的网站
又拍网架构-又一个用到python的网站
又拍网架构-又一个用到python的网站
最新问答
更多绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)
如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)
ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)
SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)
宁夏银川永宁县望远镇哪里有修mp5的?
我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)
郑州会计培训班
如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)
在响应图像上叠加网格(Overlay grid on responsive image)
无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)
Copyright ©2023 656463.com All Rights Reserved.滇ICP备2022006988号-50
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们,本站将尽快处理。谢谢合作!