Chinaunix首页 | 论坛 | 博客
  • 博客访问: 449854
  • 博文数量: 64
  • 博客积分: 3271
  • 博客等级: 中校
  • 技术积分: 727
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-30 18:42
文章分类

全部博文(64)

文章存档

2013年(1)

2011年(19)

2010年(42)

2009年(2)

分类: SQLite/嵌入式数据库

2011-08-21 22:03:11

bind 的使用。

  1. #include "sqlite3.h"
  2. #include <stdlib.h>

  3. int main( int argc, char **argv )
  4. {
  5.     char *file = "test.db"; /* default to temp db */
  6.     sqlite3 *db = NULL;
  7.     sqlite3_stmt *stmt = NULL;
  8.     int rc = 0;
  9.     int idx = -1;


  10.     sqlite3_initialize( );
  11.     rc = sqlite3_open_v2( file, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
  12.     if ( rc != SQLITE_OK) {
  13.         sqlite3_close( db );
  14.         exit( -1 );
  15.     }

  16.     char *data[]={
  17.         "hello1",
  18.         "hello2",
  19.         "hello3",
  20.         "hello4",
  21.         "hello5",
  22.         "hello6",
  23.         "world7",
  24.     };
  25.     char *str[]={
  26.         "str3",
  27.         "str4",
  28.         "str5",
  29.         "str6",
  30.         "str7",
  31.         "str8",
  32.         "str9",
  33.     };
  34.     rc = sqlite3_prepare_v2( db, "INSERT INTO tbl(data, str) VALUES (:data, :str )", -1, &stmt, NULL );
  35.     if ( rc != SQLITE_OK) exit( -1 );
  36.     int i;
  37.     for(i = 0; i < 7; i++){
  38.         idx = sqlite3_bind_parameter_index( stmt, ":data" );
  39.         sqlite3_bind_text( stmt, idx, data[i], -1, SQLITE_STATIC );
  40.         idx = sqlite3_bind_parameter_index( stmt, ":str" );
  41.         sqlite3_bind_text( stmt, idx, str[i], -1, SQLITE_STATIC );
  42.         rc = sqlite3_step( stmt );
  43.         if (( rc != SQLITE_DONE )&&( rc != SQLITE_ROW )) exit ( -1 );
  44.         sqlite3_reset(stmt);
  45.     }
  46.     sqlite3_finalize( stmt );

  47.     



  48.     sqlite3_close( db );
  49.     sqlite3_shutdown( );
  50. }

阅读(2741) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~