Search code examples
pythonmysqlshelltcsh

Filling Sql table with a script


I have a SQL database and I want to fill tables with a script. The following source code shows what I have done so far:

  #!/bin/tcsh
   mysql -h dbs1 -D my_devel -u USER --password=XYZ
   insert into my_table ( col_id, type, file, result, signature) values (***,'###','+++','$$$','...');
   exit

According that I pass the parameters '*','#','+','$' and '.' via a method defined in python.

But somehow this is not working for me. Can someone tell me why, or suggest an alternative?


Solution

  • This seems to work fine for me:

    #!/bin/tcsh
    
    # count-rows.csh
    
    mysql -h 127.0.0.1 -D tpcw -u root <<EOF
    select count(*) from $1;
    exit
    EOF
    

    You need to send the commands to execute to the standard input of the mysql utility. There are several ways to do that, but using a here document is probably the easiest method.

    That said, if your data comes from a Python program, you should look at using a proper MySQL connector for Python. You should see a definite performance increase, but more importantly you will be able to handle any errors properly.