httpd 安装 、sqlite3安装 学习笔记

前端之家收集整理的这篇文章主要介绍了httpd 安装 、sqlite3安装 学习笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
httpd:------------------------------------------------------------

sudo apt-get install httpd 或是找httpd 安装包

找到后,做如下操作,一般安装包一类的操作都差不多这样做;

./configure -prefix=/var/httpd

make
sudo make install


sudo vim /var/httpd/conf/httpd.conf

listen 8080
user akaedu
group akaedu
servername 127.0.0.1:8080


/bin下

sudo ./httpd -k start

http;//127.0.0.1:8080

/var/httpd/httpdoc/下就可以写我们自己的服务文件了;


sqllite3:------------------------------------------------------------

sudo apt-get install libsqlite3-dev
sudo apt-get install sqlite3
sudo apt-get install rlwrap // 是一个可以支持上下键的软件

rlwrap sqlite3 test.db

.bashrc //改个好用的别名
alias sqlite3='rlwrap sqlite3'
export PS1='-->'

sqlite3 m.db //在打开一个文件的时候如果当前目录没有后面的这个m.db的话,会自己建一个m.db 的database;

create table student(id integer,name text,age integer,score integer); //建表

.table //看表

.schema student 看表里面当前有多少字段;

记录内容的操作:
insert into student(id,name,age,score) values(1,'xiaoMing',21,100);

not null,primary key,unique,

select * from student;

delete from student;
.import data.txt student; 自己写字txt文件可以把它导入进来。注意内容要以|分格

update student set age=27 where id=2;

delete from student where id =1;

name like 'X%' //支持like 等操作
age between 22 and 25;

distinct name //过滤相同的

order by id desc/asc
score+socre2
select * from sapplay union all select * from custom;  两表连查

2.
create table employee(id integer primary key,depart text,salary integer);
select sum(salary) from employee;

select depart,sum(salary) from employee group by depart;

select depart,sum(salary) from employee group by depart having sum(salary)>3000;

select depart,sum(salary) from employee where id<5 group by depart having sum(salary) >5000;



表的修改
alter table student add score2 integer;

alter table student modify score2 text;(不一定支持

drop table student;

create table employee_bak as select * from employee;//bak

上前的内容基本上和我们一般用的sql差不多,看看那个这个也就会了,不过有的时候我们这个小sql不支持;

下面是用sqlite3 在C语言下的操作:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sqlite3.h>
  4. int rscallback(void *p,int argc,char **argv,char **argvv)
  5. {
  6. int i;
  7. *(int *)p = 0;
  8. for(i=0; i<argc; i++) {
  9. printf("%s=%s ",argvv[i],argv[i]?argv[i]:"NULL");
  10. }
  11. putchar('\n');
  12. return 0;
  13. }
  14. int main(void)
  15. {
  16. sqlite3 *db;
  17. char *err = 0;
  18. int ret = 0;
  19. int empty = 1;
  20.  
  21. ret = sqlite3_open("./test.db",&db);
  22. if(ret != sqlITE_OK) {
  23. fputs(sqlite3_errmsg(db),stderr);
  24. fputs("\n",stderr);
  25. exit(1);
  26. }
  27. /*ret = sqlite3_exec(db,"insert into employee values(12,\"www\",\"male\",31);",rscallback,∅,&err);
  28. if(ret != sqlITE_OK) {
  29. fputs(err,stderr);
  30. sqlite3_close(db);
  31. exit(1);
  32. }*/
  33. /* if(empty) {
  34. fputs("table employee is empty\n",stderr);
  35. exit(1);
  36. }*/
  37. ret = sqlite3_exec(db,"select * from employee;",stderr);
  38. sqlite3_close(db);
  39. exit(1);
  40. }
  41. if(empty) {
  42. fputs("table employee is empty\n",stderr);
  43. exit(1);
  44. }
  45. sqlite3_close(db);
  46. return 0;
  47. }

