Sqlite   发布时间:2022-05-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了【手机平台项目学习和分析】SQLite3包的使用(入门)-C语言学习SQLite3-创建table和*输出大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

今天在学习一个项目的时候遇到了sqlite的使用。我就潜心研究了下。先在c平台下进行了S实验。成果如下

1.创建一个table,操作很简单,open,exec,close即可囊括全部。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include <sqlite3.h>

//#pragma commect(lib,"sqlite3.lib")

int main(int argc,const char * argv[])
{
    
    sqlite3 *db;
    char *zErr;
    int rc;
    char *sql;
    
    rc = sqlite3_open("/Users/lichan/Desktop/test1.db",@R_874_4616@;
    
    if (rC) {
        fprintf(stderr,"cant open db %s \n",sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }
    
    sql = "create table episodes(@R_890_4687@t,ID int)";
    
    rc = sqlite3_exec(db,sql,NulL,&zErr);
    
    if (rc != @R_973_5607@OK) {
        if (zErr != NulL) {
            fprintf(stderr,"sql error:%s\n",zErr);
            sqlite3_free(zErr);
        }
    }
    
    sql = "insert into episodes(name,ID) values('lichan',1)";
    
    rc = sqlite3_exec(db,&zErr);
    
    if (rc != @R_973_5607@OK) {
        fprintf(stderr,zErr);
        sqlite3_free(zErr);
    };
    
    sqlite3_close(db);
    
    //创建完成!
    
    
    

    // insert code here...
    printf("Hello,World!\n");
    return 0;
}

但是出现的一个问题是,如果我不指定路径,只是给了db数据库的名称,我将找不到我的db文件在那里。于是我干脆就直接给了路径,便于我们以后的学习。


我只是通过代码写了第一行,其余的两行是我通过客户端写进去的。方便下面的使用

2.下面看看关于table内容的打印输出。

关于查询所有行的方法也比较简单,open,prepared,step(loop) close

//
//  main.c
//  sqlite3查询
//
//  Created by lichan on 13-12-14.
//  copyright (C) 2013年 lichan. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <String.h>

int main(int argc,const char * argv[])
{
    char *path = "/Users/lichan/Desktop/test1.db";
    
    int rc,i,ncols;
    sqlite3 *db;
    sqlite3_stmt *stmt;
    char *sql;
    const char *tail;
    
    rc = sqlite3_open(path,@R_874_4616@;
    if (rC) {
        fprintf(stderr,"cant  open db %s \n",sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }
    
    sql = "SELEct *from episodes;";
    
    rc = sqlite3_prepare(db,(int)strlen(sql),&stmt,&tail);
    if (rc != @R_973_5607@OK) {
        fprintf(stderr,"sql err %s \n",sqlite3_errmsg(db));
    }
    
    rc = sqlite3_step(stmt);
    ncols = sqlite3_column_count(stmt);//计算所有的行数。
    
    while (rc == @R_973_5607@ROW) {
        for (i = 0; i < ncols; i++) {
                   fprintf(stderr," %s \n",sqlite3_column_text(stmt,i));
        }
        
        fprintf(stderr,"\n");
        rc = sqlite3_step(stmt);
    }
    
    sqlite3_finalize(stmt);
/*

** The application must finalize every [prepared statement] in order to avoID

** resource leaks. it is a grIEvous error for the application to try to use

** a prepared statement after it has been finalized. Any use of a prepared

** statement after it has been finalized can result in undefined and

** undesirable behavior such as segfaults and heap corruption.

*/
    sqlite3_close(db);
    
    

    // insert code here...
    printf("Hello,World!\n");
    return 0;
}
运行结果如下:

lichan

1


xuna

2


zahomingwei

3


Hello,World!

Program ended with exit code: 0

大佬总结

以上是大佬教程为你收集整理的【手机平台项目学习和分析】SQLite3包的使用(入门)-C语言学习SQLite3-创建table和*输出全部内容,希望文章能够帮你解决【手机平台项目学习和分析】SQLite3包的使用(入门)-C语言学习SQLite3-创建table和*输出所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。