🚙

💨 💨 💨

×

  • Categories

  • Archives

  • Tags

  • About

MySQL入门二之一些小注意点

Posted on 03-02-2015 | In DB

distinct关键字

distinct是应用于所有列的, 而不是某一个列

mysql> select * from test_table;
+------+------+
| one | two |
+------+------+
| 56 | 12 |
| 52 | 10 |
| 56 | 12 |
| 56 | 13 |
+------+------+
4 rows in set (0.00 sec)

mysql> select distinct one, two from test_table;
+------+------+
| one | two |
+------+------+
| 56 | 12 |
| 52 | 10 |
| 56 | 13 |
+------+------+
3 rows in set (0.00 sec)

mysql> select distinct one from test_table;
+------+
| one |
+------+
| 56 |
| 52 |
+------+
2 rows in set (0.00 sec)

. . .

MySQL入门一之增删查改与关联

Posted on 02-27-2015 | In DB

增删改查

  • INSERT INTO table_name (列1, 列2,…) VALUES (值1, 值2,….)

  • DELETE FROM 表名称 WHERE 列名称 = 值

  • UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值

  • SELECT 列名称 FROM 表名称

关联

SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据。

Join和Key概绍

有时为了得到完整的结果,我们需要从两个或更多的表中获取结果。我们就需要执行 join。
数据库中的表可通过键将彼此联系起来。主键(Primary Key)是一个列,在这个列中的每一行的值都是唯一的。在表中,每个主键的值都是唯一的。这样做的目的是在不重复每个表中的所有数据的情况下,把表间的数据交叉捆绑在一起。
请看 “Persons” 表:

Id_P LastName FirstName Address City
1 Adams John Oxford Street London
2 Bush George Fifth Avenue New York
3 Carter Thomas Changan Street Beijing

请注意,”Id_P” 列是 Persons 表中的的主键。这意味着没有两行能够拥有相同的 Id_P。即使两个人的姓名完全相同,Id_P 也可以区分他们。
接下来请看 “Orders” 表:

Id_O OrderNo Id_P
1 77895 3
2 44678 3
3 22456 1
4 24562 1
5 34764 65

请注意,”Id_O” 列是 Orders 表中的的主键,同时,”Orders” 表中的 “Id_P” 列用于引用 “Persons” 表中的人,而无需使用他们的确切姓名。
请留意,”Id_P” 列把上面的两个表联系了起来。

下面列出了您可以使用的 JOIN 类型,以及它们之间的差异。

  • JOIN(INNER JOIN): 如果左右表中都有至少一个匹配,则返回行
  • LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
  • RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
  • FULL JOIN: 只要其中一个表中存在匹配,就返回行

注 : JOIN使用on的, 而不是where.

使用Join(INNER JOIN)

除了上面的方法,我们也可以使用关键词 JOIN 来从两个表中获取数据。
如果我们希望列出所有人的定购,可以使用下面的 SELECT 语句:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
ORDER BY Persons.LastName

结果集:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678

使用Left Join

现在,我们希望列出所有的人,以及他们的定购 - 如果有的话。
您可以使用下面的 SELECT 语句:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
LEFT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

结果集:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678
Bush George - - - -

LEFT JOIN 关键字会从左表 (Persons) 那里返回所有的行,即使在右表 (Orders) 中没有匹配的行。

使用Right Join

现在,我们希望列出所有的定单,以及定购它们的人 - 如果有的话。
您可以使用下面的 SELECT 语句:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
RIGHT JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

结果集:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678
34764

RIGHT JOIN 关键字会从右表 (Orders) 那里返回所有的行,即使在左表 (Persons) 中没有匹配的行。

使用Full Join

现在,我们希望列出所有的人,以及他们的定单,以及所有的定单,以及定购它们的人。
您可以使用下面的 SELECT 语句:

SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName

结果集:

LastName FirstName OrderNo
Adams John 22456
Adams John 24562
Carter Thomas 77895
Carter Thomas 44678
Bush George
34764

FULL JOIN 关键字会从左表 (Persons) 和右表 (Orders) 那里返回所有的行。如果 “Persons” 中的行在表 “Orders” 中没有匹配,或者如果 “Orders” 中的行在表 “Persons” 中没有匹配,这些行同样会列出。

