这里写自定义目录标题
1.CMakeList.txt
CMakeList.txt中需要指明库文件路径,头文件路径,交叉编译工具链。
cmake_minimum_required(VERSION 3.5.1)
project(BANANAPEELX)
##设置交叉编译工具链
set(TOOLCHAIN_DIR /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/arm-linux-gnueabihf-g++)
set(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/arm-linux-gnueabihf-gcc)
set(DATABASE_LIBS sqlite3)
##添加头文件路径
include_directories(.)
include_directories(/home/alientek/sqlite-autoconf-3420000/install/include)
##添加库目录
link_directories(/home/alientek/sqlite-autoconf-3420000/install/lib)
##add_executable(hello test_database.c)
set(SRC_LIST test_database.c)
add_executable(test_database ${SRC_LIST})
##链接库
target_link_libraries(test_database ${DATABASE_LIBS} )
2.源文件
test_database.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include "sqlite3.h"
char DBFileName[80];
char DBTableName[20];
int main( int argc,char* argv[] )
{
char SqlStr[256];
char* ErrMsg;
char str[64];
int startidx;
int i1, rc;
time_t t;
struct tm *p;
sqlite3* db;
startidx = 0;
strcpy( DBFileName, "/home/mydatabase/test.db");
strcpy( DBTableName, "table1");
//创建数据库文件
rc = sqlite3_open( DBFileName, &db );
sprintf( SqlStr, "create table %s(Name char(50), Value int, Date char(50))",
DBTableName );
//创建表
sqlite3_exec( db, SqlStr, 0, 0, &ErrMsg );
//往表里插入10条记录
for( i1=startidx; i1<startidx+10; i1++ )
{
time( &t );
p = localtime ( &t );
sprintf( str, "%d.%d.%d-%d:%d:%d\n", p->tm_year + 1900, p->tm_mon+1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec );
printf("%d\n", i1+1);
sprintf(SqlStr, "insert into %s values(\"Name%d\", %d, \"%s\")",
DBTableName, i1+1, i1+1, str );
sqlite3_exec( db, SqlStr, 0, 0, &ErrMsg );//执行sql语句sqlite3_exec
sleep(1);
}
sqlite3_close( db );
return 0;
}
3.创建并写入
编译后将可执行文件拷贝到开发板
创建数据库test.db
sqlite3 test.db
后执行
./test_database
4.查看数据库
sqlite3 test.db
.schema
select * from table1;
数据库更多操作参考:
数据库命令