SQLite C++获取表信息、获取列名、获取列类型

前端之家收集整理的这篇文章主要介绍了SQLite C++获取表信息、获取列名、获取列类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

废话不说,直接上代码

struct table
{
	string name;
	string createsql;
};

//获取表信息
void GetTables(vector<table>& vecTable)
{
	char *szError= new char[256];
	sqlite3_stmt *stmt = NULL;
	sqlite3_prepare((sqlite3*)m_pDb,"select name,sql from sqlite_master where type='table'  order by name",-1,&stmt,0);	
	vector<string> vecTables;
	if(stmt) 
	{
		while(sqlite3_step(stmt) == sqlITE_ROW) 
		{		
			table tb;
			tb.name =(char *)sqlite3_column_text(stmt,0);
			tb.createsql = (char *)sqlite3_column_text(stmt,1);
			vecTable.push_back(tb);
		}
		sqlite3_finalize(stmt);
		stmt = NULL;
	}
}

//获取列名
bool GetColName(vector<string>& vecColName,string strTableName)
{
	sqlite3_stmt *stmt = NULL;
	char sql[200];
	sprintf(sql,"SELECT * FROM %s limit 0,1",strTableName.c_str());
	char **pRes=NULL;
	int nRow=0,nCol=0;
	char *pErr=NULL;

	//第一行是列名称
	sqlite3_get_table((sqlite3*)m_pDb,sql,&pRes,&nRow,&nCol,&pErr);	
	for (int i=0; i<nRow; i++)
	{
		for (int j=0; j<nCol; j++)
		{
			char *pv = *(pRes+nCol*i+j);
			vecColName.push_back(pv);
		}
		break;
	}

	if (pErr!=NULL)
	{
		sqlite3_free(pErr);
	}
	sqlite3_free_table(pRes);
	return true;
}


//获取列类型
bool GetColType(vector<int>& vecType,strTableName.c_str());
	sqlite3_prepare((sqlite3*)m_pDb,0);	
	if(stmt) 
	{
		while(sqlite3_step(stmt) == sqlITE_ROW) 
		{	
			int nCount = sqlite3_column_count(stmt);
			for (int i=0; i<nCount; i++)
			{
				int nValue = sqlite3_column_int(stmt,0);
				int nType = sqlite3_column_type(stmt,i);
				vecType.push_back(nType);
				switch (nType)
				{
				case 1:
					//sqlITE_INTEGER
					break;
				case 2:
					//sqlITE_FLOAT
					break;
				case 3:
					//sqlITE_TEXT
					break;
				case 4:
					//sqlITE_BLOB
					break;
				case 5:
					//sqlITE_NULL
					break;
				}
			}
			break;
		}
		sqlite3_finalize(stmt);
		stmt = NULL;
	}
	return true;
}
原文链接:https://www.f2er.com/sqlite/199168.html

猜你在找的Sqlite相关文章