• shell操作mysql


    shell操作mysql

    #!/bin/bash  
    
    HOSTNAME="192.168.111.84"                                           #数据库信息
    PORT="3306"
    USERNAME="root"
    PASSWORD=""
    
    DBNAME="test_db_test"                                                       #数据库名称
    TABLENAME="test_table_test"                                            #数据库中表的名称
    
    #也可以写 HOSTNAME="localhost",端口号 PORT可以不设定
    
     #创建数据库
    create_db_sql="create database IF NOT EXISTS ${DBNAME}"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"
    
    #注意:-p${PASSWORD}中间不能有空格
    
    #创建表
    create_table_sql="create table IF NOT EXISTS ${TABLENAME} (  name varchar(20), id int(11) default 0 )"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${DBNAME} -e "${create_db_sql}"
    
    #插入数据
    insert_sql="insert into ${TABLENAME} values('billchen',2)"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e  "${insert_sql}"
    
    #查询
    select_sql="select * from ${TABLENAME}"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${select_sql}"
    
    #更新数据
    update_sql="update ${TABLENAME} set id=3"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e "${update_sql}"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e  "${select_sql}"
    
    #删除数据
    delete_sql="delete from ${TABLENAME}"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e  "${delete_sql}"
    mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e  "${select_sql}"
  • 相关阅读:
    Java多线程_同步工具CountDownLatch
    Java多线程_Semaphore信号量
    Java多线程_ReentrantLock
    Java多线程_CAS算法和ABA问题
    Java多线程_Atomic
    Java多线程_生产者消费者模式2
    Java多线程_生产者消费者模式1
    Java多线程_阻塞队列
    Java多线程_wait/notify/notifyAll方法
    Java多线程同步_synchronized
  • 原文地址:https://www.cnblogs.com/30go/p/11160152.html
Copyright © 2020-2023  润新知