This code demonstrates how to upload files into SQL. The files should begin as a .csv
file, typically from a dataframe.
Useful rescources with more detail:
https://grid.rcs.hbs.org/importing
https://grid.rcs.hbs.org/import-and-export-text-files
A. Format File - prepare the file for upload into SQL database by changing its format into a pipe-separated (|
) .txt
file
- Open csv-to-txt-for-SQL.py
- Change in_filename, out_filename, db_table_name, file_location in lines 6 - 9 to your specific files and directories
- Run the code. The code will output A. formatted txt-file and B. a command which can be used to create the sql-table
- Copy the command so you enter it in for step 3. of the Create Database process below
- Move the txt file onto the GRID's desktop for the Move File process below
B. Create Database - enter the GRID (or SecureCRT) and use the following commands on the command line interface.
mysql
use agoldenberg_twitter_data;
create table table_import (Column_1 char(20), Column_2 char(20), Column_3 char(20));
- this command should have also appeared in the file formatter above
Create Import Directory
mkdir /export/mdb_external/import/username
chmod 700 /export/mdb_external/import/username
Move File
pwd
#Get the full directory of your desktop where your file is returns for example:/export/home/rcsguest/rcs_username/Desktop
mv /export/home/rcsguest/rcs_username/Desktop/SampleData.txt /export/mdb_external/import/username
Import File
mysql
use agoldenberg_DATABASENAME;
load data local infile '/export/mdb_external/import/username/SampleData.txt' into table table_import fields terminated by '|' lines terminated by '\n' ignore 1 lines;
Check Data
Describe TABLENAME;
SELECT * FROM TABLENAME;
Remove temp-import files & folder
- rm -rf /export/mdb_external/import/username