SQLite Hello Code

简介: 1: //------------------------------------------------------------------------------ 2: // Copyright (c) 2012 eryar All Rights Reserved.
 
  1: //------------------------------------------------------------------------------
  2: //	Copyright (c) 2012 eryar All Rights Reserved.
  3: //
  4: //		File    : Main.cpp
  5: //		Author  : eryar@163.com
  6: //		Date    : 2012-10-27 23:01
  7: //		Version : 0.1v
  8: //
  9: //	Description : Test SQLite program.
 10: //
 11: //==============================================================================
 12: 
 13: #include <stdio.h>
 14: #include "sqlite3.h"
 15: 
 16: // Query callback function.
 17: int QueryCallback(void* data, int colCount, char** colValues, char** colName);
 18: 
 19: int main(int argc, char* argv[])
 20: {
 21:     int iResultCode = 0;
 22:     char*   szErrorMsg  = NULL;
 23:     const char* szCreateTableSQL = "create table users(name varchar(20) primary key, age int, birthday datetime);";
 24:     
 25:     // Connect to the database.
 26:     sqlite3*    pDB = NULL;
 27: 
 28:     iResultCode = sqlite3_open("test.db", &pDB);
 29: 
 30:     if (iResultCode != SQLITE_OK)
 31:     {
 32:         fprintf(stderr, "Cann't open database: %s\n", sqlite3_errmsg(pDB));
 33:         return 1;
 34:     }
 35: 
 36:     printf("Connected to the database successfully!\n");
 37: 
 38: 
 39:     // Execute the create table command.
 40:     iResultCode = sqlite3_exec(pDB, szCreateTableSQL, 0, 0, &szErrorMsg);
 41:     if (iResultCode != SQLITE_OK)
 42:     {
 43:         fprintf(stderr, "SQL error: %s! \n", szErrorMsg);
 44: 
 45:         sqlite3_free(szErrorMsg);
 46:     }
 47: 
 48:     // Insert records to the database.
 49:     iResultCode = sqlite3_exec(pDB, "insert into users values('张三', 20, '2012-10-27');", 0, 0, &szErrorMsg);
 50:     if (iResultCode)
 51:     {
 52:         printf("Insert a record to the database.\n");
 53:     }
 54: 
 55:     // Query the database.
 56:     iResultCode = sqlite3_exec(pDB, "select * from users;", QueryCallback, 0, &szErrorMsg);
 57: 
 58:     sqlite3_close(pDB);
 59: 
 60:     return 0;
 61: }
 62: 
 63: // For every record will call the callback function.
 64: // N records will call the callback function N times.
 65: int QueryCallback( void* data, int colCount, char** colValues, char** colName )
 66: {
 67:     for (int i = 0; i < colCount; i++)
 68:     {
 69:         printf("%s = %s\n", colName[i], colValues[i] == 0 ? "NULL":colValues[i]);
 70:     }
 71: 
 72:     return 0;
 73: }
 74: 

 

输出结果:

 
  1: Connected to the database successfully!
  2: name = 张三
  3: age = 20
  4: birthday = 2012-10-27
  5: Press any key to continue
目录
相关文章
|
1月前
|
API 数据库 C语言
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
【C/C++ 数据库 sqlite3】SQLite C语言API返回值深入解析
170 0
|
3月前
|
存储 数据库连接 数据库
Android数据存储:解释SQLite数据库在Android中的使用。
Android数据存储:解释SQLite数据库在Android中的使用。
42 0
|
2月前
|
存储 监控 安全
内网屏幕监控软件的数据存储与管理:使用SQLite数据库保存监控记录和配置信息
在当今数字化时代,安全和监控在企业和组织中变得至关重要。内网屏幕监控软件作为一种关键工具,帮助组织监视员工的活动并确保信息安全。这种软件不仅需要高效地记录和管理监控数据,还需要能够方便地进行配置和调整。本文将讨论如何使用SQLite数据库来保存监控记录和配置信息,并介绍如何通过自动化机制将监控到的数据提交到指定网站。
165 2
|
12天前
|
SQL 关系型数据库 数据库
Python中SQLite数据库操作详解:利用sqlite3模块
【4月更文挑战第13天】在Python编程中,SQLite数据库是一个轻量级的关系型数据库管理系统,它包含在一个单一的文件内,不需要一个单独的服务器进程或操作系统级别的配置。由于其简单易用和高效性,SQLite经常作为应用程序的本地数据库解决方案。Python的内置sqlite3模块提供了与SQLite数据库交互的接口,使得在Python中操作SQLite数据库变得非常容易。
|
17天前
|
关系型数据库 MySQL 数据库连接
Python+SQLite数据库实现服务端高并发写入
Python中使用SQLite内存模式实现高并发写入:创建内存数据库连接,建立表格,通过多线程并发写入数据。虽然能避免数据竞争,但由于SQLite内存模式采用锁机制,可能在高并发时引发性能瓶颈。若需更高性能,可选择MySQL或PostgreSQL。
23 0
|
1月前
|
关系型数据库 数据库 C++
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
嵌入式数据库sqlite3【基础篇】基本命令操作,小白一看就懂(C/C++)
|
1月前
|
存储 SQL 数据库
django如何连接sqlite数据库?
django如何连接sqlite数据库?
48 0
|
2月前
|
SQL 数据库管理
sqlite语句order by两个字段同时排序处理
sqlite语句order by两个字段同时排序处理
21 0
|
2月前
|
SQL 关系型数据库 MySQL
Python中的数据库操作:SQLite与MySQL的连接
Python中的数据库操作:SQLite与MySQL的连接
124 0
|
2月前
|
SQL 存储 数据库
艺术型轻量级数据库 --Sqlite
艺术型轻量级数据库 --Sqlite

热门文章

最新文章