access the database(under the example of mysql)
there is a course about installing mysql on ubuntu
a course about installing mysql on ubuntu
Ruby provides Database Driven(dbd) for each database,
we have got the bastic dbd ,in order to get
install dbi(database interface)
command
gem install dbi
instal dbd of mysql(database driven)
sudo apt-get install libmysqlclient15-dev
gem install dbd-mysql
-
install to package
- libmysqlclient-dev
- ruby-dev
input the command in Terminal after installing
sudo apt-get install libmysqlclient-dev ruby-dev
you must add “sudo” on the commond
you won’t get enough root without “sudo” before the commond
在数据库中创建表
create table users(
id int(10) unsigned not null auto_increment,
username text default null,
password text default null,
primary key (id)
) engine=InnoDB default charset=utf8;
rails generate scaffold username:text password:text id:int
rails generate scaffold user name
1 以上语句是通过脚手架创建资源
创建user表,表里面有两个属性,分别是name,password,其中password类型为text
generate 生成,形成,造成;产生物理反应;产生(后代);引起
scaffold 支架,脚手架;<史>断头台
2 通过rake命令将创建的资源添加到数据库中
- 创建数据库
rake db:create
更新数据库中的表结构
rake db:migrate
3修改主页
编辑config目录下的routes.rb文件,添加代码
root 'user#index'