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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377127.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377127.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377127.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

《MySQL 8.0.22執行器源碼分析(2)解讀函數 ExecuteIteratorQuery》

函數代碼 bool SELECT_LEX_UNIT::ExecuteIteratorQuery(THD *thd) {THD_STAGE_INFO(thd, stage_executing);DEBUG_SYNC(thd, "before_join_exec");Opt_trace_context *const trace &thd->opt_trace;Opt_trace_object trace_wrapper(trace);Opt_trace_object…

C語言,如何產生隨機數

1. 基本函數 在C語言中取隨機數所需要的函數是: int rand(void);void srand (unsigned int n); rand()函數和srand()函數被聲明在頭文件stdlib.h中,所以要使用這兩個函數必須包含該頭文件: #include <stdlib.h> 2. 使用方法 rand()函數返回0到RAND_MAX之間的偽隨機數(pse…

MongoDB源碼概述——內存管理和存儲引擎

數據存儲&#xff1a; 之前在介紹Journal的時候有說到為什么MongoDB會先把數據放入內存&#xff0c;而不是直接持久化到數據庫存儲文件&#xff0c;這與MongoDB對數據庫記錄文件的存儲管理操作有關。MongoDB采用操作系統底層提供的內存文件映射&#xff08;MMap&#xff09;的方…

OBTW的完整形式是什么?

OBTW&#xff1a;哦&#xff0c;順便說一下 (OBTW: Oh, By The Way) OBTW is an abbreviation of "Oh, By The Way". OBTW是“哦&#xff0c;順便說一下”的縮寫 。 It is an expression, which is commonly used in messaging or chatting on social media network…

SharePoint 2010 Form Authentication (SQL) based on existing database

博客地址 http://blog.csdn.net/foxdaveSharePoint 2010 表單認證&#xff0c;基于現有數據庫的用戶信息表本文主要描述本人配置過程中涉及到的步驟&#xff0c;僅作為參考&#xff0c;不要僅限于此步驟。另外本文通俗易懂&#xff0c;適合大眾口味兒。I. 開啟并配置基于聲明的…

《MySQL 8.0.22執行器源碼分析(3.1)關于RowIterator》

目錄RowIteratorInit()Read()SetNullRowFlag()UnlockRow()StartPSIBatchMode()EndPSIBatchModeIfStarted()real_iterator()RowIterator 使用選定的訪問方法讀取單個表的上下文&#xff1a;索引讀取&#xff0c;掃描等&#xff0c;緩存的使用等。 它主要是用作接口&#xff0c;但…

hdu 2432法里數列

這題本來完全沒思路的&#xff0c;后來想一想&#xff0c;要不打個表找找規律吧。于是打了個表&#xff0c;真找到規律了。。。 打表的代碼如下&#xff1a; int n; void dfs(int x1, int y1, int x2, int y2) {if (y1 y2 < n) {dfs(x1, y1, x1 x2, y1 y2);printf("…

python學習筆記四——數據類型

1.數字類型&#xff1a; 2.字符串類型&#xff1a; 切片&#xff1a;a[m:n:s] m:起始值 n:結束值&#xff08;不包括n&#xff09; s:步長&#xff0c;負數表示從后向前取值 3.序列&#xff1a;列表&#xff0c;元組和字符串都是序列 序列的兩個主要特點是索引操作符和切片…

小狐貍ChatGPT系統 不同老版本升級至新版數據庫結構同步教程

最新版2.6.7下載&#xff1a;https://download.csdn.net/download/mo3408/88656497 小狐貍GPT付費體驗系統如何升級&#xff0c;該系統更新比較頻繁&#xff0c;也造成了特別有用戶數據情況下升級時麻煩&#xff0c;特別針對會員關心的問題出一篇操作教程&#xff0c;本次教程…

《MySQL 8.0.22執行器源碼分析(3.2)關于HashJoinIterator》

在本文章之前&#xff0c;應該了解的概念&#xff1a; 連接的一些概念、NLJ、BNL、HashJoin算法。 目錄關于join連接probe行保存概念Hashjoin執行流程&#xff08;十分重要&#xff09;HashJoinIterator成員函數講解1、BuildHashTable2、ReadNextHashJoinChunk3、ReadRowFromPr…

json 語法_JSON的基本語法

json 語法JSON which stands for JavaScript Object Notation is a lightweight readable data format that is structurally similar to a JavaScript object much like its name suggests. 代表JavaScript Object Notation的 JSON是一種輕量級的可讀數據格式&#xff0c;其結…

RFC3261(17 事務)

SIP是一個基于事務處理的協議&#xff1a;部件之間的交互是通過一系列相互獨立的消息交換來完成的。特別是&#xff0c;一個SIP 事務由一個單個請求和這個請求的所有應答組成&#xff0c;這些應答包括了零個或者多個臨時應答以及一個或者多個終結應答。在事務中&#xff0c;當請…

HDUOJ---1754 I Hate It (線段樹之單點更新查區間最大值)

I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 33469 Accepted Submission(s): 13168 Problem Description很多學校流行一種比較的習慣。老師們很喜歡詢問&#xff0c;從某某到某某當中&#xff0c;…

WEG的完整形式是什么?

WEG&#xff1a;邪惡邪惡的咧嘴 (WEG: Wicked Evil Grin) WEG is an abbreviation of "Wicked Evil Grin". WEG是“ Wicked Evil Grin”的縮寫 。 It is also known as EWG (Evil Wicked Grin) "Grin" refers to a broad smile. "Wicked" refer…

C# 把數字轉換成鏈表

例如&#xff1a;123456轉換成 1 -> 2 -> 3-> 4-> 5-> 6 View Code static LinkedList<int> CovertIntToLinkedList(int num){Stack<int> stack new Stack<int>();LinkedList<int> result new LinkedList<int>();while (num!0…

《MySQL 8.0.22執行器源碼分析(4.1)Item_sum類以及聚合》

Item_sum類用于SQL聚合函數的特殊表達式基類。 這些表達式是在聚合函數&#xff08;sum、max&#xff09;等幫助下形成的。item_sum類也是window函數的基類。 聚合函數&#xff08;Aggregate Function&#xff09;實現的大部分代碼在item_sum.h和item_sum.cc 聚合函數限制 不…

Java 性能優化實戰記錄(2)---句柄泄漏和監控

前言: Java不存在內存泄漏, 但存在過期引用以及資源泄漏. (個人看法, 請大牛指正) 這邊對文件句柄泄漏的場景進行下模擬, 并對此做下簡單的分析.如下代碼為模擬一個服務進程, 忽略了句柄關閉, 造成不能繼續正常服務的小場景. 1 public class FileHandleLeakExample {2 3 p…

什么是Java文件?

Java文件 (Java files) The file is a class of java.io package. 該文件是java.io包的類。 If we create a file then we need to remember one thing before creating a file. First, we need to check whether a file exists of the same name or not. If a file of the sa…

繞過本地驗證提交HTML數據

我們在入侵一個網站,比如上傳或者自己定義提交的文件時,會在本地的代碼中遇到阻礙,,也就是過 濾,過濾有兩種,一種是在遠程服務器的腳本上進行的過濾,這段代碼是在服務器上運行后產生作用的,這種過 濾方式叫做遠程過濾;另一種是在我們的IE瀏覽器里執行的腳本過濾,就是說是在我們…

《dp補卡——343. 整數拆分、96. 不同的二叉搜索樹》

343. 整數拆分 1、確定dp數組以及下標含義。 dp[i]&#xff1a;分拆數字i&#xff0c;可以得到的最大的乘積 2、確定遞推公式&#xff1a; dp[i]最大乘積出處&#xff1a;從1遍歷j到i&#xff0c;j * dp[i-j] 與 j * (i-j)取最大值。( 拆分j的情況&#xff0c;在遍歷j的過程…