Shell et mysql

Résolu
lamou23 Messages postés 218 Statut Membre -  
lamou23 Messages postés 218 Statut Membre -
bonjour

je voudrais remplir une table mysql en mode non interactif à l'aide d'un script shell?

j'ai une table Etudiant avec les champs (Nom,Prénom,Matricule)

je voudrai insérer un premier enregistrement dans la table 'tabetud' de la base de données 'ETUDIANT' .

d'habitude je fais ceci à travers le termina:
mysql -u root -p motdepasse

puis:

use ETUDIANT;

ensuite:
INSERT INTO tabetud (nom,prénom,matricule) VALUES ('karim','mechou','54566');

Mon but c'est de faire tout ça dans un script shell !!!!
aidez moi s'il vous plait

merci d'avance

2 réponses

  1. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    Re,

    Un exemple, à toi d'adapter, toutes les commandes dont faites en shell directement.
    ~$ mysql -u root -p'azertyuiop' <<< "SHOW DATABASES"
    Database
    information_schema
    Personne
    aaa
    bbb
    jobeet
    mysql
    repertoire_home
    sql_learn
    ~$ mysql -u root -p'azertyuiop' <<< "CREATE DATABASE ETUDIANT"
    ~$ mysql -u root -p'azertyuiop' <<< "SHOW DATABASES"
    Database
    information_schema
    ETUDIANT
    Personne
    aaa
    bbb
    jobeet
    mysql
    repertoire_home
    sql_learn
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "CREATE TABLE tabetud(nom varchar(50),prenom varchar(50),matricule varchar(10))"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "show tables"
    Tables_in_ETUDIANT
    tabetud
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "desc tabetud" 
    Field   Type    Null    Key     Default Extra
    nom     varchar(50)     YES             NULL
    prenom  varchar(50)     YES             NULL
    matricule       varchar(10)     YES             NULL
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "select * from tabetud"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "INSERT INTO tabetud VALUES ('karim','mechou','54566')"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "select * from tabetud"
    nom     prenom  matricule
    karim   mechou  54566
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "INSERT INTO tabetud VALUES ('azerty','toto','11111')"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT <<< "select * from tabetud"
    nom     prenom  matricule
    karim   mechou  54566
    azerty  toto    11111
    ~$ 
    
    0
  2. lami20j Messages postés 21506 Date d'inscription   Statut Modérateur, Contributeur sécurité Dernière intervention   3 571
     
    Re,

    Ou en utilisant l'option -e de la commande mysql

    ~$ mysql -u root -p'azertyuiop' -e "SHOW DATABASES"
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema | 
    | Personne           | 
    | aaa                | 
    | bbb                | 
    | jobeet             | 
    | mysql              | 
    | repertoire_home    | 
    | sql_learn          | 
    +--------------------+
    ~$ mysql -u root -p'azertyuiop' -e "CREATE DATABASE ETUDIANT"
    ~$ mysql -u root -p'azertyuiop' -e "SHOW DATABASES"
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema | 
    | ETUDIANT           | 
    | Personne           | 
    | aaa                | 
    | bbb                | 
    | jobeet             | 
    | mysql              | 
    | repertoire_home    | 
    | sql_learn          | 
    +--------------------+
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "show tables"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "CREATE TABLE tabetud(nom varchar(50),prenom varchar(50),matricule varchar(10))"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "show tables"
    +--------------------+
    | Tables_in_ETUDIANT |
    +--------------------+
    | tabetud            | 
    +--------------------+
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "desc tabetud" 
    +-----------+-------------+------+-----+---------+-------+
    | Field     | Type        | Null | Key | Default | Extra |
    +-----------+-------------+------+-----+---------+-------+
    | nom       | varchar(50) | YES  |     | NULL    |       | 
    | prenom    | varchar(50) | YES  |     | NULL    |       | 
    | matricule | varchar(10) | YES  |     | NULL    |       | 
    +-----------+-------------+------+-----+---------+-------+
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "select * from tabetud"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "INSERT INTO tabetud VALUES ('karim','mechou','54566')"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "select * from tabetud"
    +-------+--------+-----------+
    | nom   | prenom | matricule |
    +-------+--------+-----------+
    | karim | mechou | 54566     | 
    +-------+--------+-----------+
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "INSERT INTO tabetud VALUES ('azerty','toto','11111')"
    ~$ mysql -u root -p'azertyuiop' ETUDIANT -e "select * from tabetud"
    +--------+--------+-----------+
    | nom    | prenom | matricule |
    +--------+--------+-----------+
    | karim  | mechou | 54566     |
    | azerty | toto   | 11111     |
    +--------+--------+-----------+
    
    0
    1. lamou23 Messages postés 218 Statut Membre
       
      merci bcp :)
      0