十八般武艺

学习思考成长


  • 首页

  • 分类

  • 归档

  • 标签

从网页查询数据库

发表于 2016-04-16   |  
  1. 检查过滤用户输入的数据
  2. 建立数据库连接
  3. 查询数据库
  4. 获取数据
  5. 显示结果给用户

使用sql进行CRUD操作(MySQL Version)

发表于 2016-04-16   |   分类于 Method   |  

示例数据库:sakila database,详细介绍。

所谓CRUD操作是指对数据库的数据进行Create,Read,Update,Delete操作,下面我们一一介绍:

  1. Create

    新建数据,即插入数据。

    1
    2
    INSERT [into] table [(column1,column2,column3,...)] VALUES
    (value1, value2, value3,...);
  2. Read

    读取数据,即查询数据。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    SELECT [options] items
    [into file_details]
    FROM tables
    [WHERE conditions]
    [GROUP BY group_type]
    [HAVING where_definition]
    [ORDER BY order_type]
    [LIMIT limit_criteria]
    [PROCEDURE proc_name(arguments)]
    [lock_options];

    除此之外,还有多表查询和子查询。

  3. Update

    更新数据。

    1
    2
    3
    4
    5
    UPDATE [LOW_PRIORITY] [IGNORE] tablename
    SET column1=expression1,column2=expression2,...
    [WHERE condition]
    [ORDER BY order_criteria]
    [LIMIT number]
  4. Delete
    删除数据。

    1
    2
    3
    4
    DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM table
    [WHERE condition]
    [ORDER BY order_cols]
    [LIMIT number]

Feynman Technique 费曼技巧

发表于 2016-04-02   |   分类于 Concept   |  

什么是费曼技巧

费曼技巧是一种以教促学的学习方法。

适用的学习内容

适用于概念和概念应用的学习
例如XXX是什么,怎么使用XXX。

应用场景

  1. 学习一个新的或者是你不是很明白的想法或概念,
  2. 记住一个你已经明白的想法或者概念。
  3. 用于考试学习,快速深入理解一个想法或者概念,并转化成长期记忆。

步骤

  1. 选择一个新概念

  2. 假装你在教授这个概念给学生。

  3. 无论什么时候卡壳,立刻查资料,直达通顺。

  4. 最后简化表达,适当使用类比。

实例

1.理解扭矩

扭矩是一种转动的力

想象一个扳手,正在转动一个螺帽 ,左转松,右转紧 。

2. 理解递归

这里学习资料,来自维基百科:递归

在方法中使用方法本身,同时不断缩小问题规模,直至小到我们知道怎么解决。

比如吃苹果

吃苹果的方法: 拿一个苹果削皮切块用牙签吃。

吃苹果的方法已经知道,用递归吃100个苹果的方法是什么?

先从100个苹果拿1个苹果放一边,想剩下99个怎么吃?然后再把1个苹果放一边,想剩下98个怎么吃?最终问题将变成1个苹果怎么吃?而你吃1个苹果的方法已经知道了!

问题已经从吃100个苹果的问题,变成吃99个苹果的问题,最后变成吃1个苹果的问题。

下面用代码表示递归吃苹果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function eatApples($n)
{
echo "I am goint to eat $n apples\n";
//n>1个苹果的吃法不知道,缩小问题规模,直到变成n=1个苹果的问题
if ($n > 1) {
eatApples($n - 1);
// n-1个苹果的方法
echo "I have eaten $n apples so far\n";
//n=1个苹果的吃法已经知道
} else {
echo "I have eaten 1 apple so far\n";
}
}

如果不理解,可以在看看下面示例。

经典递归示例 - 阶乘

n = 0, fact(n) =1

n > 0, n *fact(n-1)

1
2
3
4
5
6
7
8
9
10
11
function f($n)
{
if($n == 0)
{
return 1;
} else
{
return $n * f($n-1);
}
}

​

How to Use Git

发表于 2016-04-02   |   分类于 method   |  

本文参考阮一峰 - Git 使用规范流程

团队开发中,遵循一个合理、清晰的Git使用流程,是非常重要的。

否则,每个人都提交一堆杂乱无章的commit,项目很快就会变得难以协调和维护。

第零步:克隆远程仓库

1
$ git clone git@gitlab.moodle.nordanglia.com:moodle/school.git

第一步:新建issue和分支

首先,每次开发新功能或者修复bug,都应该新建一个issue和一个单独的分支。

分支命名应该以issue number开头,例如1-feature-isams-integration,24-bug-require-parameter。

1
2
3
4
5
6
# 获取production最新代码
$ git checkout production_29
$ git pull
# 新建一个开发分支1-feature-name,
$ git checkout -b 1-feature-name

第二步:提交分支commit

分支修改后,就可以提交commit了。

1
2
3
$ git add .
$ git status
$ git commit --verbose

git status 命令,用来查看发生变动的文件。
git diff 命令,用来查看详细改动。
git commit 命令的verbose参数,会列出 diff 的结果。

第三步:撰写提交信息

提交commit时,必须给出完整扼要的提交信息,下面是一个范本。

1
2
3
4
5
Present-tense summary under 50 characters
More information about commit (under 72 characters).
issue name #issue number

第一行是不超过50个字的提要,然后空一行,罗列出改动原因、主要变动、以及需要注意的问题。

