问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
你好,欢迎来到懂视!登录注册
当前位置: 首页 - 正文

什么是嵌入式数据库SQlite

发布网友 发布时间:2022-04-20 14:58

我来回答

3个回答

懂视网 时间:2022-04-30 14:39

Include stdio.h, sqlite3.h and stdlib.h , stdlib.h is for malloc and sqlite3.h contains the standard function declarations needed for the required functionality.

// The number of queries to be handled,size of each query and pointer
int q_cnt = 5,q_size = 150,ind = 0;
char **queries = malloc(sizeof(char) * q_cnt * q_size);

q_cnt stored the number of queries we may want to do, q_size stores the max size of a SQL query, ind is the index.

**queries is a double array or matrix which stores the multiple queries. The total amount of storage to be allocated is sizeof(char) * q_cnt * q_size

// A prepered statement for fetching tables
sqlite3_stmt *stmt;

// Create a handle for database connection, create a pointer to sqlite3
sqlite3 *handle;

// try to create the database. If it doesnt exist, it would be created
// pass a pointer to the pointer to sqlite3, in short sqlite3**
retval = sqlite3_open(“sampledb.sqlite3″,&handle);
// If connection failed, handle returns NULL
if(retval)
{
printf(“Database connection failed ”);
return -1;
}
printf(“Connection successful ”);

We need to create a pointer to sqlite3 and sqlite3_stmt structures. sqlite3 is the structure which is to hold the database connection handle. sqlite3_stmt is just like a cursor to a database.

sqlite3_open function needs the address of the sqlite3 database instance on the disk. The second parameter is the pointer to the pointer to sqlite3 structure. One mistake which I stumbled upon was to create a sqlite3 ** handle and then pass it to this function. The correct way is to create a sqlite3* handle and then pass the pointer to it using the & operator

// Create the SQL query for creating a table
char create_table[100] = “CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY,pass TEXT NOT NULL,activated INTEGER)”;

// Execute the query for creating the table
retval = sqlite3_exec(handle,create_table,0,0,0);

// Insert first row and second row
queries[ind++] = “INSERT INTO users VALUES(‘manish’,‘manish’,1)”;
retval = sqlite3_exec(handle,queries[ind-1],0,0,0);
queries[ind++] = “INSERT INTO users VALUES(‘mehul’,‘pulsar’,0)”;
retval = sqlite3_exec(handle,queries[ind-1],0,0,0);

Create a table if it does not exist and then insert two rows. Note that sqlite3 does not support inserting two rows in one single query. Maybe I need to confirm this fact again, but I never worked for me ever.

// select those rows from the table
queries[ind++] = “SELECT * from users”;
retval = sqlite3_prepare_v2(handle,queries[ind-1],-1,&stmt,0);
if(retval)
{
printf(“Selecting data from DB Failed ”);
return -1;
}

// Read the number of rows fetched
int cols = sqlite3_column_count(stmt);

Create a prepared statement for fetching data from the database usingsqlite3_prepare_v2 function call. The first parameter is the database handle itself which is a sqlite3* pointer. The second parameter is the SQL statement which needs to be executed. The third parameter tells upto how long the second parameter to be read. Pass -1 to make it read till line terminator. Fourth statement is the pointer to pointer to prepared statement structure. Take care of the pointer concept as I told about sqlite3 structure. The fifth parameter is filled with the unused portion of the query. Have a look at the official documentation.

sqlite3_column_count function gets the number of columns for the result fetched.

while(1)
{
// fetch a row’s status
retval = sqlite3_step(stmt);

if(retval == SQLITE_ROW)
{
// SQLITE_ROW means fetched a row

// sqlite3_column_text returns a const void* , typecast it to const char*
for(int col=0 ; col {
const char *val = (const char*)sqlite3_column_text(stmt,col);
printf(“%s = %s ”,sqlite3_column_name(stmt,col),val);
}
printf(“ ”);
}
else if(retval == SQLITE_DONE)
{
// All rows finished
printf(“All rows fetched ”);
break;
}
else
{
// Some error encountered
printf(“Some error encountered ”);
return -1;
}
}

We have put this code in infinite while loop as we are not sure how much rows it contains. Usually, the table returns n+1 rows, where 1 extra row is for telling that all rows have been fetched. sqlite3_step returns the status which is actually an enumeration. Check all the results contants here. Two most used are SQLITE_DONE, SQLITE_ROW. The former tells that all the rows have been fetched, now the user can come out of this loop and continue. SQLITE_ROW tells that a valid row has been fetched.

// Close the handle to free memory
sqlite3_close(handle);
return 0;
}