ProtoBuf的安装与使用

Posted on 02-23-2015 | In Misc

介绍

与 JSON 相比, Protobuf 的序列化和反序列化的速度更快,而且传输的数据会先压缩,
使得传输的效率更高些 。
Protobuf , 全称 Protocol Buffer , 是 Google 公司内部的混合语言数据标准,是一种轻便
高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。 它很适合做数据
存储或 RPC 数据交换格式 。 Protobuf是可用于通信协议、数据存储等领域的语言无关、平台
无关、可扩展的序列化结构数据格式 。

安装

谷歌的东西要想在大陆安装起来总是有点那啥, 你懂的.

需要的依赖

sudo apt-get install curl

sudo apt-get install autoconf autogen

sudo apt-get install libtool

安装步骤

下载自github的代码需要首先执行 $ ./autogen.sh 生成configure文件

注意autogen.sh 需要gtest包,默认是从 googletest.googlecode.com下载,国内需要翻墙才能访问,很多人问autogen.sh运行失败,这里我补充一下

修改一下autogen.sh , 将这段:

echo "Google Test not present.  Fetching gtest-1.5.0 from the web..."
curl http://googletest.googlecode.com/files/gtest-1.5.0.tar.bz2 | tar jx
mv gtest-1.5.0 gtest

修改为:

wget https://github.com/google/googletest/archive/release-1.5.0.tar.gz
tar xzvf release-1.5.0.tar.gz
mv googletest-release-1.5.0 gtest

再执行 autogen.sh,这样就不会报错了

$ ./configure
$ make
$ make check
$ make install

默认是装在

usr/local/bin
usr/local/lib,
usr/local/include 

检查是否安装成功

protoc --version

如果安装成功,会出现版本号 如

libprotoc 2.6.1

如果有问题,会输出错误内容, 最后我安装完成,用上述命令检查版本号时出现如下问题

protoc: error while loading shared libraries: libprotocbuf.so.9: cannot open shared

错误原因

protobuf的默认安装路径是/usr/local/lib,而/usr/local/lib不在ubuntu体系默认的LD_LIBRARY_PATH里,所以就找不到lib

解决办法 :

1 - 在 /etc/ld.so.conf.d/目录下创建文件 bprotobuf.conf文件,文件内容如下

/usr/local/lib

2 - 输入命令

sudo ldconfig

这时,再输入protoc --version就可以正常看到版本号了

使用

Writer.cpp
#include<iostream>
#include<fstream>
#include "Mymessage.pb.h"
using namespace std;
int main(){
Im::Content msg1;
msg1.set_id(101);
msg1.set_str("ggsmd");
fstream output("./log", ios::out | ios::trunc | ios::binary);
if (!msg1.SerializeToOstream(&output)) {
cerr << "Failed to write msg." << endl;
return -1;
}
return 0;
}
Reader.cpp
#include<iostream>
#include<fstream>
#include "Mymessage.pb.h"
using namespace std;
void ListMsg(const Im::Content & msg){
cout << msg.id() << endl;
cout << msg.str() << endl;
}
int main(int argc, char* argv[]){
Im::Content msg1;
fstream input("./log", ios::in | ios::binary);
if (!msg1.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
return -1;
}
ListMsg(msg1);
return 0;
}
makefile
INC=/usr/local/include
LIB=/usr/local/lib
lib=protobuf

all:Writer Reader

Writer.o:Writer.cpp
g++ -g -c Writer.cpp -I$(INC) -L$(LIB) -l$(lib)
Reader.o:Reader.cpp
g++ -g -c Reader.cpp -I$(INC) -L$(LIB) -l$(lib)

Writer:Writer.o Mymessage.pb.o
g++ -g -o Writer Writer.o Mymessage.pb.o -I$(INC) -L$(LIB) -l$(lib)
Reader:Reader.o Mymessage.pb.o
g++ -g -o Reader Reader.o Mymessage.pb.o -I$(INC) -L$(LIB) -l$(lib)
Mymessage.pb.o:Mymessage.pb.cc
g++ -g -c Mymessage.pb.cc -I$(INC) -L$(LIB) -l$(lib)

