亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1383 | 回復(fù): 0
打印 上一主題 下一主題

Drop All MySQL Tables without Deleting A Database [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-12-19 13:55 |只看該作者 |倒序?yàn)g覽

有時候我們不具有刪除數(shù)據(jù)庫的權(quán)限,只是被分配一個我們可以改動的數(shù)據(jù)庫給我們。這個時候就不能使用以下方法:

  1. mysql> DROP DATABASE atomstore;

  2. mysql> CREATE DATABASE atomstore;

下面我用C寫的兩個小程序可以簡單地做同樣的事情而不用刪除數(shù)據(jù)庫。而用腳本實(shí)現(xiàn)的方法(by VIVEK GITE)也將附于文后。

 

源碼在Ubuntu Linux ubuntu 2.6.24-22-generic #1 SMP Mon Nov 24 19:35:06 UTC 2008 x86_64 GNU/Linux下測試通過。

  1. /usr/include/mysql/mysql.h
  2. 是安裝mysql時安裝的頭文件

  3. 我使用的編譯命令如下:gcc -o mysql_list_tables mysql_list_tables.-lmysqlclient -lz

  4. -lmysqlclient -lz”必須要附上。
 

使用方法如下:

  1. ./mysql_list_tables host user password database table_list.txt 
  2.  
  3. 這樣會生成所有表名,并存于table_list.txt中。 
  4.  
  5. ./mysql_drop_tables host user password database table_list.txt 
  6.  
  7. 這樣會將table_list.txt中的表全部刪除。 
  8.  
  9. 其中
  10. host 是mysql server 的地址
  11. user 是你的mysql 用戶名 
  12. password 是你的mysql 登陸密碼 
  13. database 是你想要刪除的所有表所在的具體mysql數(shù)據(jù)庫。

 
  1. /* C source file: mysql_list_tables.c 
  2.  * Written by linuxqc@gmail.com */
  3. #include </usr/include/mysql/mysql.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>

  6. int main(int argc, char *argv[])
  7. {
  8. MYSQL *mysql;
  9. MYSQL_RES *result;
  10. MYSQL_ROW row;
  11. FILE *fp;

  12. if (argc != 6) {
  13.     printf("mysql_list_tables, a small tool to list all your tables in a mysql database.\n");
  14.     printf("Usage:\n");
  15.     printf("mysql_list_tables <host> <user> <password> <database> <table_list.txt>\n");
  16.     printf("table_list.txt ia a text file in which all the names of the tables of the specified database will be stored one name per line.\n");
  17.     printf("This list can be used by the tool mysql_drop_tables.\n"); 
  18.     exit(EXIT_FAILURE);
  19. }

  20. fp = fopen(argv[5],"w");
  21. if (fp == NULL) {
  22.     printf("can't open output stream.\n");
  23.     exit(EXIT_FAILURE);
  24. }
  25. if ((mysql=mysql_init(NULL)) == NULL) {
  26.     printf("mysql_init() error!\n");
  27. }
  28. if (mysql_real_connect(mysql, argv[1],argv[2], argv[3], argv[4],3306,NULL,0)== NULL) {
  29.     printf("mysql_connect() error!\n");
  30. }
  31. if ((result = mysql_list_tables(mysql, NULL)) == NULL) {
  32.     printf("mysql_list_tables() error!\n");
  33. }
  34. while ((row = mysql_fetch_row(result)) != NULL) {
  35.     printf("%s\n", row[0]);
  36.     fprintf(fp, "%s\n", row[0]);
  37. }
  38. fclose(fp);
  39. mysql_free_result(result);
  40. mysql_close(mysql);
  41. exit(EXIT_SUCCESS);
  42. }

 

  1. /* C source file: mysql_drop_tables.c 
  2.  * Written by linuxqc@gmail.com */
  3. #define _GNU_SOURCE
  4. #include <stdio.h>
  5. #include </usr/include/mysql/mysql.h>
  6. #include <stdlib.h>

  7. int main(int argc, char *argv[])
  8. {
  9. MYSQL *mysql;
  10. MYSQL_RES *result;
  11. MYSQL_ROW row;
  12. FILE * fp;
  13. char * line = NULL;
  14. size_t len = 0;
  15. ssize_t read;
  16. char command[] = "DROP TABLE ";
  17. char query[100] = "\0";

  18. if (argc != 6) {
  19.     printf("mysql_drop_tables, a small tool to drop all your tables.\n");
  20.     printf("Usage:\n");
  21.     printf("mysql_drop_tables <host> <user> <password> <database> <table_list.txt>\n");
  22.     printf("table_list.txt ia a text file in which all the names of the tables which you want to drop have been stored one name per line\n");
  23.     printf("You may use the tool mysql_list_tables to create the table_list.txt\n"); 
  24.     exit(EXIT_FAILURE);
  25. }

  26. if ((mysql=mysql_init(NULL)) == NULL) {
  27.     printf("mysql_init() error!\n");
  28. }
  29. if (mysql_real_connect(mysql, argv[1],argv[2], argv[3], argv[4],3306,NULL,0)== NULL) {
  30.     printf("mysql_connect() error!\n");
  31. }
  32. if ((result = mysql_list_tables(mysql, NULL)) == NULL) {
  33.     printf("mysql_list_tables() error!\n");
  34. }
  35. fp = fopen(argv[5],"r");
  36. if (fp == NULL) {
  37.     printf("can't read input stream.\n");
  38.     exit(EXIT_FAILURE);
  39. }
  40. while ((read = getline(&line, &len, fp)) != -1) {
  41.     strcpy(query, command);
  42.     strcat(query, line);
  43.     if (mysql_query(mysql, query) != 0) {
  44.         printf("mysql_query() error!\n");
  45.     }
  46. }
  47. if (line) {
  48.     free(line);
  49. }
  50. fclose(fp);
  51. mysql_free_result(result);
  52. mysql_close(mysql);
  53. exit(EXIT_SUCCESS);
  54. }

 附一:VIVEK GITE寫的腳本:

  1. #!/bin/bash
  2. MUSER="$1"
  3. MPASS="$2"
  4. MDB="$3"
  5.  
  6. # Detect paths
  7. MYSQL=$(which mysql)
  8. AWK=$(which awk)
  9. GREP=$(which grep)
  10.  
  11. if [ $# -ne 3 ]
  12. then
  13.     echo "Usage: $0 {MySQL-User-Name} {MySQL-User-Password} {MySQL-Database-Name}"
  14.     echo "Drops all tables from a MySQL"
  15.     exit 1
  16. fi
  17.  
  18. TABLES=$($MYSQL -u $MUSER -p$MPASS $MDB -'show tables' | $AWK '{ print $1}'| $GREP -'^Tables' )
  19.  
  20. for t in $TABLES
  21. do
  22.     echo "Deleting $t table from $MDB database..."
  23.     $MYSQL -u $MUSER -p$MPASS $MDB -"drop table $t"
  24. done

 關(guān)于此腳本請參看:http://www.cyberciti.biz/faq/how-do-i-empty-mysql-database/

您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP