时间:2021-07-01 10:21:17 帮助过:6人阅读
3.测试文件
1 // sqlite_demo.c 2 3 4 #include "stdio.h" 5 #include "/opt/sqlite3/include/sqlite3.h" 6 7 8 void print_node(void) 9 { 10 printf(" add [id] [msg(128)] ---- add item \n"); 11 printf(" show ---- show all item \n"); 12 printf(" exit ---- exit \n"); 13 } 14 15 int create_tabel(sqlite3 *db) 16 { 17 const char *sql_create_table = "create table t(id int primary key,msg varchar(128))"; 18 // const char *sql_drop_table = "drop table if exists t"; 19 char *errmsg = 0; 20 int ret = 0; 21 22 //简单的删除操作可以直接使用sqlite3_exec即可。这里不需要回调函数以及回调函数的参数。 当然需要可以关注sqlite3_exec返回的结果是否为SQLITE_OK的值。 23 //sqlite3_exec(db,sql_drop_table,NULL,NULL,&errmsg); 24 ret = sqlite3_exec(db, sql_create_table, NULL, NULL, &errmsg); 25 if(ret != SQLITE_OK) 26 { 27 fprintf(stderr,"create table fail: %s\n",errmsg); 28 } 29 30 return ret; 31 } 32 33 int print_record(void *params,int n_column,char **column_value,char **column_name) 34 { 35 int i; 36 for(i=0; i<n_column; i++) 37 { 38 printf("\t%s", column_value[i]); 39 } 40 printf("\n"); 41 return 0; 42 } 43 44 int show_all(sqlite3 *db) 45 { 46 int ret = 0; 47 char *errmsg = 0; 48 ret = sqlite3_exec(db, "select * from t", print_record, NULL, &errmsg); 49 if (ret != SQLITE_OK) 50 { 51 fprintf(stderr,"query SQL error: %s\n",errmsg); 52 } 53 return 0; 54 } 55 56 int insert_node(sqlite3 *db, int id, char * msg) 57 { 58 int ret = 0; 59 char *errmsg = 0; 60 char cmd[256] = { 0 }; 61 sprintf(cmd, "insert into t(id,msg) values(%d,\‘%s\‘)", id, msg); 62 printf("%s\n", cmd); 63 ret = sqlite3_exec(db, cmd, NULL, NULL, &errmsg); 64 printf("Insert a record %s\n", 65 ret == SQLITE_OK ? "OK":"FAIL"); 66 return ret; 67 } 68 69 70 int main() 71 { 72 char *errmsg = 0; 73 int ret = 0; 74 75 int index = 0; 76 char msg[128] = { 0 }; 77 78 sqlite3 *db = 0; 79 ret = sqlite3_open("./sqlite3-demo.db",&db); 80 if(ret != SQLITE_OK) 81 { 82 fprintf(stderr,"Cannot open db: %s\n",sqlite3_errmsg(db)); 83 return 1; 84 } 85 printf("Open database\n"); 86 87 //简单的删除操作可以直接使用sqlite3_exec即可。这里不需要回调函数以及回调函数的参数。 当然需要可以关注sqlite3_exec返回的结果是否为SQLITE_OK的值。 88 //sqlite3_exec(db,sql_drop_table,NULL,NULL,&errmsg); 89 ret = create_tabel(db); 90 for (index = 0; index < 10; index++) 91 { 92 sprintf(msg, "index%d --", index); 93 insert_node(db, index, msg); 94 } 95 show_all(db); 96 97 sqlite3_free(errmsg); 98 sqlite3_close(db); 99 100 printf("Close database\n"); 101 102 return 0; 103 }
demo的Makefile文件,该make文件使用广泛的万能makefile文件;
makefile中需要注意:
-L/opt/sqlite3/lib
-lsqlite3
编译命令:
1 make CROSS_COMPILE=arm-none-linux-gnueabi- distclean 2 make CROSS_COMPILE=arm-none-linux-gnueabi- all 3 4 cp __install/sqlite_demo.elf ../../install/opt/sqlite3/demo/
附makefile文件:
1 ############################################################################# 2 3 # 4 5 # Generic Makefile for C/C++ Program 6 7 # 8 9 # License: GPL (General Public License) 10 11 # Author: whyglinux <whyglinux AT gmail DOT com> 12 13 # Date: 2006/03/04 (version 0.1) 14 15 # 2007/03/24 (version 0.2) 16 17 # 2007/04/09 (version 0.3) 18 19 # 2007/06/26 (version 0.4) 20 21 # 2008/04/05 (version 0.5) 22 23 # 24 25 # Description: 26 27 # ------------ 28 29 # This is an easily customizable makefile template. The purpose is to 30 31 # provide an instant building environment for C/C++ programs. 32 33 # 34 35 # It searches all the C/C++ source files in the specified directories, 36 37 # makes dependencies, compiles and links to form an executable. 38 39 # 40 41 # Besides its default ability to build C/C++ programs which use only 42 43 # standard C/C++ libraries, you can customize the Makefile to build 44 45 # those using other libraries. Once done, without any changes you can 46 47 # then build programs using the same or less libraries, even if source 48 49 # files are renamed, added or removed. Therefore, it is particularly 50 51 # convenient to use it to build codes for experimental or study use. 52 53 # 54 55 # GNU make is expected to use the Makefile. Other versions of makes 56 57 # may or may not work. 58 59 # 60 61 # Usage: 62 63 # ------ 64 65 # 1. Copy the Makefile to your program directory. 66 67 # 2. Customize in the "Customizable Section" only if necessary: 68 69 # * to use non-standard C/C++ libraries, set pre-processor or compiler 70 71 # options to <MY_CFLAGS> and linker ones to <MY_LIBS> 72 73 # (See Makefile.gtk+-2.0 for an example) 74 75 # * to search sources in more directories, set to <SRCDIRS> 76 77 # * to specify your favorite program name, set to <PROGRAM> 78 79 # 3. Type make to start building your program. 80 81 # 82 83 # Make Target: 84 85 # ------------ 86 87 # The Makefile provides the following targets to make: 88 89 # $ make compile and link 90 91 # $ make NODEP=yes compile and link without generating dependencies 92 93 # $ make objs compile only (no linking) 94 95 # $ make tags create tags for Emacs editor 96 97 # $ make ctags create ctags for VI editor 98 99 # $ make clean clean objects and the executable file 100 101 # $ make distclean clean objects, the executable and dependencies 102 103 # $ make help get the usage of the makefile 104 105 # 106 107 #=========================================================================== 108 109 110 111 ## Customizable Section: adapt those variables to suit your program. 112 113 ##========================================================================== 114 115 #-include ../Rules.make 116 117 # The pre-processor and compiler options. 118 119 MY_CFLAGS = 120 121 122 123 # The linker options. 124 125 MY_LIBS = -L/opt/sqlite3/lib 126 127 128 129 # The pre-processor options used by the cpp (man cpp for more). 130 131 CPPFLAGS = -Wall -g -O2 132 133 134 135 # The options used in linking as well as in any direct use of ld. 136 137 LDFLAGS += -lrt -lsqlite3 138 139 # The directories in which source files reside. 140 141 # If not specified, only the current directory will be serached. 142 143 SRCDIRS = ./include . 144 145 # The files that are not included during compilation. 146 147 EX_SRCS = 148 149 # The executable file name. 150 151 # If not specified, current directory name or `a.out‘ will be used. 152 153 PROGRAM = sqlite_demo 154 155 156 157 MY_INSTALLPROGRAM = ./__install/sqlite_demo.elf 158 159 160 161 162 163 ## Implicit Section: change the following only when necessary. 164 165 ##========================================================================== 166 167 168 169 # The source file types (headers excluded). 170 171 # .c indicates C source files, and others C++ ones. 172 173 #SRCEXTS = .c .C .cc .cpp .CPP .c++ .cxx .cp 174 175 SRCEXTS = .c 176 177 178 179 # The header file types. 180 181 #HDREXTS = .h .H .hh .hpp .HPP .h++ .hxx .hp 182 183 HDREXTS = .h 184 185 186 187 # The pre-processor and compiler options. 188 189 # Users can override those variables from the command line. 190 191 #CFLAGS = -g -O0 -Wall -mcpu=arm926ej-s -march=armv5t 192 193 CXXFLAGS= -g -O2 194 195 196 197 GNU_TOOLCHAIN_PREFIX = $(CROSS_COMPILE) 198 199 # The C program compiler. 200 201 CC = $(GNU_TOOLCHAIN_PREFIX)gcc 202 203 # The C++ program compiler. 204 205 # CXX = $(GNU_TOOLCHAIN_PREFIX)g++ 206 207 # Un-comment the following line to compile C programs as C++ ones. 208 209 # CC = $(CXX) 210 211 # The command used to delete file. 212 213 RM = rm -f 214 215 216 217 ETAGS = etags 218 219 ETAGSFLAGS = 220 221 222 223 CTAGS = ctags 224 225 CTAGSFLAGS = 226 227 #VERBOSE_COMMAND = noquiet 228 229 ## Quiet commands 230 231 ifeq ($(VERBOSE_COMMAND),) 232 233 Q = @ 234 235 Q_compile = @echo ‘ CC $< => $@‘; 236 237 Q_link = @echo ‘ LD $@‘; 238 239 Q_ar = @echo ‘ AR $@‘; 240 241 Q_mkdir = echo ‘ MKDIR $1‘; 242 243 Q_clean = @echo ‘ CLEAN‘; 244 245 Q_distclean = @echo ‘ DISTCLEAN‘; 246 247 endif 248 249 ## Stable Section: usually no need to be changed. But you can add more. 250 251 ##========================================================================== 252 253 SHELL = /bin/sh 254 255 EMPTY = 256 257 SPACE = $(EMPTY) $(EMPTY) 258 259 ifeq ($(PROGRAM),) 260 261 CUR_PATH_NAMES = $(subst /,$(SPACE),$(subst $(SPACE),_,$(CURDIR))) 262 263 PROGRAM = $(word $(words $(CUR_PATH_NAMES)),$(CUR_PATH_NAMES)) 264 265 ifeq ($(PROGRAM),) 266 267 PROGRAM = a.out 268 269 endif 270 271 endif 272 273 ifeq ($(SRCDIRS),) 274 275 SRCDIRS = . 276 277 endif 278 279 ALL_SRS = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS)))) 280 281 SOURCES = $(filter-out $(EX_SRCS),$(ALL_SRS)) 282 283 HEADERS = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(HDREXTS)))) 284 285 286 287 CFLAGS = $(foreach i,$(SRCDIRS),-I$i) 288 289 290 291 SRC_CXX = $(filter-out %.c,$(SOURCES)) 292 293 OBJS = $(addsuffix .o, $(basename $(SOURCES))) 294 295 DEPS = $(OBJS:.o=.d) 296 297 298 299 ## Define some useful variables. 300 301 DEP_OPT = $(shell if `$(CC) --version | grep "GCC" >/dev/null`; then 302 303 echo "-MM -MP"; else echo "-M"; fi ) 304 305 DEPEND = $(CC) $(DEP_OPT) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS) 306 307 DEPEND.d = $(subst -g ,,$(DEPEND)) 308 309 COMPILE.c = $(Q_compile)$(CC) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS) -c 310 311 COMPILE.cxx = $(Q_compile)$(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c 312 313 LINK.c = $(Q_link)$(CC) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) 314 315 LINK.cxx = $(Q_link)$(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) 316 317 318 319 .PHONY: all objs tags ctags clean distclean help show 320 321 322 323 # Delete the default suffixes 324 325 .SUFFIXES: 326 327 328 329 all: $(PROGRAM) 330 331 332 333 # Rules for creating dependency files (.d). 334 335 #------------------------------------------ 336 337 338 339 %.d:%.c 340 341 @echo -n $(dir $<) > $@ 342 343 @$(DEPEND.d) $< >> $@ 344 345 346 347 %.d:%.C 348 349 @echo -n $(dir $<) > $@ 350 351 @$(DEPEND.d) $< >> $@ 352 353 354 355 %.d:%.cc 356 357 @echo -n $(dir $<) > $@ 358 359 @$(DEPEND.d) $< >> $@ 360 361 362 363 %.d:%.cpp 364 365 @echo -n $(dir $<) > $@ 366 367 @$(DEPEND.d) $< >> $@ 368 369 370 371 %.d:%.CPP 372 373 @echo -n $(dir $<) > $@ 374 375 @$(DEPEND.d) $< >> $@ 376 377 378 379 %.d:%.c++ 380 381 @echo -n $(dir $<) > $@ 382 383 @$(DEPEND.d) $< >> $@ 384 385 386 387 %.d:%.cp 388 389 @echo -n $(dir $<) > $@ 390 391 @$(DEPEND.d) $< >> $@ 392 393 394 395 %.d:%.cxx 396 397 @echo -n $(dir $<) > $@ 398 399 @$(DEPEND.d) $< >> $@ 400 401 402 403 # Rules for generating object files (.o). 404 405 #---------------------------------------- 406 407 objs:$(OBJS) 408 409 410 411 %.o:%.c 412 413 $(COMPILE.c) $< -o $@ 414 415 416 417 %.o:%.C 418 419 $(COMPILE.cxx) $< -o $@ 420 421 422 423 %.o:%.cc 424 425 $(COMPILE.cxx) $< -o $@ 426 427 428 429 %.o:%.cpp 430 431 $(COMPILE.cxx) $< -o $@ 432 433 434 435 %.o:%.CPP 436 437 $(COMPILE.cxx) $< -o $@ 438 439 440 441 %.o:%.c++ 442 443 $(COMPILE.cxx) $< -o $@ 444 445 446 447 %.o:%.cp 448 449 $(COMPILE.cxx) $< -o $@ 450 451 452 453 %.o:%.cxx 454 455 $(COMPILE.cxx) $< -o $@ 456 457 458 459 # Rules for generating the tags. 460 461 #------------------------------------- 462 463 tags: $(HEADERS) $(SOURCES) 464 465 $(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES) 466 467 468 469 ctags: $(HEADERS) $(SOURCES) 470 471 $(CTAGS) $(CTAGSFLAGS) $(HEADERS) $(SOURCES) 472 473 474 475 # Rules for generating the executable. 476 477 #------------------------------------- 478 479 $(PROGRAM):$(OBJS) 480 481 ifeq ($(SRC_CXX),) # C program 482 483 $(LINK.c) $(OBJS) $(MY_LIBS) -o $@ 484 485 @echo Type ./$@ to execute the program. 486 487 else # C++ program 488 489 $(LINK.cxx) $(OBJS) $(MY_LIBS) -o $@ 490 491 @echo Type ./$@ to execute the program. 492 493 endif 494 495 cp -f $(PROGRAM) $(MY_INSTALLPROGRAM) 496 497 ifndef NODEP 498 499 ifneq ($(DEPS),) 500 501 sinclude $(DEPS) 502 503 endif 504 505 endif 506 507 508 509 install: 510 511 cp -f $(PROGRAM) $(MY_INSTALLPROGRAM) 512 513 clean: 514 515 $(Q_clean)$(RM) $(OBJS) $(PROGRAM) $(PROGRAM).elf 516 517 518 519 distclean: clean 520 521 $(Q_distclean)$(RM) $(DEPS) TAGS 522 523 524 525 # Show help. 526 527 help: 528 529 @echo ‘Generic Makefile for C/C++ Programs (gcmakefile) version 0.5‘ 530 531 @echo ‘Copyright (C) 2007, 2008 whyglinux <whyglinux@hotmail.com>‘ 532 533 @echo 534 535 @echo ‘Usage: make [TARGET]‘ 536 537 @echo ‘TARGETS:‘ 538 539 @echo ‘ all (=make) compile and link.‘ 540 541 542 543 @echo ‘ NODEP=yes make without generating dependencies.‘ 544 545 @echo ‘ objs compile only (no linking).‘ 546 547 @echo ‘ tags create tags for Emacs editor.‘ 548 549 @echo ‘ ctags create ctags for VI editor.‘ 550 551 @echo ‘ clean clean objects and the executable file.‘ 552 553 @echo ‘ distclean clean objects, the executable and dependencies.‘ 554 555 @echo ‘ show show variables (for debug use only).‘ 556