Skip to content

Latest commit

 

History

History
105 lines (74 loc) · 3.17 KB

File metadata and controls

105 lines (74 loc) · 3.17 KB

CSV file to mysql database - using python 2.7.5

1) Make sure you have installed Development Tools

1.png

2) Check hostname is resolving if not please add the local host entry.

2.png

3) Update your system for all fixes

3.png

4) Check firewall status

4.png

5) Stop the firewall if running

5.png

6) Recheck the firewall status

6.png

7) Check the selinux status

7.png

8) If selinux status is enabled, please set in disabled in file the /etc/selinux/config

8.png

9) We are going to install the mysql community version, so please the mysql repo from http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

9.png

10) Install the mysql noarch.rpm, it will add the new repo on your system

10.png

11) Update the newly added repo and dependencies

11.png

12) Mysql installation

12.png

13) Start the mysql server

13.png

14) Check the mysql server status

14.png

15) Follow the secure installation

15.png 16.png 17.png

16) Check if you can login in mysql server with updated password

18.png

17) By default pip not included with python2.7 and CentOS Base repo.

19.png

18) Add EPEL repo to get pip, pip help to install python modules.

20.png

19) Install the pip package

21.png

20) Check Pip is working fine

22.png

21) Check pip version

23.png

22) Create a database

24.png 26.png

23) Create table and make sure you have same number of field as in CSV.

27.png

26) Install the MySQL-Python module

28.png

27) Make the CSV file, here we have replaced spaces with comma

29.png

28) Write the python script to import the data from csv to mysql - csv_mysql.py

#!/usr/bin/python
import MySQLdb
import csv

db = MySQLdb.connect(host="pydesk", # The Host
                      user="root", # username
                      passwd="x", # password
                      db="storage") # name of the data base

cursor = db.cursor()
Query = """ LOAD DATA LOCAL INFILE 'result.csv' INTO TABLE
storage FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED
BY '"' Lines terminated by '\n' IGNORE 1 LINES """

cursor.execute(Query)
db.commit()
cursor.close()

29) run the python code

31.PNG

30) login in mysql and check the data. sample data..!!

30.png

                                            Thank you..!!