动态实现insert update

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sqlite3.h>
  4. static int sort_callback(void *p,char **argvv)
  5. {
  6. int i;
  7. int time = *(int *) p;
  8. time++;
  9. *(int *) p = time;
  10. printf("[%d]: ",time);
  11. for(i = 0; i < argc; i++)
  12. printf("%8s ",argv[i]? argv[i]: "NULL");
  13. printf("\n");
  14. return 0;
  15. }
  16.  
  17. int do_insert(sqlite3 *db)
  18. {
  19. int ret = 0;
  20. char name[10];
  21. char gender[10];
  22. char score_a[10];
  23. char score_b[10];
  24. char score_c[10];
  25. char *sql = NULL;
  26. printf("Input Name:\n");
  27. scanf("%s",name);
  28. printf("Input Gender:\n");
  29. scanf("%s",gender);
  30. printf("Input score A:\n");
  31. scanf("%s",score_a);
  32. printf("Input score B:\n");
  33. scanf("%s",score_b);
  34. printf("Input score C:\n");
  35. scanf("%s",score_c);
  36.  
  37. printf("Name:%s,Gender:%s,score A:%s,score B:%s,score C:%s\n",gender,score_a,score_b,score_c);
  38.  
  39. sql = sqlite3_mprintf("insert into student values(%Q,%Q,%s,%s)",score_c);
  40.  
  41. ret = sqlite3_exec(db,sql,NULL,NULL);
  42. sqlite3_free(sql);
  43. if(ret != sqlITE_OK) {
  44. fputs("insert error\n",stderr);
  45. }
  46. return ret;
  47. }
  48.  
  49. int do_delete(sqlite3 *db)
  50. {
  51. int ret = 0;
  52. char name[10];
  53. char *sql = NULL;
  54.  
  55. printf("Input Name:\n");
  56. scanf("%s",name);
  57.  
  58. sql = sqlite3_mprintf("delete from student where name=%Q",name);
  59. ret = sqlite3_exec(db,NULL);
  60. sqlite3_free(sql);
  61. if(ret != sqlITE_OK) {
  62. fputs("delete error\n",stderr);
  63. }
  64. return ret;
  65. }
  66.  
  67. int do_update(sqlite3 *db)
  68. {
  69. int ret = 0;
  70. char name[10];
  71. char gender[10];
  72. char score_a[10];
  73. char score_b[10];
  74. char score_c[10];
  75. char *sql = NULL;
  76. printf("Input Name:\n");
  77. scanf("%s",score_c);
  78.  
  79. sql = sqlite3_mprintf("update student set "
  80. "gender=%Q,scoreA=%s,scoreB=%s,"
  81. "scoreC=%s where name=%Q",score_c,name);
  82.  
  83. ret = sqlite3_exec(db,NULL);
  84. sqlite3_free(sql);
  85. if(ret != sqlITE_OK) {
  86. fputs("update error\n",stderr);
  87. }
  88. return ret;
  89. }
  90.  
  91. int do_sort(sqlite3 *db)
  92. {
  93. int ret = 0;
  94. int time = 0;
  95. ret = sqlite3_exec(db,"select name,scoreA,"
  96. "scoreB,scoreC,scoreA+scoreB+scoreC "
  97. "from student order by scoreA+scoreB+scoreC desc",sort_callback,&time,NULL);
  98. if(ret != sqlITE_OK) {
  99. fputs("sort error\n",stderr);
  100. }
  101. return ret;
  102. }
  103.  
  104. void displaymenu(void)
  105. {
  106. printf("1. Insert Record\n");
  107. printf("2. Delete Record\n");
  108. printf("3. Update Record\n");
  109. printf("4. Sort\n");
  110. printf("q. quit\n");
  111. printf("Please Select [1-4]:\n");
  112. return;
  113. }
  114.  
  115. int main(void)
  116. {
  117. sqlite3 *db;
  118. char *err = 0;
  119. int ret = 0;
  120. int empty = 1;
  121. int options = 0;
  122. int donext = 1;
  123.  
  124. ret = sqlite3_open("./test.db",&db);
  125. if(ret != sqlITE_OK) {
  126. fputs(sqlite3_errmsg(db),stderr);
  127. fputs("\n",stderr);
  128. exit(1);
  129. }
  130. displaymenu();
  131. while(donext)
  132. {
  133. options = getchar();
  134. switch(options){
  135. case '1':
  136. {
  137. do_insert(db);
  138. displaymenu();
  139. }
  140. break;
  141. case '2':
  142. {
  143. do_delete(db);
  144. displaymenu();
  145. }
  146. break;
  147. case '3':
  148. {
  149. do_update(db);
  150. displaymenu();
  151. }
  152. break;
  153. case '4':
  154. {
  155. do_sort(db);
  156. displaymenu();
  157. }
  158. break;
  159. case 'q':
  160. {
  161. donext = 0;
  162. }
  163. break;
  164. default:
  165. {
  166. }
  167. break;
  168. }
  169. }
  170. sqlite3_close(db);
  171. return 0;
  172. }
由于用到sqlITE库,所以编译的时候要加-lsqlite3

猜你在找的Sqlite相关文章