SQLite3的C编程

本文介绍了一个使用C语言编写的简单示例程序,演示如何利用SQLite进行数据库连接、创建表及批量插入数据的操作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*=================================
.*  The Standard include file.
.*
.*===============================*/
#include <stdio.h>
#include <stdlib.h>

/*=================================
.*
.*  The extend include file.
.*
.*===============================*/
#include "sqlite3.h"
/* #include "sqlite3ext.h"  */

int main()
{
     /* Connect SQLite system. */
     sqlite3 *pDatabase = NULL;
     int result;
     char sql[500];
     char *err_msg = NULL;
     int i;

     result = sqlite3_open("test.db3", &pDatabase);
     if( result != SQLITE_OK ) {
          printf("Failure to open the database./n");
          return -1;
     } else
          printf("Good to open the database./n");

     sprintf(sql,"BEGIN");
     sqlite3_exec(pDatabase, sql,0,0,err_msg);
     sprintf(sql, "CREATE TABLE [TestDB] (/
                   [id] int, [name] varchar(20), [age] int)");
     /*
     if (SQLITE_OK != sqlite3_exec(pDatabase, sql, 0, 0, &err_msg)) {
          printf("operate failed: %s./n", err_msg);
          return -1;
     }*/
     for(i = 0; i < 10000; i++) {
          sprintf(sql, "INSERT INTO [TestDB] ([id], [name], [age]) /
                        VALUES (%d, '%s', %d)", i, "JGood", i);
          sqlite3_exec(pDatabase, sql, 0, 0, &err_msg);
     }

     sprintf(sql,"END");
     sqlite3_exec(pDatabase,sql,0,0,err_msg);
     sqlite3_close(pDatabase);

     return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值