假设我们有这么一张表,里面有较多的数据,我们需要将其导入到MySQL数据库中进行处理。

Part 1
打开命令行,连接数据库,即命令行中输入
mysql -u root -p
回车后根据提示输入密码后,再次回车即可进入

Part 2
1)新建数据库
create database project;
2)使用刚刚新建的数据库
use project;

3)新建一个表,用来保存之前Excel表格中的数据,我们需要参考excel表格中列中的名字和内容,新建的表格(table)中列的名字应和其保持一致,OK,我们直接上手开始吧~
新建表命令如下
create table male
(
ID2 char(5) primary key,region2 char(4),gender char(2),age2 int,height2 int,marriage2 bool,educatior char(1),danwei char(8),housing2 char(1),income2 int,ex_age_low2 int ,ex_age_high2 int,ex_rigon2 int,ex_height_low2 int,ex_height_high2 int,ex_education2 int,ex_housing2 int,ex_marriage2 int ,ex_income2 int
);

为了简便,我就完全按照excel表格中列的名字和数据来给我数据库中列定义名字和数据类型
4)我们要把Excel中数据形式稍微修整一下才能让我们的数据库系统读入我们的数据库表中,具体设置如下:
打开要导入的Excel表格,点击文件,另存为

选择保存类型,注意,这点很重要,选择保存为“文本文件(制表符分割)”,然后点击保存。


然后会弹出这个询问框,仍继续,点击是。
然后我们打开刚刚保存的txt文件

将前面的标题栏删除掉,因为我们只需要数据,也只能只有数据

删除后,内容如下

OK,我们保存修改后文件,然后关掉它
Part 3
导入刚刚保存的数据(.txt)
接下来继续在命令行中进行操作,在命令行中输入如下命令:
load data local infile "F:/Desktop/male.txt" into table male fields terminated by "\t" lines terminated by "\r\n";
说明:我刚刚把数据保存保存到F:/Desktop下,文件名字为male.txt
因为用制表符分割的数据,所以用到命令“fields terminated by "\t" ”
而且windows下文件通常以换行并回车作为文件行的结尾,故加上了“ lines terminated by "\r\n" ”

OK! 输入刚才命令并后回车后,如截屏所示,导入成功。OK,我们查看一下表中的内容。


OK,导入数据结束,下一步我们可根据需要对数据进行分析处理。这就很有意思了,但不是我们这次需要讨论的问题,那就先这样了,拜拜 ... (逃~~
网友评论