int main(int argc, char *argv[]) { Database *db; LinkedList *ll; db = db_create("project_seven"); //CREATE DATABASE project_seven; tbl_delete(db, "animals"); //DROP TABLE IF EXISTS animals; func_delete(db, "num_animals"); //DROP FUNCTION IF EXISTS num_animals; tbl_create(db, "animals"); //CREATE TABLE animals /* Capabilities of this function changed to make implementation easier. The purpose of this function is to demonstrate memory management */ func_create(db, "num_animals", num_animals); //CREATE FUNCTION num_animals() /*Creates rows in table*/ ll = ll_create(); ll_append(ll, "Cow"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Cow"); ll = ll_create(); ll_append(ll, "Dog"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Dog"); /*Printing tables, and rows created*/ db_print(db); //SHOW TABLES; tbl_print(db, "animals"); //SELECT * FROM animals; /*Deallocating memory*/ tbl_clear(db, "animals"); //DELETE FROM animals; tbl_print(db, "animals"); //SELECT * FROM animals; /*Allocating so that we can deallocate again*/ ll = ll_create(); ll_append(ll, "Pig"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Pig"); ll = ll_create(); ll_append(ll, "Cat"); tbl_insert(db, "animals", ll); //INSERT INTO animals (animal) VALUES ("Cat"); tbl_print(db, "animals"); //SELECT * FROM animals; /*Deallocating memory*/ tbl_clear(db, "animals"); //TRUNCATE TABLE animals; tbl_print(db, "animals"); //SELECT * FROM animals; tbl_delete(db, "animals"); //DROP TABLE IF EXISTS animals; func_delete(db, "num_animals"); //DROP FUNCTION IF EXISTS num_animals; db_print(db); //SHOW TABLES; db_delete(db); //DROP DATABASE IF EXISTS project_seven; return 0; }
static bool create_db(void) { acl::db_sqlite db(__dbfile); if (db.open() == false) { printf("open dbfile: %s error\r\n", __dbfile.c_str()); return (false); } db.show_conf(); return (tbl_create(db)); }
int main(void) { acl::string dbfile("测试.db"); acl::db_sqlite db(dbfile); int max = 100; if (db.open() == false) { printf("open dbfile: %s error\r\n", dbfile.c_str()); getchar(); return 1; } printf("open dbfile %s ok\r\n", dbfile.c_str()); if (tbl_create(db) == false) { printf("create table error\r\n"); getchar(); return 1; } // 配置数据库引擎 db.set_conf("PRAGMA synchronous = off"); db.set_conf("PRAGMA encoding = \"UTF-8\""); acl::string buf; if ((db.get_conf("PRAGMA encoding", buf))) printf(">>PRAGMA encoding: %s\r\n", buf.c_str()); db.show_conf(); acl::meter_time(__FILE__, __LINE__, "---begin insert---"); for (int i = 0; i < max; i++) { bool ret = tbl_insert(db, i); if (ret) printf(">>insert ok: i=%d, affected: %d\r", i, db.affect_count()); else printf(">>insert error: i = %d\r\n", i); } printf("\r\n"); printf(">>insert total affect: %d\n", db.affect_total_count()); acl::meter_time(__FILE__, __LINE__, "---end insert---"); acl::meter_time(__FILE__, __LINE__, "---begin select---"); int n = 0; for (int i = 0; i < max; i++) { int ret = tbl_select(db, i); if (ret >= 0) { n += ret; printf(">>select ok: i=%d, ret=%d\r", i, ret); } else printf(">>select error: i = %d\r\n", i); } printf("\r\n"); printf(">>select total: %d\r\n", n); acl::meter_time(__FILE__, __LINE__, "---end select---"); acl::meter_time(__FILE__, __LINE__, "---begin delete---"); for (int i = 0; i < max; i++) { bool ret = tbl_delete(db, i); if (ret) printf(">>delete ok: %d, affected: %d\r", i, (int) db.affect_count()); else printf(">>delete error: i = %d\r\n", i); } printf("\r\n"); printf(">>delete total affected: %d\n", db.affect_total_count()); acl::meter_time(__FILE__, __LINE__, "---end delete---"); printf("Enter any key to exit.\r\n"); getchar(); return 0; }