欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

test-sqlite3.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 <sqlite3.h>#define	CREATE_TABLE_STRING			"CREATE TABLE IF NOT EXISTS test (uid INTEGER, label TEXT, desc TEXT, PRIMARY KEY (uid) );"#define	INSERT_TEMPLATE_STRING	"INSERT INTO test (label, desc) VALUES (:001, $001);"#define	DB_FNAME								"./test.db"#define	ROW_MAX									1int main (int argc, char *argv[]){	sqlite3 * database = NULL;	sqlite3_stmt * pStmt = NULL;	gchar * errmsg = NULL;	gint ret;		if (sqlite3_open(DB_FNAME,									 &database) != SQLITE_OK)	{		g_print("%s(): open database failed: %s.\n",						__FUNCTION__,						sqlite3_errmsg(database));		goto failure;	}		if (sqlite3_exec(database,									 CREATE_TABLE_STRING,									 NULL,									 NULL,									 &errmsg) != SQLITE_OK)	{		g_print("%s(): create extend table failed: %s.\n",						__FUNCTION__,						errmsg);			goto failure;	}		do	{		gint i;				ret = sqlite3_prepare(database,													INSERT_TEMPLATE_STRING,													-1,													&pStmt,													0);		if (ret != SQLITE_OK)		{			g_print("%s(): sqlite3_prepare() failed: %s.\n",							__FUNCTION__,							sqlite3_errmsg(database));			goto failure;		}		if (TRUE)		{			gint index;						index = sqlite3_bind_parameter_index(pStmt, ":001");			g_print("%s(): colname = %s, index = %d\n",							__FUNCTION__,							":001",							index);			sqlite3_bind_text(pStmt,											 index,											 "label",											 -1,											 SQLITE_TRANSIENT);						index = sqlite3_bind_parameter_index(pStmt, "$001");			g_print("%s(): colname = %s, index = %d\n",							__FUNCTION__,							"$001",							index);			sqlite3_bind_text(pStmt,											 index,											 "desc",											 -1,											 SQLITE_TRANSIENT);		}		ret = sqlite3_step(pStmt);		if (ret == SQLITE_ROW)		{			goto failure;		}				if (ret != SQLITE_DONE)		{			g_print("%s(): sqlite3_step() WARNING: main, %d[%s].\n",							__FUNCTION__,							sqlite3_errcode(database),							sqlite3_errmsg(database));			if (ret == SQLITE_BUSY)			{				sqlite3_finalize(pStmt);								goto failure;			}		}				ret = sqlite3_finalize(pStmt);	} while (ret == SQLITE_SCHEMA);		sqlite3_close(database);			return EXIT_SUCCESS;failure:	sqlite3_close(database);		return EXIT_FAILURE;}/*vi:ts=2:nowrap:ai:expandtab */

⌨️ 快捷键说明

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