int column_names_printed = 0; void print_row(int n_values,char** values) { int i; for (i = 0; i < n_values; ++i) { printf("%10s",values[i]); } printf("/n"); } int print_result(void* data,int n_columns,char** column_values,char** column_names) { if (!column_names_printed) { print_row(n_columns,column_names); column_names_printed = 1; } print_row(n_columns,column_values); return 0; } int sqlite() { sqlite3 *db=NULL; char *errMsg = NULL; int rc; rc = sqlite3_open("test.db",&db); if( rc ){ fprintf(stderr,"Can't open database: %s/n",sqlite3_errmsg(db)); sqlite3_close(db); exit(1); } else printf("open test.db successfully!/n"); rc = sqlite3_exec(db,"BEGIN TRANSACTION;",NULL,NULL); rc = sqlite3_exec(db,"CREATE TABLE students(number varchar(10),name varchar(10),/ sex varchar(6),age varchar(2));","INSERT INTO students VALUES('00001','Mary','female','15');/ INSERT INTO students VALUES('00002','John','male','16');/ INSERT INTO students VALUES('00003','Mike','15');/ INSERT INTO students VALUES('00004','Kevin','17');/ INSERT INTO students VALUES('00005','Alice','14');/ INSERT INTO students VALUES('00006','Susan','16');/ INSERT INTO students VALUES('00007','Christina','15');/ INSERT INTO students VALUES('00008','Brian','16');/ INSERT INTO students VALUES('00009','Dennis','14');/ INSERT INTO students VALUES('00010','Daphne','18');",&errMsg); rc = sqlite3_exec(db,"COMMIT TRANSACTION;",NULL); rc = sqlite3_exec(db,"Create Trigger UserOK_DeleteOK AFTER DELETE On students FOR EACH ROW / begin / delete from students where number = '00001' and old.number = '00010' ;/ end;",&errMsg); printf("error code: %d/n",rc); printf("error message: %s/n",errMsg); sqlite3_close(db); return 0; }
原文链接:https://www.f2er.com/sqlite/202951.html