Ruby on Rails学习笔记(二 )

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命令将创建的资源添加到数据库中

  1. 创建数据库
    rake db:create
  2. 更新数据库中的表结构
    rake db:migrate

    3修改主页

    编辑config目录下的routes.rb文件,添加代码

root 'user#index'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值