clean:Writer Reader Writer.o Reader.o Mymessage.pb.o
rm Writer Reader Writer.o Reader.o Mymessage.pb.o
Mymessage.proto
package Im; 
message Content
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
}

打印结果

执行
protoc -I=./ --cpp_out=./ Mymessage.proto
命令后,会生成 Mymessage.pb.h 和 Mymessage.pb.cc 文件。 再执行 make 命令,生成
Writer 和 Reader 文件 。 执行 ./Writer 命令后,再执行./Reader 命令,终端上输出:

b@b-VirtualBox:~/tc$ protoc -I=./ --cpp_out=./ Mymessage.proto
b@b-VirtualBox:~/tc$ ll
total 44
drwxrwxr-x 2 b b  4096  5月 19 22:43 ./
drwxr-xr-x 4 b b  4096  5月 19 22:35 ../
-rw-rw-r-- 1 b b   647  5月 19 22:36 makefile
-rw-rw-r-- 1 b b 12214  5月 19 22:43 Mymessage.pb.cc
-rw-rw-r-- 1 b b  7762  5月 19 22:43 Mymessage.pb.h
-rw-rw-r-- 1 b b   161  5月 19 22:36 Mymessage.proto
-rw-rw-r-- 1 b b   421  5月 19 22:36 Reader.cpp
-rw-rw-r-- 1 b b   340  5月 19 22:35 Writer.cpp

b@b-VirtualBox:~/tc$ make
g++ -g -c Writer.cpp -I/home/sharexu/charpter13/1302/include -L/home/sharexu/charpter13/1302/lib -lprotobuf
g++ -g -c Mymessage.pb.cc -I/home/sharexu/charpter13/1302/include -L/home/sharexu/charpter13/1302/lib -lprotobuf    
g++ -g -o Writer Writer.o Mymessage.pb.o -I/home/sharexu/charpter13/1302/include -L/home/sharexu/charpter13/1302/lib -lprotobuf
g++ -g -c Reader.cpp -I/home/sharexu/charpter13/1302/include -L/home/sharexu/charpter13/1302/lib -lprotobuf    
g++ -g -o Reader Reader.o Mymessage.pb.o -I/home/sharexu/charpter13/1302/include -L/home/sharexu/charpter13/1302/lib -lprotobuf

b@b-VirtualBox:~/tc$ ll
total 772
drwxrwxr-x 2 b b   4096  5月 19 22:43 ./
drwxr-xr-x 4 b b   4096  5月 19 22:35 ../
-rw-rw-r-- 1 b b    647  5月 19 22:36 makefile
-rw-rw-r-- 1 b b  12214  5月 19 22:43 Mymessage.pb.cc
-rw-rw-r-- 1 b b   7762  5月 19 22:43 Mymessage.pb.h
-rw-rw-r-- 1 b b 244112  5月 19 22:43 Mymessage.pb.o
-rw-rw-r-- 1 b b    161  5月 19 22:36 Mymessage.proto
-rwxrwxr-x 1 b b 188430  5月 19 22:43 Reader*
-rw-rw-r-- 1 b b    421  5月 19 22:36 Reader.cpp
-rw-rw-r-- 1 b b  57656  5月 19 22:43 Reader.o
-rwxrwxr-x 1 b b 184244  5月 19 22:43 Writer*
-rw-rw-r-- 1 b b    340  5月 19 22:35 Writer.cpp
-rw-rw-r-- 1 b b  59232  5月 19 22:43 Writer.o

b@b-VirtualBox:~/tc$ ./Writer    

b@b-VirtualBox:~/tc$ ./Reader 
101
ggsmd

C++对象模型之虚函数实例讲解

Posted on 02-21-2015 | In Misc

介绍

因为c++只规定了 虚继承/ 虚函数/ 多继承/ 的行为, 但将实现方法留给编译器作者. 所以各个平台的实现并不相同, 得出的结果也不尽相同.

经测试, vs和gcc目前比较统一的情况只有2种 :

  • 无继承+无虚函数
  • 无继承+虚函数

故本文只讨论这2种, 以及了解虚函数和虚继承的含义.

. . .

python中的__name__和__main()__

Posted on 02-10-2015 | In Misc
#hello.py
def sayHello():
str="hello"
print(str);

