为数据库创建初始User数据
我们可以从./simple-demo/controller/demo_data.go和user.go中分别获得一些项目的初始数据。现使用sql语句将其插入数据库。
user.go中有如下数据:
// usersLoginInfo use map to store user info, and key is username+password for demo
// user data will be cleared every time the server starts
// test data: username=zhanglei, password=douyin
var usersLoginInfo = map[string]User{
"zhangleidouyin": {
Id: 1,
Name: "zhanglei",
FollowCount: 10,
FollowerCount: 5,
IsFollow: true,
},
}
对比User数据表的定义:
create table User ( user_id bigint not null, user_name varchar(32) not null, avatar char(64), background_image char(64), signature varchar(100), password varchar(32) not null, token varchar(64) not null, primary key (user_id) );
编写出在数据表User中插入数据的sql语句:
insert into User values (1, "zhanglei", null, null, null, "douyin", "1234567");
demo_data.go中有如下数据:
var DemoUser = User{
Id: 1,
Name: "TestUser",
FollowCount: 0,
FollowerCount: 0,
IsFollow: false,
}
同样地,根据User表的定义,可以写出在数据表User中插入数据的sql语句:
insert into User values (2, "TestUser", null, null, null, "douyin", "1234567");
因id重复了,此处id设置2。
为数据库创建初始Video数据
demo_data.go中有如下数据:
var DemoVideos = []Video{
{
Id: 1,
Author: DemoUser,
PlayUrl: "https://www.w3schools.com/html/movie.mp4",
CoverUrl: "https://cdn.pixabay.com/photo/2016/03/27/18/10/bear-1283347_1280.jpg",
FavoriteCount: 0,
CommentCount: 0,
IsFavorite: false,
},
}
对比Video数据表的定义: create table Video ( video_id bigint not null, user_id bigint not null, play_url char(64) not null, cover_url char(64) not null, title varchar(50) not null, primary key (video_id) );
编写出在数据表Video中插入数据的sql语句:
insert into Video values (1, 2, "3214195939554309269", "10438559648169641774","熊出没");
为数据库创建初始Comment数据
demo_data.go中有如下数据:
var DemoComments = []Comment{
{
Id: 1,
User: DemoUser,
Content: "Test Comment",
CreateDate: "05-01",
},
}
对比Comment数据表的定义:
create table Comment ( comment_id bigint not null, user_id bigint not null, video_id bigint not null, create_date date not null, comment_content varchar(300) not null, primary key (comment_id) );
编写出在数据表Comment中插入数据的sql语句:
insert into Comment values (1, 2, 1, '2023-05-01', "Test Comment");
将设计好的数据导入mysql中
用修改后的密码登录mysql终端。首先使用下列语句新建数据库:
create database douyin;
其次使用下列语句,选择新建的数据库douyin:
use douyin;
最后输入下列命令,将设计好的数据表导入数据库中:
source ./database/initdata.sql;
(未完待续)