一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net C sqlite数据库的方法

时间:2011-05-23 编辑:简简单单 来源:一聚教程网

asp教程.net C sqlite数据库教程的方法

需要一个SQLite的引擎啊,有个System.Data.SQLite,添加到项目引用之后就可以用了,给你个简单的参考:
  SQLiteConnection mycon = new SQLiteConnection(@"data source=dbPerson.db3");
  mycon.Open();
  SQLiteCommand cmd = mycon.CreateCommand();
  cmd.CommandText = @"select * from person";
  SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
  DataSet ds = new DataSet();
  da.Fill(ds);
  dataGridView1.DataSource = ds.Tables[0];
  mycon.Close();

你需要下载sqlite的源代码
    http://www.sqlite.org/sqlite-3.6.6.2.tar.gz
    #tar xf sqlite-3.6.6.2.tar.gz
    #cd sqlite-3.6.6.2.tar.gz
    #./configure prefix=/usr
    #make
    #make install

    然后。。就可以开始第一步尝试了。在c中访问sqlite数据库
  
    c代码如下:

    #include
    #include
    #include
    int main( void )
    {
        sqlite3 *db=NULL;
        char *zErrMsg = 0;
        int rc;
        rc = sqlite3_open("zieckey.db", &db);
        if( rc )
        {
            fprintf(stderr, "Can't open sqlite: %sn", sqlite3_errmsg(db));
            sqlite3_close(db);
            exit(1);
        }
      else printf("open sqlite successn");
        sqlite3_close(db); //关闭数据库
        return 0;
     }
  
   将此文件另存为sql.c
     可以链接sqlite动态库
     #gcc sql.c -lsqlite3 -o sql
     也可以直接连接静态库
   #gcc sql.c /usr/lib/libsqlite3.a -lpthread -o sql

   执行
     #./sql
     会显示
     open sqlite success

     恭喜你。sqlite可以正常工作了

下面可以工作了,我们看更详细的做法

// checkusername.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

//sqlite3_exec的重载,避免穿那么多用不到的参数(纯C没有重载)
SQLITE_API int sqlite3_exec(sqlite3* db,const char *sql)
{
 char *zErrMsg = 0;
 return sqlite3_exec(db, sql, NULL, NULL, &zErrMsg);
}

int _tmain(int argc, _TCHAR* argv[])
{
 printf("Contenttype:text/htmlnn"); //根据HTTP协议,这里一定要有个空行。

 sqlite3 *db;
    int rc;
    rc = sqlite3_open("D:greeninstalltinywebserverwwwrootrp.db3", &db);
    if( rc!=SQLITE_OK  )
 {
        printf( "Can't open database: %sn", sqlite3_errmsg(db));
        sqlite3_close(db);
        return -1;
    }
 /*rc = sqlite3_exec(db, "Insert into T_User(username,password) values('admin','123')");
 if( rc !=SQLITE_OK )
 {
        printf("Can't open database: %sn", sqlite3_errmsg(db));
        sqlite3_close(db);
        return -1;
    }*/

    sqlite3_stmt *pStmt;
   
    //建立过程
 rc = sqlite3_prepare(db, "select * from T_User where password=?", -1, &pStmt, 0);
    if(rc != SQLITE_OK){
        printf( "execute sql error: %sn", sqlite3_errmsg(db));
        sqlite3_close(db);
        return -1;
    }
   
    //绑定参数
 if(sqlite3_bind_text(pStmt, 1, "123",-1,SQLITE_STATIC) != SQLITE_OK){
        printf( "sqlite3_bind_int error: %sn", sqlite3_errmsg(db));
        goto test_exit;
    }
   
    // 多次执行过程
    while(sqlite3_step(pStmt)!=SQLITE_DONE){
  const unsigned char* name = sqlite3_column_text(pStmt,1);
     const unsigned char* password = sqlite3_column_text(pStmt,2);
  printf("%s=%sn",name,password);
    }
   
test_exit:   
    if(sqlite3_finalize(pStmt) != SQLITE_OK){
        printf( "testPrepareStmt-sqlite3_finalize");
    }

    sqlite3_close(db);
 printf("ok");
 return 0;
}

热门栏目