sqlite3_close simply closes the database connection.

Save the code in a file named, say dataman.c , compile it using the command

$ gcc dataman.c -o dataman -l sqlite –std=c99

You obviously need to have sqlite development headers installed for compiling the same. The name of the package on Ubuntu is libsqlite3-dev

Official SQLite Documentation

参考:

  1. 快速入门 http://www.sqlite.org/sqlite.html
  2. “SQLite header and source version mismatch” http://jianshusoft.blog.51cto.com/2380869/824575
  3. c语言的较为详细的例子 http://milky.manishsinha.net/2009/03/30/sqlite-with-c/


SQLite/嵌入式数据库

标签:

热心网友 时间:2022-04-30 11:47

SQlite就是一个精简版的SQL库,可以不用SQL Server服务后台支持的情况下,实现选择、删除等SQL命令,主要用在独立的C/C++/Java客户端程序里。

热心网友 时间:2022-04-30 13:05

http://ke.baidu.com/view/19310.htm
SQLite:轻量级嵌入式数据库

SQLite,作为一款轻量级的嵌入式数据库引擎,其核心特点在于无需服务器配置,能够直接在应用程序内部运行,支持静态或动态连接。这种零配置的特性使得它在资源受限的嵌入式设备中尤为适用,特别适合C++等工业领域广泛应用的语言接口。在各种应用场景中,SQLite常用于单用户应用程序的本地数据存储,或者作为客户端...

slite是什么意思

SQLite是一个轻量级的关系型数据库管理系统。它不同于传统的数据库管理系统,SQLite是一个嵌入式数据库引擎,这意味着它可以在单个计算机上运行,并且可以轻松集成到各种应用程序中。由于其紧凑性和可靠性,SQLite广泛应用于移动设备、桌面应用程序和嵌入式系统中。详细解释:1. 数据库管理系统: SQLite作为...

sqlite适合实时数据吗

1、SQLite是嵌入式数据库:SQLite可嵌入到使用应用程序中,共用相同的进程空间,而不是单独的一个进程,所以SQLite在程序内部是完整的,自包含的数据库引擎。2、SQLite只有一个文件:SQLite只需要一个文件,这个文件的格式在多个主要版本中都是通用的,也就是说如果有一个3.0.0版本(2004年)的SQLite数...

手机存储的轻型数据库(SQLite)是干什么的?

SQLite是一种轻型数据库,常用于移动设备和嵌入式系统中。在手机存储中,SQLite被用作一个本地的嵌入式数据库,用于存储和管理应用程序的数据。SQLite提供了一个简单的、易于集成的方式,使开发者可以将数据存储在手机本地,而不需要连接到远程服务器。SQLite的主要优点是其轻量级、高效和易用性。它占用的...

什么是 sqlite 数据库,Sqlite相比ACCESS的优势

什么是 sqlite ?�0�2�0�2sqlite 是一款轻量级的、基于文件的嵌入式数据库,2000年就已经诞生,经过7年多的发展,直到今天已经成为最流行的嵌入式数据库,包括google在内的公司在其桌面软件中亦使用 sqlite 存储用户数据。由此可以看出,已经没有任何理由去怀疑...

什么是嵌入式数据库

简单来说,就是一些终端使用的数据库,如POS机、手机等或者其他的非电脑上面使用的,一般都可以叫 嵌入式数据库 典型的数据库是sqlite,单文件数据库

sqlite是不是只能本地访问

