Search code examples
programming-languagessqllinux

How to make simple sql modifications from shell?


I have never made any software for linux, but it would be better for me now. I would like to write a simple SQL modification software that I can run via terminal and don't need to run php, apache. (withouth apache)

Could someone convert me this:

<?php

$le = mysql_query('SELECT * FROM tbl_table WHERE alfa="0"');
while ($i = mysql_fetch_assoc($le))
{
  $le2 = mysql_num_rows('SELECT * FROM tbl_table3 WHERE t1id="'.$i["id"].'"');
  if ($le2 > 0)
 {
 mysql_query('UPDATE tbl_table SET num="'.$le2.'" WHERE id="'.$i["id"].'"');
 }
}
?>

Or can anyone give me a tutorial for how I could write it for a shell command, or something that can be started from terminal? Or what is the English name of what I'm looking for? (linux debian)


Solution

  • There are many ways to skin a cat, but I'd go with a Python script.

    Since you're running Linux you'll already have Python on the system. You'll need to install a MySQL driver such as MySQLdb and then just convert your simple PHP script to Python.

    There's an excellent answer which covers what you'll need to know (and more) here: How do I connect to a MySQL Database in Python?.