⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test-database-get.c

📁 linux下的电话本的最底层
💻 C
字号:
/*	librecord2 - Record Object manipulation and storage library 2 * *	Authors: YE Nan <nan.ye@orange-ftgroup.com>  *	 *	This software and associated documentation files (the "Software")  *	are copyright (C) 2005 LiPS Linux Phone Standards Forum [FranceTelecom]  *	All Rights Reserved.  * *	A copyright license is hereby granted for redistribution and use of  *	the Software in source and binary forms, with or without modification,  *	provided that the following conditions are met:  *	- Redistributions of source code must retain the above copyright notice,  *	this copyright license and the following disclaimer.  *  - Redistributions in binary form must reproduce the above copyright  * 	notice, this copyright license and the following disclaimer in the  *	documentation and/or other materials provided with the distribution.  *	- Neither the name of LiPS  nor the names of its Members may be used  *	to endorse or promote products derived from the Software without  *	specific prior written permission.  * *	A patent license for any Necessary Claims owned by Members of LiPS Forum  *	to make, have made, use, import, offer to sell, lease and sell or otherwise  *	distribute any implementation compliant with the any specification adopted  *	by the LiPS Forumcan be obtained from the respective Members on reasonable  *	and non-discriminatory terms and conditions and under reciprocity, as  *	regulated in more detail in the Internal Policy of the LiPS Forum.  * *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER, ITS MEMBERS AND CONTRIBUTORS  *	"AS IS", AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,  *	THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE  *	AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER,  *	ITS MEMBERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,  *	SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  *	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  *	OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  *	WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  *	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  *	POSSIBILITY OF SUCH DAMAGE.  */#include <stdio.h>#include <stdlib.h>#include <time.h>#include <glib.h>#include <database.h>#define TEMPL_FNAME			"./sample.templ"#define	DB_FNAME				"./sample.db"#define	DB_ELEMENT_NAME	"sample"typedef enum {	SEL_UID = 1,	SEL_ADV,	SEL_QUIT} KeySelection;typedef enum {	REC_FIELD_UID						= 1,	REC_FIELD_NAME					= 3,	REC_FIELD_MOBILE				= 6,	REC_FIELD_NUM_HOME			= 7,	REC_FIELD_NUM_OFFICE		= 8,	REC_FIELD_FAX						= 9,	REC_FIELD_COMPANY				= 10,	REC_FIELD_DEPART				= 11,	REC_FIELD_WEBSITE				= 12,	REC_FIELD_EMAIL					= 13,	REC_FIELD_PICTURE				= 14,	REC_FIELD_RINGTONE			= 15,	REC_FIELD_ADDRESS				= 16,	REC_FIELD_CUSTOM_FLOAT	= 17,	REC_FIELD_CUSTOM_BINARY	= 18,} RecordFields;FieldTemplate * templ = NULL;static voidlist_record (RecordDBElement	*element,						 guint32					 uid){	Record * record = NULL;	RecordDB * db = NULL;		db = record_db_element_get_db(element);	record_db_open(db);	record_db_trans_begin(db);	record_db_element_get_record(element,															 &record,															 uid);	record_db_trans_commit(db);	record_db_close(db);	record_print(record);		record_free(record);		return;}static voidlist_records (RecordDBElement	*element,							Record					*sample,							gboolean				 fuzzy,							gboolean				 intersect,							gboolean				 count_only){	RecordDBError error;	RecordDB * db = NULL;		db = record_db_element_get_db(element);		record_db_open(db);	record_db_trans_begin(db);	if (count_only)	{		guint count = 0;		error = record_db_element_get_records_count(element,																								sample,																								fuzzy,																								intersect,																								&count);		record_db_trans_commit(db);		g_print("%s(): %d record(s) matched\n",						__FUNCTION__,						count);	}	else	{		Iterator * iter = NULL;			error = record_db_element_get_records_extentions(element,																					&iter,																					sample,																					fuzzy,																					intersect,																					"AND ftid3 LIKE 'Person%.Demo' LIMIT 3");		record_db_trans_commit(db);		if (iter == NULL)		{			g_print("%s(): Empty results set. ERR %d\n",							__FUNCTION__,							error);		}		else		{			for (iterator_to_first(iter);					 !iterator_at_last(iter);					 iterator_next(iter))			{				Record * record = (Record *)iterator_current(iter);								record_print(record);			}				g_print("%s(): count = %d\n",							__FUNCTION__,							iterator_size(iter));			iterator_free(iter, TRUE);		}	}		record_db_close(db);	return;}voidshow_menu (void){	g_print("-------- Menu --------\n");	g_print("1. Search by UID\n");	g_print("2. Search advanced\n");	g_print("3. Quit\n");	g_print("----------------------\n");	g_print(":");		return;}int main (int argc, char *argv[]){	RecordDB * db = NULL;	RecordDBElement * element = NULL;	gboolean endflag = FALSE;		templ = field_template_new_from_file(TEMPL_FNAME);		db = record_db_new(DB_FNAME);	element = record_db_element_new(db,										 							DB_ELEMENT_NAME,										 							templ);	while (!endflag)	{		gchar selection = 0;				show_menu();		scanf("%c", &selection);    selection -= '0';		switch (selection)		{			case SEL_UID:			{				guint32 uid = 0;								g_print("entring UID:");				scanf("%d", &uid);				g_print("%s(): retrieve record(uid = %d)...\n",								__FUNCTION__,								uid);				list_record(element, uid);				break;			}						case SEL_ADV:			{				Record * sample = NULL;				gboolean fuzzy = FALSE;				gboolean intersect = FALSE;				gboolean count_only = FALSE;				gchar ch;								g_print("%s(): fuzzy search? [Y/n]",								__FUNCTION__);				scanf("\n%c", &ch);				fuzzy = (ch == 'Y' || ch == 'y') ? TRUE : FALSE; 				g_print("%s(): intersect search? [Y/n]",								__FUNCTION__);				scanf("\n%c", &ch);				intersect = (ch == 'Y' || ch == 'y') ? TRUE : FALSE; 				g_print("%s(): retrieve count only? [Y/n]",								__FUNCTION__);				scanf("\n%c", &ch);				count_only = (ch == 'Y' || ch == 'y') ? TRUE : FALSE; 								g_print("%s(): querying ... [fuzzy = %s, intersect = %s, count_only = %s]\n",								__FUNCTION__,								(fuzzy ? "TRUE" : "FALSE"),								(intersect ? "TRUE" : "FALSE"),								(count_only ? "TRUE" : "FALSE"));							sample = record_new(templ);				record_set_field_default(sample,																 REC_FIELD_MOBILE,																 "13%",																 FIELD_SIZE_AUTO);				record_set_field_default(sample,																 REC_FIELD_NUM_OFFICE,																 "13%",																 FIELD_SIZE_AUTO);				record_set_field_default(sample,																 REC_FIELD_NUM_HOME,																 "83%",																 FIELD_SIZE_AUTO);						list_records(element,										 sample,										 fuzzy,										 intersect,										 count_only);				break;			}						case SEL_QUIT:				endflag = TRUE;				break;		}	}		record_db_element_free(element);	record_db_free(db);		field_template_free(templ);		return EXIT_SUCCESS;}/*vi:ts=2:nowrap:ai:expandtab */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -