当前位置:网站首页>Embedded database -- SQLite
Embedded database -- SQLite
2022-07-28 03:20:00 【In the first World War, Cheng Shuo rushed】
embedded database --SQLite
- Database notes
Database notes
1. A database file is similar to a EXCEL file
There are multiple tables in the database file (tables), Be similar to EXCEL Medium SHEET.
In each table , Store something similar to EXCEL Medium 2 Dimension table data .
Every column in every table , Call a field .
Every row in every table , Call a record .
2. Command line ( With . The first order )
- .tables View the table under the current database file
- .schema View the table structure under the current database file ( Fields and types of tables )
3. Basic SQL sentence ( Is a syntax rule that all databases follow , A command that ends with a semicolon )
3.1 Create a table , You need to know the name of the table , Field name , The type of each field
create table The name of the table ( Field name 1 Field 1 type , Field name 2 Field 2 type , ...);
EG:create table student(id text , name text , age text , score text);
create table user(id text ,name text, pwd text);
3.2 insert data
INSERT INTO The name of the table VALUES (value1, value2, value3, ...);
EG: SERT INTO student VALUES("1000", "zhangsan", "18", "90");
3.3 View the data in a table
SELECT * FROM The name of the table (WHERE Conditions );
EG:SELECT * FROM student;
SELECT * FROM student WHERE id = "1000";
SELECT name,score FROM student WHERE id = "1000";
3.4 Delete table ( Be similar to EXCEL Zhongba whole SHEET Delete all )
DROP TABLE The name of the table ;
3.5 Delete data from table
DELETE FROM The name of the table (WHERE Conditions );
EG:DELETE FROM student WHERE name ="zhangsan";
3.6 Modify the data in the table
UPDATE The name of the table SET Field 1 = "" AND Field 2 = "" ...(WHERE ) ;
EG: UPDATE student SET score =“80” WHERE name ="zhangsan";
4 .API Use
sqlite3_open
The name of the first parameter database , The first 2 Parameters sqlite3 * db Represents a handle to the database ;
int sqlite3_exec(); Execute one SQL sentence .
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated SQL sentence */
int (*callback)(void*,int,char**,char**), /* Callback function Callback function */
void *, /* 1st argument to callback The parameters of the callback function */
char **errmsg /* Error msg written here error message */
/*
callback Will automatically be called many times , Every record ,callback Will be automatically called once .
notused : The incoming parameters of the callback function
colNum: How many columns are there in this record
colContent: The specific content of each column
colName: The name of each column
*/
eg:
int main(int argc, char *argv[])
{
QocoreApplication a(argc,argv);
sqlite3 * pdb;
int res = sqlite3_open( "d : //test.db",&pdb);
if(res != sQLITE_OK)
{
printf( "open db err\n");
return -1;
}
printf ( "open db success\n" );
char *errMsg;
char sql[100]= {0} ;
strcpy(sql,"CREATE TABLE user(id TEXT ,name TEXT,pwd TEXT); ");
res = sqlite3_exec(pdb, sql,NULL,NULL,&errMsg);
if(res != SQLITE_OK)
{
printf( "sqlite3_exec db err errMsg=[%s]\n", errMsg);
return -1;
}
printf( "sqlite3_exec db success\n" );
sqlite3_close;
return a.exec();
}
callback
int callback(void* notused, int colNum, char** colContent, char**colName )
~~~
eg:
/ / callback Function is when querying SQL When the statement returns , Automatically called .
// Whenever there is a record , The function is called once
// notused :sqlite3_exec Incoming parameter
// colNum: The number of columns of this record
// colContent: Array of strings , For the contents of each column
// colName: The column name of each column of this record
int firstFlg = 0;
int callback(void* notused,int colNum,char** colContent,char**colName )
{
if( ! firstFlg)
{
{
// Print column names
for ( int i = 0;i <colNo;i++){
printf("%s ",colName[i]);
}
printf("\n");
firstFlg++;
for(int i = 0;i < colNum;i++)
{
printf("%s ", colContent[i]);
}
}
strcpy(sql,"SELECT * FROM user; ");
firstFlg = 0;
res = sqlite3_exec(pdb, sql,NULL,NULL,&errMsg);
if(res != SQLITE_OK)
{
printf( "sqlite3_exec dr err errMsg=[%s]\n",errMsg);
return -1;
}
printf("sqlite3_exec db success n" );
sqlite3_get_table
sqlite3_get_table, Execute a query SQL sentence , And return all the results of the query .
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
The first 1 No more parameters , Look at the previous example .
The first 2 The parameters are sql sentence , Follow sqlite3_exec Inside sql It's the same . Is a very common with \0 At the end of the char* character string .
The first 3 The first parameter is the query result , It's still a one-dimensional array ( Don't think it's a two-dimensional array , Don't think it's a three-dimensional array ).
Its memory layout is : Field name , This is followed by the value of each field . Let's use an example .
The first 4 The first parameter is how many records are queried ( That is, find out how many lines , The line excluding the field name ).
The first 5 How many fields are the parameters ( How many columns ).
The first 6 The first parameter is the error message , It's the same as before , I won't say much more here .
pazResult The result set is stored , The order of storage is each field in the header , Then each field of each record is stored
EG: The result set in the database is as follows :
Name | Age
-----------------------
Alice | 43
Bob | 28
Cindy | 21
Then the storage format is as follows
pazResult[0] = "Name";pazResult[1] = "Age";
pazResult[2] = "Alice";pazResult[3] = "43";
pazResult[4] = "Bob";pazResult[5] = "28";
pazResult[6] = "Cindy";pazResult[7] = "21";
sqlite3_close, Close the database
//lsqlite3 *db Open database handle
//const char *zSql Of the query SQL sentence
//char ***pazResult All result sets //
int *pnRow How many rows of data are there in the returned result
//int *pncolumn How many columns of data are there in the returned result char **pazResult;
int row = 0;
int col = 0;
res =sqlite3_get_table(pdb,sql,&pazResult,&row,&col,&errMsg);
if(res != SQLITE_OK)
{
printf( "sqlite3_exec db err errMsg=[%s]\n",errMsg);
return -l;
}
for(int j = 0; j <= row;j++)
{
for (int i = 0; i < col;i++)
{
printf("%-20s",pazResult[j*col+i]);
}
printf("\n");
}
sqlite3_free_table(pazResult);
sqlite3_close(pdb);
5.sqlite3 stay VS Environment configuration
- Take the header file (sqlite3.h) And libraries (sqlite3.lib) Put it in the source code folder of the project .
- Items in the menu -》 attribute -》 Configuration properties -》 The connector -》 routine -》 Additional Library Directory -》 Join in sqlite3.lib Path
- Items in the menu -》 attribute -》 Configuration properties -》 The connector -》 Input -》 Add dependency -》 Join in sqlite3.lib( Tell the compiler the name of the library that needs to be loaded )
- Items in the menu -》 attribute -》 Configuration properties -》 The connector -》 Input -> Ignore specific defaults *
- Join in LIBCD.lib
- Add... To the source file #include “sqlite3.h”, #pragma comment(lib,“sqlite3.lib”)
6.sqlite3 stay QT Environment configuration
- Take the header file (sqlite3.h) And libraries (libsqlite3.a) Put it in the source code folder of the project .
- stay QT Added to the project documents LIBS += -L D:\workspace\20220721\ -lsqlite3, hold libsqlite3.a Add to the project ()
边栏推荐
- QML使用Layout布局时出现大量<Unknown File>: QML QQuickLayoutAttached: Binding loop detected for property循环绑定警告
- C#实现弹出一个对话框的同时,后面的form不可用
- The test post changes jobs repeatedly, jumping and jumping, and then it disappears
- Engineering Geology Practice - engineering geology problem set
- 【类的本质(Objective-C语言中)】
- Talk about the speech synthesis function of Baidu University of science and technology news Feiyun Zhisheng
- Hotel VR panoramic display shooting provides more opportunities for cooperation and negotiation
- QT official example: Fridge Magnets example
- IronOCR for .NET 2022.8
- 线程基础
猜你喜欢

工程电磁场复习基本知识点

C#WinForm开发:如何将图片添加到项目资源文件(Resources)中

My approval of OA project (meeting inquiry & meeting signature)

Brush questions every day to consolidate knowledge

QFileDevice、QFile、QSaveFile、QTemporaryFile

四、固态硬盘存储技术的分析(论文)

Review basic knowledge points of engineering electromagnetic field

More than 50 interviews have been summarized, and notes and detailed explanations have been taken from April to June (including core test sites and 6 large factories)

On weight decay and discarding method

Web服务器
随机推荐
WEB安全基础 - - -命令执行漏洞
ELS timer
"Introduction to engineering electromagnetic field" after class exercises with answers
c#——switch case语句
【ACwing 1064 小国王】状压dp
线程基础
Web服务器
Brush questions every day to consolidate knowledge
The object array is converted to string and then separated by strings, including the competition to select the children of a field, or the sum,
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!
颜色的识别方法和探索 基于matlab
Distributed transaction Senta (I)
蓝桥杯:第九届—“彩灯控制器”
VI command details
ELS keyboard information
C WinForm development: how to add pictures to project resources
基于c8t6芯片开发RC522模块实现呼吸灯
【Codeforces Round #806 (Div. 4)(A~F)】
Alibaba cloud international email service package purchase process
C#设置Textbox控件不可编辑