最后,提供对应的issue的name和number(比如iSAMS integration #1)。

第四步:与主干同步

分支的开发过程中,要经常与主干保持同步。

1
2
3
4
5
# 获取远程production最新代码
$ git fetch origin
# 本地合并production的到当前分支,并把你的commits放在最后面。
$ git rebase origin/production_29

第五步:合并commit

分支开发完成后,很可能有一堆commit,但是合并到主干的时候,往往希望只有一个(或最多两三个)commit,这样不仅清晰,也容易管理。

那么,怎样才能将多个commit合并呢?这就要用到 git rebase 命令。
 

1
$ git rebase -i origin/production_29

git rebase命令的i参数表示互动(interactive),这时git会打开一个互动界面,进行下一步操作。

下面采用Tute Costa的例子,来解释怎么合并commit。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
pick 07c5abd Introduce OpenPGP and teach basic usage
pick de9b1eb Fix PostChecker::Post#urls
pick 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

上面的互动界面,先列出当前分支最新的4个commit(越下面越新)。

每个commit前面有一个操作命令,默认是pick,表示该行commit被选中,要进行rebase操作。

4个commit的下面是一大堆注释,列出可以使用的命令。

pick:正常选中
reword:选中,并且修改提交信息;
edit:选中,rebase时会暂停,允许你修改这个commit(参考这里)
squash:选中,会将当前commit与上一个commit合并
fixup:与squash相同,但不会保存当前commit的提交信息
exec:执行其他shell命令  

上面这6个命令当中,squash和fixup可以用来合并commit。

先把需要合并的commit前面的动词,改成squash(或者s)。  

1
2
3
4
pick 07c5abd Introduce OpenPGP and teach basic usage
s de9b1eb Fix PostChecker::Post#urls
s 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend

这样一改,执行后,当前分支只会剩下两个commit。

第二行和第三行的commit,都会合并到第一行的commit。提交信息会同时包含,这三个commit的提交信息。

1
2
3
4
5
6
7
8
9
# This is a combination of 3 commits.
# The first commit's message is:
Introduce OpenPGP and teach basic usage
# This is the 2nd commit message:
Fix PostChecker::Post#urls
# This is the 3rd commit message:
Hey kids, stop all the highlighting

如果将第三行的squash命令改成fixup命令。

1
2
3
4
pick 07c5abd Introduce OpenPGP and teach basic usage
s de9b1eb Fix PostChecker::Post#urls
f 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend

运行结果相同,还是会生成两个commit,第二行和第三行的commit,都合并到第一行的commit。

但是,新的提交信息里面,第三行commit的提交信息,会被注释掉。

1
2
3
4
5
6
7
8
9
# This is a combination of 3 commits.
# The first commit's message is:
Introduce OpenPGP and teach basic usage
# This is the 2nd commit message:
Fix PostChecker::Post#urls
# This is the 3rd commit message:
# Hey kids, stop all the highlighting

第六步:推送到远程仓库

合并commit后,就可以推送当前分支到远程仓库了。

1
$ git push --force origin 1-feature-name

git push命令要加上force参数,因为rebase以后,分支历史改变了,跟远程分支不一定兼容,有可能要强行推送(参见这里)。

第七步:发出Merge Request

提交到远程仓库以后,就可以发出 Merge Request 到Production_29分支,然后请求别人进行代码review,并部署到Staging Server测试。

第八步: 部署

部署到Production Server。

如果一切顺利,合并到Production_29,如果有问题,用production_29的代码回滚。

怎么样在github pages上搭建Jekyll博客?

发表于 2016-04-02   |   分类于 method   |  

0. 最终效果

请看JekyII blog http://duranchen.github.io

博客的主要是是功能:

  1. 写博客
  2. 可评论

下面先建立博客,然后加评论功能。

1. 在github上创建一个Repo

在浏览中登陆github,创建一个新Repo,命名为’username.github.io’,这里username就是你在github的账户名。

2. 安装JekyII

请参考setting-up-your-github-pages-site-locally-with-jekyll

如果有错误,需要安装ruby DevKit,请参考https://github.com/oneclick/rubyinstaller/wiki/Development-Kit

3. 配置 Jekyll

请参考https://help.github.com/articles/configuring-jekyll/

1
2
3
4
5
6
7
github: [metadata]
kramdown:
input: GFM # Enable GitHub Flavored Markdown (fenced code blocks)
hard_wrap: false
gems:
- jekyll-coffeescript
- jekyll-paginate

特别注意
input: GFM # Enable GitHub Flavored Markdown (fenced code blocks)

4. 如何写博客

  1. Jekyll Post Format, 请参考http://jekyllrb.com/docs/frontmatter/

    1
    2
    3
    4
    5
    6
    ---
    title: This is my title
    layout: post
    ---
    Here is my page (GitHub Flavored Markdown ).
  2. Post内容写法,请参考GitHub Flavored Markdown写法。https://guides.github.com/features/mastering-markdown/

5. 添加comments功能

请参考https://help.disqus.com/customer/portal/articles/472138-jekyll-installation-instructions

  1. 在https://disqus.com/admin/signup/ 注册一个账户和讨论站点。

  2. Add a variable called comments to the YAML Front Matter and set its value to true. A sample front matter might look like:

    1
    2
    3
    4
    5
    ...
    layout: default
    comments: true
    # other options
    ...
  3. In between a tag, add theUniversal Embed Code in the appropriate template where you’d like Disqus to load.

    Comments can be disabled per-page by setting comments: false or by not including the comments option at all.

6.本地预览

1
$ bundle exec jekyll serve
1…67
陈群

陈群

ever-growing

65 日志
6 分类
11 标签
© 2016 陈群
由 Hexo 强力驱动
主题 - NexT.Pisces