if __name__ == "__main__":
print ('This is main of module "hello.py"')
sayHello()

python作为一种脚本语言,我们用python写的各个module都可以包含以上那么一个累死c中的main函数,只不过python中的这种__main__与c中有一些区别,类似于php的魔术那一套, 主要体现在:

1、当单独执行该module时,比如单独执行以上hello.py: python hello.py,则输出

This is main of module "hello.py"
hello

可以理解为"if __name__=="__main__":"这一句与c中的main()函数所表述的是一致的,即作为入口;

2、当该module被其它module 引入使用时,其中的"if __name__=="__main__":"

所表示的Block不会被执行,

这是因为此时module被其它module引用时,

其__name__的值将发生变化,__name__的值将会是module的名字。

比如在python shell中import hello后,查看hello.__name__:

import hello
hello.__name__
'hello'

3、因此,在python中,当一个module作为整体被执行时,moduel.name的值将是"__main__";

而当一个module被其它module引用时,module.__name__将是module自己的名字,

当然一个module被其它module引用时,其本身并不需要一个可执行的入口main了。

条件编译指令之#if和#ifdef和#if defined之间的区别

Posted on 02-09-2015 | In Misc

#if的使用说明

#if的后面接的是表达式 :

#if (MAX==10)||(MAX==20)
code...
#endif

它的作用是:如果(MAX==10)||(MAX==20)成立,那么编译器就会把其中的#if 与 #endif之间的代码编译进去(注意:是编译进去,不是执行!!)

#if defined的使用

#if后面接的是一个宏, 而#if define(x)的使用如下 :

#if defined (x)
...code...
#endif

这个#if defined它不管里面的“x”的逻辑是“真”还是“假”它只管这个程序的前面的宏定义里面有没有定义“x”这个宏,如果定义了x这个宏,那么,编译器会编译中间的…code…否则不直接忽视中间的…code…代码。
另外 #if defined(x)也可以取反,也就用 #if !defined(x)

#ifdef的使用

  • #ifdef的使用和#if defined()的用法一致
  • #ifndef又和#if !defined()的用法一致。

最后强调两点:

  • 这几个宏定义只是决定代码块是否被编译!
  • 别忘了#endif

Lua特别之处笔记

Posted on 02-03-2015 | In Misc

2.2 Booleans

两个取值 false 和 true。但要注意 Lua 中所有的值都可以作为条件。在控制结构的条
件中除了 false 和 nil 为假,其他值都为真。所以 Lua 认为 0 和空串都是真。

2.5 table

我们用一个疑问来引入table的介绍

传的是值还是引用?

lua的函数调用传的是值还是引用?

. . .

GDB基础教程

Posted on 02-02-2015 | In Linux

GDB 操作提示

在编译可执行文件时需要给 gcc 加上 “-g” 选项,这样它才会为生成的可执行文件加入额外的调试信息。
不要使用编译器的优化选项,比如: “-O”,”-O2”。因为编译器会为了优化而改变程序流程,那样不利于调试。
在 GDB 中执行 shell 命令可以使用:shell command
GDB 命令可以使用 TAB 键来补全。按两次 TAB 键可以看到所有可能的匹配。
GDB 命令缩写:例如 info bre 中的 bre 相当于 breakpoints。

启动GDB

  • gdb executable
  • gdb -e executable -c core-file
  • gdb executable -pid process-id (使用ps相关命令可以查看进程的 pid)

GDB常用命令

  • help 列出 gdb 帮助信息。
  • info+subcommand , 比如 :
    • info breakpoints : 列出断点。
    • info watchpoints : 列出观察点。
    • info threads : 列出当前的线程。
    • info locals : 列出Local variables of current stack frame
  • step(简写一个s也可) 进入下一行代码的执行,会进入函数内部。
  • next(简写一个n也可) 执行下一行代码。但不会进入函数内部。
  • finish 跳出当前代码(之前跳入调试)
  • continue(c) 继续执行直到下一个断点或观察点。
  • b 断点
  • kill 停止程序执行。
  • quit(q) 退出 GDB调试器
  • run(r) 从头开始执行程序,也允许进行重定向。
  • print(p) variable 打印指定变量的值。
    • p variable
    • p file::variable
    • p ‘file’::variables
  • backtrace(简写bt), 显示函数调用栈
    • bt : 显示所有函数调用栈
    • bt n : 显示程序的调用栈信息,只显示栈顶n桢(frame)
    • bt -n : 显示程序的调用栈信息,只显示栈底部n桢(frame)
    • set backtrace limit n : 设置bt显示的最大桢层数
    • where 和 info stack : 都是bt的别名,功能一样

