#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
#include <stdarg.h>
#include "database.h"
#include "cjson/cJSON.h"

void main()
{
    void * hdbc;
	
	/* 连接数据库 */
    hdbc = connect_database(1);

    /* 创建table, 其中列类型包括长整形、浮点型、字符串 */
    ret = create_database_table(1, hdbc, "ctest", "create table ctest ( id bigint,  fl double,  dt character(10), status character(7))");

    /* 向table插入数据 */
	for (i = 1; i < 6; i++)
	{
		char sql[256];
		sprintf( sql, "insert into ctest values( %ld, '%lf', '%s', '%s' )", i, i, i, i );
		ret = update_database(1, hdbc, 1, "ctest", sql, 0);
	}


    /* 使用参数化方法插入数据 */
    ret = update_database(1, hdbc, 1, "ctest", "insert into ctest values(?, ?, ?, '30' )", 3, 
								DB_DATA_INT_TYPE, 4, 91123123333333333, 
								DB_DATA_FLOAT_TYPE, 8, 11111111.555555555,  
								DB_DATA_STRING_TYPE, 4, "TEMP");

    /* 使用参数化方法更新数据 */
    ret = update_database(1, hdbc, 1, "ctest", "update ctest set dt = 'database' where status = ?", 1, DB_DATA_STRING_TYPE, 4, "00017");

    /* 使用参数化方法删除数据 */
    ret = update_database(1, hdbc, 1, "ctest", "delete from ctest where status = ?", 1, DB_DATA_STRING_TYPE, 4, "30");

    /* 使用参数化方法查询数据库 */
	int num;
	char * retptr = NULL;
	retptr = select_datebase_by_number(1, hdbc, "ctest", "select * from ctest where id = ?", 1, 10, &num, 1, DB_DATA_INT_TYPE, 4, 91123123333333333);
	
    /* 释放查询数据库申请的内存 */
    ret = free_database_memory(1, "ctest", retptr);

    /* 根据指定信息查询数据库的获取的结果的条目数 */
    ret = get_select_datebase_number(1, hdbc, "ctest", "select * from ctest where id = '911231233333333337'", &num, 0);

    /* 使用参数化方法根据指定信息查询数据库的获取的结果的条目数 */
    ret = get_select_datebase_number(1, hdbc, "ctest", "select * from ctest where id = ?", &num, 1, DB_DATA_INT_TYPE, 4, 911231233333333337);

    /* 释放数据库连接 */
    ret = disconnect_database(1, hdbc);

    return;
}