c语言 sqlite_SQLite与C语言

c语言 sqlite

SQLite数据库简介 (Introduction to SQLite database)

SQLite is a relational database; it is used for embedded devices. Now a day it is using worldwide for different embedded devices.

SQLite是一个关系数据库。 它用于嵌入式设备。 如今,它已在全球范围内用于不同的嵌入式设备。

SQLite database has following features

SQLite数据库具有以下功能

  • Serverless

    无服务器

  • Zero Configuration

    零配置

  • Transactional SQL database engine

    事务型SQL数据库引擎

It supports following operating systems

它支持以下操作系统

  • Linux

    的Linux

  • Windows

    视窗

  • Mac

    苹果电脑

  • Solaris

    的Solaris

  • Android

    安卓系统

It can be used with following programming languages

可以与以下编程语言一起使用

  • C

    C

  • C++

    C ++

  • PHP

    PHP

  • Java

    Java

  • Python etc

    Python等

In this article we will learn Sqlite3 tool for SQLite database, to use Sqlite3 tool first we need to install it into our system, it is easily available for all operating systems. Sqlite3 tool is a command-line tool. We can use all the most SQL statement in the SQLite database.

在本文中,我们将学习用于SQLite数据库的Sqlite3工具,要首先使用Sqlite3工具,我们需要将其安装到我们的系统中,所有操作系统都可以轻松使用它。 Sqlite3工具是命令行工具。 我们可以使用SQLite数据库中所有最多SQL语句。

在SQLite中创建数据库 (Creation for database in SQLite)

    $ sqlite3 mytest.db
    SQlite version 3.8.2 2015-11-07 15:54:36
    Enter  ".help" for instructions
    Enter SQL statements terminated with a ";"

In the above example, we passed the database name with the sqlite3 tool using the command line, now "mytest.db" database will be created in your system.

在上面的示例中,我们使用sqlite3工具通过命令行传递了数据库名称,现在将在您的系统中创建“ mytest.db”数据库。

如何检查SQLite数据库中创建的表? (How to check created tables in SQLite database?)

    Sqlite> .tables

The above command display list of created tables in selected database.

上面的命令显示在所选数据库中创建的表的列表。

如何从数据库退出? (How to exit from database?)

    Sqlite> .exit

Above command is used to exit or quit from selected database.

上面的命令用于退出或退出所选数据库。

Here, we will use the Linux operating system and GNU C Compiler (GCC). We will write the first program by which we can get the version number of installed Sqlite database.

在这里,我们将使用Linux操作系统和GNU C编译器(GCC) 。 我们将编写第一个程序,通过该程序可以获得已安装的Sqlite数据库的版本号

#include<sqlite3.h>
#include<stdio.h>

int  main()
{
	printf("%s\n",sqlite3_libversion());
	return 0;
}

In the above example, we include header file <sqlite3.h> that contains a declaration for multiple functions but we used one of them is sqlite3_libversion() which returns the version number of installed SQLite database.

在上面的示例中,我们包括头文件<sqlite3.h> ,该头文件包含多个函数的声明,但我们使用的其中一个是sqlite3_libversion() ,该函数返回已安装SQLite数据库的版本号。

Now, we will know how to compile this program?

现在,我们将知道如何编译该程序?

    $ gcc getversion.c -o getversion -lsqlite3 -std=c

In above compilation statement getversion.c is source code file and getversion is executable output file. And to compile sqlite library we need to use complile flags like -lsqlite -std=c .

在上面的编译语句中, getversion.c是源代码文件,而getversion是可执行输出文件。 要编译sqlite库,我们需要使用-lsqlite -std = c之类的编译标志。

Execution of the file:

执行文件:

    $ ./gerversion

Output

输出量

3.8.2

Now, we use another example for getting SQLite database version using SQLite Query in c program.

现在,我们使用另一个示例在C程序中使用SQLite Query获取SQLite数据库版本。

#include <sqlite3.h>
#include <stdio.h>

int main()
{
	sqlite3 *  DB;
	sqlite_stmt * RES;

	int rec = 0;

	rec = sqlite3_open(":memory:",&DB);

	if( rec != SQLITE_OK)
	{
		printf("\nDatabase cannot opened: %s",sqlite3_errmsg(DB));
		sqlite3_close(DB);

		return 1;
	}

	rec = sqlite3_prepare_v2(DB,"select SQLITE_VERSION()", -1, &RES,0);

	if( rec != SQLITE_OK)
	{
		printf("\nFailed to get data: %s",sqlite3_errmsg(DB));
		sqlite3_close(DB);

		return 1;
	}

	rec = sqlite3_step(RES);

	if(rec == SQLITE_ROW)
	{
		printf("%s\n",sqlite3_column_text(RES,0));
	}

	sqlite3_finalize(RES);
	sqlite3_close(DB);

	return 0;
} 

In the above example, we used SQLITE_VERSION() query to get version number of SQLite database.

在上面的示例中,我们使用SQLITE_VERSION()查询来获取SQLite数据库的版本号。

Also, we used the following pointers:

另外,我们使用了以下指针:

    sqlite3 *DB;
    sqlite3_stmt *RES;

Here, DB is used to represent database and RES is used to represent SQL statement.

在这里, DB用于表示数据库,而RES用于表示SQL语句。

We used following functions in above example:

在上面的示例中,我们使用了以下功能:

  • sqlite3_open() to open data here we use ":memory:" to read sqlite version.

    sqlite3_open()在这里打开数据,我们使用“:memory:”读取sqlite版本。

  • sqlite3_close() to close opened database.

    sqlite3_close()关闭打开的数据库。

  • sqlite3_errmsg() to display error message.

    sqlite3_errmsg()显示错误消息。

  • sqlite3_prepare_v2() to execute SQL query.

    sqlite3_prepare_v2()执行SQL查询。

  • sqlite3_step() to get record one by one.

    sqlite3_step()一张一张地获取记录。

  • sqlite3_finalize() is used to destroy the object of a prepared statement.

    sqlite3_finalize()用于销毁准备好的语句的对象。

Reference: http://zetcode.com/db/sqlitec/

参考:http: //zetcode.com/db/sqlitec/

翻译自: https://www.includehelp.com/c/sqlite-with-c-language.aspx

c语言 sqlite

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值