细说断点

给 test.c 的第10行设置一个断点 :

b test.c:10 

断点删除

断点的删除与断点的设置同样的重要。删除断点的命令有两个:

  • delete
  • clear

delete

用法:delete [breakpoints num] [range…]
delete可删除单个断点,也可删除一个断点的集合,这个集合用连续的断点号来描述。
例如:

delete 5
delete 1-10

clear

用法:

  • clear 删除所选定的环境中所有的断点
  • clear location location描述具体的断点。

clear 删除断点是基于行的,不是把所有的断点都删除。
例如:

clear list_insert         //删除函数的所有断点
clear list.c:list_delet   //删除文件:函数的所有断点
clear 12                  //删除行号的所有断点
clear list.c:12           //删除文件:行号的所有断点

断点的使能和禁止

对断点的控制除了建立和删除外,还可以通过使能和禁止来控制,后一种方法更灵活。

断点的四种使能操作:

  • enable [breakpoints] [range…] 完全使能
  • enable //激活所有断点
  • enable 4 //激活4断点
  • enable 5-6 //激活5~6断点
  • disable [breakpoints] [range…] 禁止
  • enable once [breakpoints] [range…] 使能一次,触发后禁止
  • enable delete [breakpoints] [range…]使能一次,触发后删除

用法举例:

diable                //禁止所有断点
disble 2            //禁止第二个断点
disable 1-5            //禁止第1到第5个断点

GDB帮助

GDB的命令很多, 有些用得少的命令记不住的话, 可以在进入GDB之后敲 “help”, 然后再敲 “help + command_class”,
比如 :

(gdb) help
List of classes of commands:

aliases – Aliases of other commands
breakpoints – Making program stop at certain points
data – Examining data
files – Specifying and examining files
internals – Maintenance commands
obscure – Obscure features
running – Running the program
stack – Examining the stack
status – Status inquiries
support – Support facilities
tracepoints – Tracing of program execution without stopping the program
user-defined – User-defined commands

Type “help” followed by a class name for a list of commands in that class.
Type “help all” for the list of all commands.
Type “help” followed by command name for full documentation.
Type “apropos word” to search for commands related to “word”.
Command name abbreviations are allowed if unambiguous.

(gdb) help running
Running the program.

List of commands:

advance – Continue the program up to the given location (same form as args for break command)
attach – Attach to a process or file outside of GDB
continue – Continue program being debugged
detach – Detach a process or file previously attached
detach checkpoint – Detach from a checkpoint (experimental)
detach inferiors – Detach from inferior ID (or list of IDS)
disconnect – Disconnect from a target
finish – Execute until selected stack frame returns
handle – Specify how to handle signals
inferior – Use this command to switch between inferiors
interrupt – Interrupt the execution of the debugged program
jump – Continue program being debugged at specified line or address
kill – Kill execution of program being debugged
kill inferiors – Kill inferior ID (or list of IDs)
next – Step program
nexti – Step one instruction
reverse-continue – Continue program being debugged but run it in reverse
reverse-finish – Execute backward until just before selected stack frame is called
reverse-next – Step program backward
reverse-nexti – Step backward one instruction
reverse-step – Step program backward until it reaches the beginning of another source line
reverse-stepi – Step backward exactly one instruction
run – Start debugged program
…

1…232425262728293031323334353637
Mike

Mike

🚙 🚗 💨 💨 If you want to create a blog like this, just follow my open-source project, "hexo-theme-neo", click the GitHub button below and check it out ^_^ . It is recommended to use Chrome, Safari, or Edge to read this blog since this blog was developed on Edge (Chromium kernel version) and tested on Safari.

11 categories
291 posts
111 tags
about
GitHub Spotify
© 2013 - 2025 Mike