Cùng ủng hộ nhau moi xiền của google

Search

Google
 

Sunday 27 January 2008

Export, Import table trong MySQL

1/Export 1 table ra text file
+)tạo 1 database và 1 table với cấu trúc phía dưới
PHP Code:
drop database if exists Exdatabase;
create database Exdatabase;
create table Exdatabase.Extable (
id tinyint(2) AUTO_INCREMENT not null,
ten varchar(30) null,
ngaysinh date null,
primary key (id)
)
engine=innodb charset=utf8;
+)nhập DL cho table Extable
PHP Code:
insert into Exdatabase.Extable(ten,ngaysinh)
values ('thanh','6543-2-1'),('vợ thanh','2345-6-7')
+)tiến hành Export data trong table Extable ra text file(ExportMySQL.txt)
PHP Code:
select * from extable into outfile 'D:\ExportMySQL.txt'
fields TERMINATED by ','
lines TERMINATED by '\r\n';
bây giờ bạn đã có 1 file ExportMySQL.txt nằm ở thư mục ổ D với định dạng file
Trích dẫn:
1,thanh,6543-02-01
2,vợ thanh,2345-06-07
chú ý :
-bạn có thể thay đổi dấu ngăn cách mỗi cột trong table Extable bằng cách thay đổi kí tự trong lệnh fields TERMINATED by 'kí tự'
-có thể export với nhiều điều kiện khác nhau sau lệnh select


2/Import 1 text file vào 1 table trong MySQL
+)tạo 1 database và 1 table với cấu trúc phía dưới
PHP Code:
drop database if exists Imdatabase;
create database Imdatabase;
create table Imdatabase.Imtable (
id tinyint(2) null,
ten varchar(30) null,
ngaysinh date null
)
engine=innodb charset=utf8;
+)Nhập dữ liệu cho text file ImportMySQL.txt('D:\ImportMySQL.txt') với các cột được ngăn cách bằng kí tự tùy ý,mỗi dòng trong text file sẽ tương ứng với mỗi record được import vào table,bạn lưu ý khi save text file bạn cần chọn Encoding=utf8 nếu muốn hiển thị unicode lên table cần import
+)tiến hành Import data từ ImportMySQL.txt vào table Imtable
PHP Code:
load data infile 'D:\ImportMySQL.txt'
into table Imtable
fields TERMINATED by
'kí tự ngăn cách'
lines TERMINATED by '\r\n';
với cấu trúc DL trong text file
Trích dẫn:
1,vô danh,2134-3-4
2,chưa rõ,3456-1-2
kết quả ta nhận được bảng Import



chú ý : bạn có thể giới hạn số dòng cần Import với lệnh IGNORE 'số dòng' LINES sau lệnh Import

với file html cũng tương tự như file text

No comments: