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语言下的操作:

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
 
int rscallback(void *p,int argc,char **argv,char **argvv)
{
    int i;
    *(int *)p = 0;
    for(i=0; i<argc; i++) {
        printf("%s=%s ",argvv[i],argv[i]?argv[i]:"NULL");
    }
    putchar('\n');
    return 0;
}
 
int main(void)
{
    sqlite3 *db;
    char *err = 0;
    int ret = 0;
    int empty = 1;

    ret = sqlite3_open("./test.db",&db);
    if(ret != sqlITE_OK) {
                fputs(sqlite3_errmsg(db),stderr);
                fputs("\n",stderr);
                exit(1);
    }
    /*ret = sqlite3_exec(db,"insert into employee values(12,\"www\",\"male\",31);",rscallback,∅,&err);
    if(ret != sqlITE_OK) {
                fputs(err,stderr);
                sqlite3_close(db);
                exit(1);
           }*/
/*           if(empty) {
                fputs("table employee is empty\n",stderr);
                exit(1);
           }*/
    ret = sqlite3_exec(db,"select * from employee;",stderr);
                sqlite3_close(db);
                exit(1);
           }
           if(empty) {
                fputs("table employee is empty\n",stderr);
                exit(1);
           }
    sqlite3_close(db);
    return 0;
}

动态实现insert update

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
 
static int sort_callback(void *p,char **argvv)
{
    int i;
    int time = *(int *) p;     
    time++;
    *(int *) p = time;
          
    printf("[%d]:  ",time);
    for(i = 0; i < argc; i++)
    printf("%8s ",argv[i]? argv[i]: "NULL");
    printf("\n");     
    return 0;    
}

int do_insert(sqlite3 *db)
{
    int ret = 0;    
    char name[10];
    char gender[10];
    char score_a[10];
    char score_b[10];
    char score_c[10];
    char *sql = NULL;
    
    printf("Input Name:\n");
    scanf("%s",name);
    printf("Input Gender:\n");
    scanf("%s",gender);    
    printf("Input score A:\n");
    scanf("%s",score_a);    
    printf("Input score B:\n");
    scanf("%s",score_b);    
    printf("Input score C:\n");
    scanf("%s",score_c);    

    printf("Name:%s,Gender:%s,score A:%s,score B:%s,score C:%s\n",gender,score_a,score_b,score_c);

    sql = sqlite3_mprintf("insert into student values(%Q,%Q,%s,%s)",score_c);    

    ret = sqlite3_exec(db,sql,NULL,NULL);
    sqlite3_free(sql);    
    if(ret != sqlITE_OK) {
        fputs("insert error\n",stderr);
    }    
    return ret;
}    

int do_delete(sqlite3 *db)
{
    int ret = 0;    
    char name[10];
    char *sql = NULL;

    printf("Input Name:\n");
    scanf("%s",name);

    sql = sqlite3_mprintf("delete from student where name=%Q",name);
    ret = sqlite3_exec(db,NULL);
    sqlite3_free(sql);
    if(ret != sqlITE_OK) {
        fputs("delete error\n",stderr);
    }       
    
    return ret;
}

int do_update(sqlite3 *db)
{
    int ret = 0;
    char name[10];
    char gender[10];
    char score_a[10];
    char score_b[10];
    char score_c[10];
    char *sql = NULL;
    
    printf("Input Name:\n");
    scanf("%s",score_c);

    sql = sqlite3_mprintf("update student set "
         "gender=%Q,scoreA=%s,scoreB=%s,"
         "scoreC=%s where name=%Q",score_c,name);

    ret = sqlite3_exec(db,NULL);
    sqlite3_free(sql);    
    if(ret != sqlITE_OK) {
        fputs("update error\n",stderr);
    }   
    return ret;
}

int do_sort(sqlite3 *db)
{
    int ret = 0;
    int time = 0;
    
    ret  = sqlite3_exec(db,"select name,scoreA,"
           "scoreB,scoreC,scoreA+scoreB+scoreC "
           "from student order by scoreA+scoreB+scoreC desc",sort_callback,&time,NULL);          
    if(ret != sqlITE_OK) {
        fputs("sort error\n",stderr);
    }    
    return ret;
}

void displaymenu(void)
{
    printf("1. Insert Record\n");
    printf("2. Delete Record\n");
    printf("3. Update Record\n");
    printf("4. Sort\n");
    printf("q. quit\n");    
    printf("Please Select [1-4]:\n");
    return;    
}    

int main(void)
{
    sqlite3 *db;
    char *err = 0;
    int ret = 0;
    int empty = 1;
    int options = 0;
    int donext = 1;

    ret = sqlite3_open("./test.db",&db);
     
    if(ret != sqlITE_OK) {
        fputs(sqlite3_errmsg(db),stderr);
        fputs("\n",stderr);
        exit(1);
    }
    
    displaymenu();
    
    while(donext)
    {   
        options = getchar();
        switch(options){
            case '1':
            {
                do_insert(db);
                displaymenu();
            }
                break;        
            case '2':
            {
                do_delete(db);
                displaymenu();
            }
                break;        
            case '3':
            {
                do_update(db);
                displaymenu();
            }
                break;        
            case '4':
            {
                do_sort(db);
                displaymenu();
            }
                break;        
            case 'q':
            {
                donext = 0;
            }
                break;        
            default:
            {
            }
            break;        
        } 
    }
    
    sqlite3_close(db);
    return 0;
}
由于用到sqlITE库,所以编译的时候要加-lsqlite3 原文链接:https://www.f2er.com/sqlite/201973.html

猜你在找的Sqlite相关文章