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
目录
相关文章
|
5月前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
484 0
|
10月前
|
SQL 数据库连接 数据库
在C++的QT框架中实现SQLite数据库的连接与操作
以上就是在C++的QT框架中实现SQLite数据库的连接与操作的基本步骤。这些步骤包括创建数据库连接、执行SQL命令、处理查询结果和关闭数据库连接。在实际使用中,你可能需要根据具体的需求来修改这些代码。
646 14
|
关系型数据库 MySQL 数据库
Python处理数据库:MySQL与SQLite详解 | python小知识
本文详细介绍了如何使用Python操作MySQL和SQLite数据库,包括安装必要的库、连接数据库、执行增删改查等基本操作,适合初学者快速上手。
1243 15
|
存储 SQL 数据库
数据库知识:了解SQLite或其他移动端数据库的使用
【10月更文挑战第22天】本文介绍了SQLite在移动应用开发中的应用,包括其优势、如何在Android中集成SQLite、基本的数据库操作(增删改查)、并发访问和事务处理等。通过示例代码,帮助开发者更好地理解和使用SQLite。此外,还提到了其他移动端数据库的选择。
388 8
|
Web App开发 SQL 数据库
使用 Python 解析火狐浏览器的 SQLite3 数据库
本文介绍如何使用 Python 解析火狐浏览器的 SQLite3 数据库,包括书签、历史记录和下载记录等。通过安装 Python 和 SQLite3,定位火狐数据库文件路径,编写 Python 脚本连接数据库并执行 SQL 查询,最终输出最近访问的网站历史记录。
363 4
|
人工智能 小程序 Java
【工具】轻松解锁SQLite数据库,一窥微信聊天记录小秘密
本文介绍了一款名为PyWxDump的开源工具,它可以获取微信账户信息、解密SQLite数据库以查看和备份聊天记录。此工具适用于已登录电脑版微信的用户,通过GitHub下载后简单几步即可操作。适合对数据恢复感兴趣的开发者,但请注意合法合规使用并尊重隐私。
2900 2
【工具】轻松解锁SQLite数据库,一窥微信聊天记录小秘密
|
存储 关系型数据库 数据库
轻量级数据库的利器:Python 及其内置 SQLite 简介
轻量级数据库的利器:Python 及其内置 SQLite 简介
503 3
|
关系型数据库 Java MySQL
C#winform中使用SQLite数据库
C#winform中使用SQLite数据库
729 3
C#winform中使用SQLite数据库
|
存储 API 数据库
QML使用Sqlite数据库存储ListModel数据
本文介绍了在QML中使用Sqlite数据库存储ListModel数据的方法,包括如何创建数据库、读取数据、动态添加和删除数据,以及如何在程序启动和退出时与数据库同步数据。
462 2
|
数据库 数据库管理
qt对sqlite数据库多线程的操作
本文总结了在Qt中进行SQLite数据库多线程操作时应注意的四个关键问题,包括数据库驱动加载、加锁、数据库的打开与关闭,以及QsqlQuery变量的使用。
1050 1

热门文章

最新文章