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