批量匯入資料庫(.sql)
阿新 • • 發佈:2018-12-21
int m_export() { FILE *fp; fp=fopen("Export.sql","w+"); char buf[100],buf2[5]; int len; struct dirent *entry; //獲取檔案間目錄內容 printf("請輸入需要匯入的資料庫目錄(絕對路徑):\n"); scanf("%s",buf);getchar(); DIR *dirptr=NULL; dirptr=opendir(buf); //開啟資料夾,失敗返回null if(dirptr==NULL) { printf("name error!!\n"); return -1; } else { while(entry=readdir(dirptr)) { memset(buf2,0,5); len=strlen(entry->d_name); memcpy(buf2,entry->d_name+len-4,4); if(strcmp(buf2,".sql")==0) { fprintf(fp,"source %s/%s\n",buf,entry->d_name); } } } fclose(fp); printf("請輸入密碼:\n"); system("mysql -u root -p database_name < Export.sql"); return 0; }
原理:
先從資料夾中讀取到所有需要匯入的.sql檔名,然後建立新的資料夾存Export.sql放這些檔名,格式:source xxx.sql;最後匯入這個檔案即可(source Export.sql);