对于任何一门语言,网络编程都是重要的一部分。对于Go来说其天生的高并发网络编程更是充满魅力。所以今天开学学习Go网络编程部分,教材是 Jan Newmarch 的 Network programming with Go 的pdf文档。 这份文档在 这里 可以找到,由 Astaxie 翻译的中文版也在这个网站的这个目录, 当然也可以在astaxie的github上找到。
The updates made so far by an open transaction are invisible to other transactions until the transaction completes, whereupon all the updates become visible simultaneously
PostgreSQL actually treats every SQL statement as being executed within a transaction. If you do not issue a BEGIN command, then each individual statement has an implicit BEGIN and (if successful) COMMIT wrapped around it. A group of statements surrounded by BEGIN and COMMIT is sometimes called a transaction block
这里还有一个叫做 savepoint 的概念,在mysql中没有这个东西,pg的事务处理中,可以设置savepoint,以允许设置rollback的位置。使用 roolback to savepoint 可以保留savepoint之前的修改。一个示例:
BEGIN; UPDATE accounts SET balance = balance - 100.00 WHERE name = 'Alice'; SAVEPOINT my_savepoint; UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Bob'; -- oops ... forget that and use Wally's account ROLLBACK TO my_savepoint; UPDATE accounts SET balance = balance + 100.00 WHERE name = 'Wally'; COMMIT;
# "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 127.0.0.1/32 ident # IPv6 local connections: host all all ::1/128 ident
CREATE TABLE weather ( id SERIAL primary key, -- same as auto_increments city varchar(80), temp_lo int, -- low temperature temp_hi int, -- high temperature prcp real, -- precipitation date date ); CREATE TABLE cities ( name varchar(80), location point );
pg支持位置的point数据类型,它是一对数据,代表经纬度,这样插入数据可以如下:
insert into cities values ('beijing', '(100.0, 36.0)');
或者要从txt文件导入数据,在mysql中,我们使用load data local infile ‘’ into table ** 这样的命令,pg使用copy命令导入:
copy table_name from '/path/to/file/of/server/'
一些对表结构的操作如下
# 添加栏位 ALTER TABLE user_tbl ADD email VARCHAR(40); # 更新结构 ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL; # 更名栏位 ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup; # 删除栏位 ALTER TABLE user_tbl DROP COLUMN email; # 表格更名 ALTER TABLE user_tbl RENAME TO backup_tbl; # 删除表格 DROP TABLE IF EXISTS backup_tbl;
select max(tmp_lo) from weather; select city from weather where tmp_lo = max(temp_lo); // 错误 select city from weather where temp_lo = (select max(temp_lo) from weather); // 正确
另一个常用的聚合就是group by了。没什么特殊的,和mysql同样的用法,注意group by 的条件子句使用 having condition 就可以了。
WHERE selects input rows before groups and aggregates are computed, whereas HAVING selects group rows after groups and aggregates are computed. Thus, the WHERE clause must not contain aggregate functions; it makes no sense to try to use an aggregate to determine which rows will be inputs to the aggregates. On the other hand, the HAVING clause always contains aggregate functions. (Strictly speaking, you are allowed to write a HAVING clause that doesn’t use aggregates, but it’s seldom useful. The same condition could be used more efficiently at the WHERE stage.)
ssh wuxu@10.4.16.95 MySQL -uroot -p // enter root password create user golang@'%' identified by 'golangMysql'; grant privileges on *.golang to golang@'%'; flush privileges;
这样就可以用golang登录新建的数据库了
Golang操作mysql
先get数据库驱动,在命令行运行go get github.com/go-sql-driver/MySQL,会自动使用git从github下载mysql驱动工程到$GOPATH/src/。