sqlite是嵌入式数据库,只能在本地使用,如果要实现网络版?那只有通过中间程序来调用数据。比如说A网站想访问B网站的Sqlite的数据,只有调用B网站中某个程序,如Data.aspx来获得数据。开发工具嘛,只需要vs2008和System.Data.SQLite.dll即可。linux下可以用什么工具来实现网络版呢?linux平台下也一样,只...

SQLite 的读写效率很高,有哪些使用其他数据库的理由

1、sqlite定位是嵌入式数据库,只能本地嵌入其它进程来工作,无法被远程的客户端访问,需要上层应用来处理这些事情;2、sqlite设计上就不是为大数据量考虑的,因此别指望它存海量数据;3、sqlite适合单线程访问,对多线程高并发的场景不适用;4、各种数据库高级特性它都不支持,比如管理工具、分析工具、维护...

什么是SQLITE数据库

SQLite是一款轻型的数据库,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如Tcl、PHP、Java等,还有ODBC接口,同样比起Mysql、...

.sqlite是什么mac

.sqlite是SQLite数据库文件的扩展名。SQLite是一款轻量级的嵌入式数据库,被广泛应用于各种嵌入式设备和移动设备中。在Mac系统上,SQLite是默认支持的,相关应用位于/usr/bin/sqlite3。你可以通过命令行方式打开SQLite,例如$ sqlite3 filename;,其中filename是你想要打开的.sqlite文件的路径。

声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com
阵亡和伤亡两个有什么区别? 阵亡和战死有什么区别? 某校五年级有256人其中女生有134人女生占五年级全体学生的几分之几男 ... 在迎国庆的日子里学校举行了一系列的活动五年级学生共有134人参加此次... 吃果冻什么意思 吃果冻什么含义 月什么溶溶 望月的歌词是什么? 形容海边日落的文案(描写海边夕阳余晖的优美句子) 《月下小景》沈从文 月下小景基本信息 嵌入式数据库的国产嵌入式数据库OpenBASE Lite 嵌入式计算机的概念 嵌入式系统与嵌入式数据库有什么区别和联系? 小米手机小爱同学锁屏可以唤醒吗 嵌入式开发需要掌握哪些知识 嵌入式实时数据库系统并发控制机制的特点主要体现... 网络数据库和嵌入式数据库的区别 小米note3的小爱同学什么时候能支持熄屏唤醒 嵌入式数据库的可用于移动开发的嵌入式数据库简介 红米Note5支持小爱同学息屏唤醒功能吗 嵌入式数据库的发展趋势在哪里 小米10✘怎么能在待机情况下不动手机直接唤... 嵌入式数据库的常用的嵌入式数据库的比较 请问小米mix 2是否支持锁屏状态下唤醒小爱同学,如... 常用嵌入式数据库有哪些 小米4能在息屏状态下唤醒小爱同学吗 嵌入式实时数据库的数据模型 什么是嵌入式数据库 小米Note2的小爱同学怎么可以息屏唤醒 嵌入式实时数据库的物理结构 mysql 是不是嵌入式数据库,该如何处理 别人天天都在发拼多多1元拼单,到底在哪里找的呀?我咋个又找不到呢? 怎样才能在京东/淘宝天猫/拼多多上抢到一元购商品? 淘宝一元秒杀专区在哪里,淘宝一元秒杀是真的吗 iphone怎么设置勿扰模式 iPhone勿扰模式怎么开启 苹果手机设置步骤 关于iphone的勿扰模式? 苹果手机怎么进入勿扰模式 ios15勿扰模式怎么设置? 支付宝商家二维码收款怎么用花呗 支付宝可以花呗收款的二维码怎么弄 支付宝怎么用花呗收款二维码 支付宝商家二维码怎么可以用花呗付款? 宝收钱码可以用花呗吗 支付宝收钱码怎么用花呗支付 支付宝码怎么收花呗和信用卡钱 支付宝二维码怎么开通花呗收款 支付宝收钱码怎么弄支持花呗的 支付宝收款二维码为什么不能用花呗红包? 支付宝花呗收钱码二维码在哪里扫? 如何让支付宝收款码能使用花呗 支付宝收款二维码怎么设置花呗付款?
  • 焦点

最新推荐

猜你喜欢

热门推荐