Пример #1
0
void
rsdb_exec_fetch(struct rsdb_table *table, const char *format, ...)
{
    static char buf[BUFSIZE * 4];
    va_list args;
    char *errmsg;
    char **data;
    int pos;
    unsigned int retval;
    int i, j;

    va_start(args, format);
    retval = vsnprintf(buf, sizeof(buf), format, args);
    va_end(args);

    if(retval >= sizeof(buf)) {
        mlog("fatal error: length problem with compiling sql");
    }

    if((retval =
            sqlite3_get_table(rb_bandb, buf, &data, &table->row_count, &table->col_count, &errmsg))) {
        int success = 0;

        switch (retval) {
        case SQLITE_BUSY:
            for(i = 0; i < 5; i++) {
                rb_sleep(0, 500000);
                if(!sqlite3_get_table
                   (rb_bandb, buf, &data, &table->row_count, &table->col_count,
                    &errmsg)) {
                    success++;
                    break;
                }
            }

            if(success)
                break;

            mlog("fatal error: problem with db file: %s", errmsg);
            break;

        default:
            mlog("fatal error: problem with db file: %s", errmsg);
            break;
        }
    }

    /* we need to be able to free data afterward */
    table->arg = data;

    if(table->row_count == 0) {
        table->row = NULL;
        return;
    }

    /* sqlite puts the column names as the first row */
    pos = table->col_count;
    table->row = rb_malloc(sizeof(char **) * table->row_count);
    for(i = 0; i < table->row_count; i++) {
        table->row[i] = rb_malloc(sizeof(char *) * table->col_count);

        for(j = 0; j < table->col_count; j++) {
            table->row[i][j] = data[pos++];
        }
    }
}
Пример #2
0
DLL_FUNCTION(int32_t) BU_SQLite_Get_Table(sqlite3* db, const char* zSql, char*** pazResult, int32_t* pnRow, int32_t* pnColumn, char** pzErrMessage) {
#pragma comment(linker, "/EXPORT:BU_SQLite_Get_Table=_BU_SQLite_Get_Table@24")
	return sqlite3_get_table(db, zSql, pazResult, pnRow, pnColumn, pzErrMessage);
}
Пример #3
0
void EvaUserSetting::loadMsgToSql(const TQString fullName,const bool isQunMsg)
{
	TQFile file(fullName);
	if(!file.open(IO_ReadOnly))
		return ;
	TQString fullNameSql = getEvaUserDir() + "/"+SqlFileName;
	int result;
	char * errmsg = NULL;
	char **dbResult;
	int nRow, nColumn,i=0;
	sqlite3 *db=NULL;
	result=sqlite3_open(fullNameSql.data(),&db);
	if( result != SQLITE_OK ) return;
	TQString sql;
	sqlite3_exec( db,"begin transaction;",0,0,&errmsg);

	TQ_UINT32 r_buddy;
	TQ_UINT32 sender;
	TQString  sNick;
	TQ_UINT32 receiver;
	TQString  rNick;
	TQ_UINT8  type; // 0 auto reply,  1 normal
	TQString  message;
	TQ_UINT32  intTime;
	TQDateTime time;
	TQ_UINT8   fontSize;
	TQ_UINT8   flag; // start from right.  bit 0: u, bit 1: i, bit 2: b
	TQ_UINT8   blue;
	TQ_UINT8   green;
	TQ_UINT8   red;
	TQDataStream stream(&file);
	std::list<chatMessage>srclist;
	while(!stream.atEnd()){
		stream>>r_buddy;
		stream>>sender;
		stream>>sNick;
		stream>>receiver;
		stream>>rNick;
		stream>>type;
		stream>>message;
		stream>>intTime;
		stream>>fontSize;
		stream>>flag;
		stream>>blue;
		stream>>green;
		stream>>red;
		sql.sprintf("select sender from chat where sender=%d and receiver=%d and time=%d and isQunMsg=%d ",sender,receiver,intTime,isQunMsg);
		result = sqlite3_get_table(db,sql.utf8().data(), &dbResult,&nRow, &nColumn, &errmsg);
		if( SQLITE_OK != result ) 
		{
			sqlite3_exec( db,"commit  transaction;",0,0,&errmsg);
			return;
		}
		if(nRow>0) continue; //找到重复的记录
		i++;
		sql.sprintf("insert into chat values (%d,%d,'%s', %d,'%s',%d, '%s', %d,%d, %d, %d,%d,%d,%d)",r_buddy,sender,sNick.local8Bit().data(),receiver,rNick.local8Bit().data(),type,message.local8Bit().data(),intTime,fontSize,flag,blue,green,red,isQunMsg);
		sqlite3_exec( db , sql.utf8().data() , 0 , 0 , &errmsg );
	}
	printf("load %s %i条\n",fullName.data(),i);
	sqlite3_exec( db,"commit transaction;",0,0,&errmsg);
	file.close();
	TQString newfile;
	newfile=fullName+".old";
	unlink(newfile.local8Bit().data());
	rename(fullName.local8Bit().data(),newfile.local8Bit().data());
	return ;
}
Пример #4
0
BOOL CUserLoginDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	
	HICON m_hIcon;
	m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);//修改对话框的图标
	SetIcon(m_hIcon,TRUE);


	// TODO:  在此添加额外的初始化
	ModifyStyleEx(0,WS_EX_APPWINDOW);
	GetClientRect(&m_rect);
	SetBackgroundColor(RGB(247,252,255));
	//初始化全局变量g_CurrentDir(当前目录绝对路径)
	GetCurrentDirectory(MAX_PATH,g_CurrentDir.GetBuffer(MAX_PATH));
	g_CurrentDir.ReleaseBuffer(); 
	//提取之前保存的用户信息,如果有的话,利用sqlite数据库
	sqlite3* UserInfodb = NULL;
	//打开数据库,不存在则创建
	if(sqlite3_open("User\\UserInfo.db3", &UserInfodb) != 0)
	{
		MessageBox(L"打开用户信息列表失败",L"初始化",MB_ICONERROR);
		return FALSE;
	}
	//创建用户信息表格,已存在则创建失败,错误信息保存在szErrmsg中
	char *sqlcmd = "CREATE TABLE UserInfo(\
				     UserName string PRIMARY KEY,\
					 PassWord string )";

	char *szErrmsg = NULL;
	sqlite3_exec(UserInfodb,sqlcmd,0,0,&szErrmsg);
	//查询用户数据
	char *zsql = "SELECT * FROM UserInfo";
	char **szResult = NULL;
	int nRow = 0;
	int nColumn = 0;
	sqlite3_get_table(UserInfodb,zsql,&szResult,&nRow,&nColumn,&szErrmsg);

	char username[MAX_USERNAMELEN] = {0};
	char passwordEncodeHex[500] = {0};
	char *passwordEncode = NULL;
	memset(username,0,MAX_USERNAMELEN);
	memset(passwordEncodeHex,0,500);
	int l = strlen(szResult[3]);
	if(nRow)
	{
		memcpy(username,szResult[2],strlen(szResult[2]));
		memcpy(passwordEncodeHex,szResult[3],strlen(szResult[3]));
	}

	m_UserIniFilePath = g_CurrentDir;
	m_UserIniFilePath+=L"\\config\\config.ini";
	m_UserName = Char2CString(username);
	m_CheckPW=GetPrivateProfileInt(L"UserSetting",L"SavePassword",0,m_UserIniFilePath);
	if(strlen(passwordEncodeHex))
	{
		//如果密码不为空,则解密
		char *userKey = "1234567887654321"; // 原始密钥128位,16字节
		CString sDataLen;
		GetPrivateProfileString(L"UserSetting",L"PasswordEnCodeLen",L"",sDataLen.GetBuffer(10),10,m_UserIniFilePath);
		int datalen = _wtoi(sDataLen);
		char keyDecode[256]; //解密密钥
		char *PassWordDecoded = NULL;
		//char *dataSrc = NULL; //补齐的数据
		//将转为16进制串的密码转为初始状态

		int L=strlen(passwordEncodeHex);
		passwordEncode = (char*)malloc(sizeof(char)*(L));
		memset(passwordEncode,0,L);
		int  x;
		for(int i=0;i<L/2;i++) 
		{		
			sscanf(passwordEncodeHex+i*2,"%02X",&x);
			passwordEncode[i]=(char)x;//(x&0xFFu);
		}
		unsigned char in[16];
		unsigned char out[16];
		PassWordDecoded = (char*)malloc(sizeof(char)*datalen);
		if(!PassWordDecoded)
		{
			return -1;
		}
		memset(PassWordDecoded,0,datalen);
		//设置解密密钥
		if(0!=AES_set_decrypt_key((const unsigned char *)userKey,128,(AES_KEY *)keyDecode))
		{
			return -1;
		}
		//循环加密,以16字节为单位
		int count = datalen/16;
		//解密
		for(int i = 0;i<count;++i)
		{
			memset(in, 0, 16);             
			memset(out, 0, 16); 	
			memcpy(in,passwordEncode+i*16,16);
			AES_ecb_encrypt(in,out,(AES_KEY *)keyDecode,AES_DECRYPT);
			memcpy(PassWordDecoded+i*16,out,16);
		}
		//初始化密码
		m_PassWord = Char2CString(PassWordDecoded);
		//释放资源
		free(passwordEncode);
		passwordEncode = NULL;
		free(PassWordDecoded);
		PassWordDecoded = NULL;
	}
	//关闭数据库
	sqlite3_close(UserInfodb);
	UserInfodb = NULL;	
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
Пример #5
0
int
do_test (sqlite3 * db_handle)
{
#ifndef OMIT_ICONV		/* only if ICONV is supported */
    char *sql_statement;
    int ret;
    char *err_msg = NULL;
    char **results;
    int rows;
    int columns;

    ret =
	sqlite3_exec (db_handle,
		      "create VIRTUAL TABLE shapetest USING VirtualShape(\"shapetest1\", UTF-8, 4326);",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -2;
      }
    ret =
	sqlite3_get_table (db_handle,
			   "SELECT RegisterVirtualGeometry('shapetest')",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "RegisterVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -3;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -4;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -5;
      }
    sqlite3_free_table (results);


    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 < 20;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -6;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -7;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -8;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -9;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -10;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -11;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 <= 19;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -12;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -13;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -14;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -15;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -16;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -17;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 = 20;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -18;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -19;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -20;
      }
    if (strcmp (results[3], "orde lees") != 0)
      {
	  fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n",
		   results[3]);
	  return -21;
      }
    if (strcmp (results[4], "20") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer2() bad result: %s.\n",
		   results[4]);
	  return -22;
      }
    if (strcmp (results[5], "POINT(3482470.825574 4495691.054818)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -23;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 > 2;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -24;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -25;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -26;
      }
    if (strcmp (results[3], "orde lees") != 0)
      {
	  fprintf (stderr, "Unexpected error: orde lees2 bad result: %s.\n",
		   results[3]);
	  return -27;
      }
    if (strcmp (results[4], "20") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer4() bad result: %s.\n",
		   results[4]);
	  return -28;
      }
    if (strcmp (results[5], "POINT(3482470.825574 4495691.054818)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -29;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase2 >= 20;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -30;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -31;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -32;
      }
    if (strcmp (results[3], "orde lees") != 0)
      {
	  fprintf (stderr, "Unexpected error: orde lees3 bad result: %s.\n",
		   results[3]);
	  return -33;
      }
    if (strcmp (results[4], "20") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer5() bad result: %s.\n",
		   results[4]);
	  return -33;
      }
    if (strcmp (results[5], "POINT(3482470.825574 4495691.054818)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -34;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 < \"p\";");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -35;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -36;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -37;
      }
    if (strcmp (results[3], "orde lees") != 0)
      {
	  fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n",
		   results[3]);
	  return -38;
      }
    if (strcmp (results[4], "20") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -39;
      }
    if (strcmp (results[5], "POINT(3482470.825574 4495691.054818)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -40;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 <= \"p\";");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -41;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -42;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -43;
      }
    if (strcmp (results[3], "orde lees") != 0)
      {
	  fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n",
		   results[3]);
	  return -44;
      }
    if (strcmp (results[4], "20") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -45;
      }
    if (strcmp (results[5], "POINT(3482470.825574 4495691.054818)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -46;
      }
    sqlite3_free_table (results);


    ret = sqlite3_exec (db_handle, "BEGIN;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "BEGIN error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -47;
      }

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 > \"p\";");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -48;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -49;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -50;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -51;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -52;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -53;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_exec (db_handle, "DELETE FROM shapetest WHERE testcase2 = 2;",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_READONLY)
      {
	  fprintf (stderr, "UPDATE error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -54;
      }
    sqlite3_free (err_msg);

    ret = sqlite3_exec (db_handle, "ROLLBACK;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "ROLLBACK error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -55;
      }

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 >= \"p\";");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -56;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -57;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -58;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -59;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -60;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -61;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 = \"windward\";");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -62;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -63;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -64;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -65;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -66;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -67;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID = 1;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -68;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -69;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -70;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -71;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -72;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -73;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID < 2;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -74;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -75;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -76;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -77;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -78;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -79;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID <= 1;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -80;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -81;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -82;
      }
    if (strcmp (results[3], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[3]);
	  return -83;
      }
    if (strcmp (results[4], "2") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -84;
      }
    if (strcmp (results[5], "POINT(3480766.311245 4495355.740524)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -85;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID > 1;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -86;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -87;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -88;
      }
    if (strcmp (results[3], "orde lees") != 0)
      {
	  fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n",
		   results[3]);
	  return -89;
      }
    if (strcmp (results[4], "20") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -90;
      }
    if (strcmp (results[5], "POINT(3482470.825574 4495691.054818)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -91;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select testcase1, testcase2, AsText(Geometry) from shapetest where PKUID >= 2;");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -92;
      }
    if ((rows != 1) || (columns != 3))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -93;
      }
    if (strcmp (results[0], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -94;
      }
    if (strcmp (results[3], "orde lees") != 0)
      {
	  fprintf (stderr, "Unexpected error: orde lees bad result: %s.\n",
		   results[3]);
	  return -95;
      }
    if (strcmp (results[4], "20") != 0)
      {
	  fprintf (stderr, "Unexpected error: integer() bad result: %s.\n",
		   results[4]);
	  return -96;
      }
    if (strcmp (results[5], "POINT(3482470.825574 4495691.054818)") != 0)
      {
	  fprintf (stderr, "Unexpected error: geometry() bad result: %s.\n",
		   results[5]);
	  return -97;
      }
    sqlite3_free_table (results);

    asprintf (&sql_statement,
	      "select PKUID, testcase1, testcase2, AsText(Geometry) from shapetest where testcase1 LIKE \"wind%%\";");
    ret =
	sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns,
			   &err_msg);
    free (sql_statement);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -98;
      }
    if ((rows != 1) || (columns != 4))
      {
	  fprintf (stderr,
		   "Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -99;
      }
    if (strcmp (results[0], "PKUID") != 0)
      {
	  fprintf (stderr, "Unexpected error: header uid bad result: %s.\n",
		   results[0]);
	  return -100;
      }
    if (strcmp (results[1], "testcase1") != 0)
      {
	  fprintf (stderr, "Unexpected error: header bad result: %s.\n",
		   results[1]);
	  return -101;
      }
    if (strcmp (results[4], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward PK bad result: %s.\n",
		   results[4]);
	  return -102;
      }
    if (strcmp (results[5], "windward") != 0)
      {
	  fprintf (stderr, "Unexpected error: windward bad result: %s.\n",
		   results[5]);
	  return -103;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_get_table (db_handle, "SELECT DropVirtualGeometry('shapetest')",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "DropVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -104;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -105;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -106;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_exec (db_handle,
		      "create VIRTUAL TABLE shapetest2 USING VirtualShape(\"shp/merano-3d/roads\", CP1252, 25832);",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -107;
      }
    ret =
	sqlite3_get_table (db_handle,
			   "SELECT RegisterVirtualGeometry('shapetest2')",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "RegisterVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -108;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -109;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -110;
      }
    sqlite3_free_table (results);
    ret =
	sqlite3_get_table (db_handle,
			   "SELECT UpdateLayerStatistics('shapetest2')",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "UpdateLayerStatistics error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -111;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "UpdateLayerStatistics Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -112;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "UpdateLayerStatistics Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -113;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_get_table (db_handle,
			   "SELECT DropVirtualGeometry('shapetest2')", &results,
			   &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "DropVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -114;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -115;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -116;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_exec (db_handle,
		      "create VIRTUAL TABLE shapetest3 USING VirtualShape(\"shp/merano-3d/points\", CP1252, 25832);",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -117;
      }
    ret =
	sqlite3_get_table (db_handle,
			   "SELECT RegisterVirtualGeometry('shapetest3')",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "RegisterVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -118;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -119;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -120;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_get_table (db_handle,
			   "SELECT DropVirtualGeometry('shapetest3')", &results,
			   &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "DropVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -121;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -122;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -123;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_exec (db_handle,
		      "create VIRTUAL TABLE shapetest4 USING VirtualShape(\"shp/merano-3d/polygons\", CP1252, 25832);",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -124;
      }
    ret =
	sqlite3_get_table (db_handle,
			   "SELECT RegisterVirtualGeometry('shapetest4')",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "RegisterVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -125;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -126;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "RegisterVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -127;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_get_table (db_handle,
			   "SELECT DropVirtualGeometry('shapetest4')", &results,
			   &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "DropVirtualGeometry error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -128;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: select columns bad result: %i/%i.\n",
		   rows, columns);
	  return -129;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr,
		   "DropVirtualGeometry Unexpected error: header() bad result: %s.\n",
		   results[0]);
	  return -130;
      }
    sqlite3_free_table (results);

/* final DB cleanup */
    ret =
	sqlite3_exec (db_handle,
		      "DELETE FROM spatialite_history WHERE geometry_column IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "DELETE FROM spatialite_history error: %s\n",
		   err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (db_handle);
	  return -131;
      }
    ret = sqlite3_exec (db_handle, "VACUUM", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "VACUUM error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (db_handle);
	  return -132;
      }
#endif /* end ICONV conditional */

    return 0;
}
Пример #6
0
int
_ds_getall_spamrecords (DSPAM_CTX * CTX, ds_diction_t diction)
{
  struct _sqlite_drv_storage *s = (struct _sqlite_drv_storage *) CTX->storage;
  buffer *query;
  ds_term_t ds_term;
  ds_cursor_t ds_c;
  char scratch[1024];
  struct _ds_spam_stat stat;
  unsigned long long token = 0;
  char *err=NULL, **row;
  int nrow, ncolumn, get_one = 0, i;

  if (s->dbh == NULL)
  {
    LOGDEBUG ("_ds_getall_spamrecords: invalid database handle (NULL)");
    return EINVAL;
  }

  stat.spam_hits = 0;
  stat.innocent_hits = 0;

  query = buffer_create (NULL);
  if (query == NULL)
  {
    LOG (LOG_CRIT, ERR_MEM_ALLOC);
    return EUNKNOWN;
  }

  snprintf (scratch, sizeof (scratch),
            "select token, spam_hits, innocent_hits "
            "from dspam_token_data where token in(");

  buffer_cat (query, scratch);
  ds_c = ds_diction_cursor(diction);
  ds_term = ds_diction_next(ds_c);
  while (ds_term)
  {
    snprintf (scratch, sizeof (scratch), "'%" LLU_FMT_SPEC "'", ds_term->key);
    buffer_cat (query, scratch);
    ds_term->s.innocent_hits = 0;
    ds_term->s.spam_hits = 0;
    ds_term->s.probability = 0;
    ds_term->s.status &= ~TST_DISK;
    ds_term = ds_diction_next(ds_c);
    if (ds_term)
      buffer_cat (query, ",");
    get_one = 1;
  }
  ds_diction_close(ds_c);
  buffer_cat (query, ")");

#ifdef VERBOSE
  LOGDEBUG ("sqlite query length: %ld\n", query->used);
  _sqlite_drv_query_error (strdup("VERBOSE DEBUG (INFO ONLY - NOT AN ERROR)"), query->data);
#endif

  if (!get_one) 
    return 0;

  if ((sqlite3_get_table(s->dbh, query->data, &row, &nrow, &ncolumn, &err))
      !=SQLITE_OK)
  {
    _sqlite_drv_query_error (err, query->data);
    buffer_destroy(query);
    return EFAILURE;
  }

  if (nrow < 1) {
    sqlite3_free_table(row);
    buffer_destroy(query);
    return 0;
  }

  if (row == NULL)
    return 0;

  stat.probability = 0;
  stat.status |= TST_DISK;
  for(i=1;i<=nrow;i++) {
    token = strtoull (row[(i*ncolumn)], NULL, 0);
    stat.spam_hits = strtol (row[1+(i*ncolumn)], NULL, 0);
    stat.innocent_hits = strtol (row[2+(i*ncolumn)], NULL, 0);

    if (stat.spam_hits < 0)
      stat.spam_hits = 0;
    if (stat.innocent_hits < 0)
      stat.innocent_hits = 0;

    ds_diction_addstat(diction, token, &stat);
  }

  sqlite3_free_table(row);

  ds_c = ds_diction_cursor(diction);
  ds_term = ds_diction_next(ds_c);
  while(ds_term && !s->control_token) {
    if (ds_term->s.spam_hits && ds_term->s.innocent_hits) {
      s->control_token = ds_term->key;
      s->control_sh = ds_term->s.spam_hits;
      s->control_ih = ds_term->s.innocent_hits;
    }
    ds_term = ds_diction_next(ds_c);
  }
  ds_diction_close(ds_c);

  if (!s->control_token)
  {
    ds_c = ds_diction_cursor(diction);
    ds_term = ds_diction_next(ds_c);
    s->control_token = ds_term->key;
    s->control_sh = ds_term->s.spam_hits;
    s->control_ih = ds_term->s.innocent_hits;
    ds_diction_close(ds_c);
  }

  buffer_destroy (query);
  return 0;
}
int main (int argc, char *argv[])
{
    sqlite3 *db_handle = NULL;
    char *sql_statement;
    int ret;
    char *err_msg = NULL;
    int i;
    char **results;
    int rows;
    int columns;
    const char *sql;

    spatialite_init (0);

    ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
	sqlite3_close (db_handle);
	db_handle = NULL;
	return -1;
    }
    
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE shapetest USING VirtualShape('shp/merano-3d/roads', CP1252, 25832);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -2;
    }

    ret = sqlite3_exec (db_handle, "DROP TABLE shapetest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -3;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE shapetest USING VirtualShape(\"shp/merano-3d/roads\", 'CP1252', -1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -4;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE shapetest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -5;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE unquoted USING VirtualShape(shapetest1, UTF8, -1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -6;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE unquoted;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -7;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE shapetest USING VirtualShape('shp/merano-3d/roads', \"CP1252\", 25832);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -8;
    }

    for (i = 0; steps[i].sql; ++i) {
	ret = sqlite3_get_table (db_handle, steps[i].sql, &results, &rows, &columns, &err_msg);
	if (ret != SQLITE_OK) {
	    fprintf (stderr, "Error: %s\n", err_msg);
	    sqlite3_free (err_msg);
	    return -9;
	}
	if (rows != steps[i].num_rows) {
	    fprintf (stderr, "Unexpected num of rows for test %i: %i.\n", i, rows);
	    return  -10;
	}
	sqlite3_free_table (results);
    }

    ret = sqlite3_exec (db_handle, "DROP TABLE shapetest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -11;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nosuchfile USING VirtualShape(nosuchfile, UTF8, -1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualShape error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -12;
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE nosuchfile;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -13;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE toofewargs USING VirtualShape(\"shapetest1\", UTF8);", NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR) {
	fprintf (stderr, "VirtualShape unexpected result: %i\n", ret);
	return -14;
    }
    sqlite3_free (err_msg);

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE toomanyargs USING VirtualShape(\"shapetest1\", UTF8, 4386, 1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR) {
	fprintf (stderr, "VirtualShape unexpected result: %i\n", ret);
	return -15;
    }
    sqlite3_free (err_msg);
    
    sqlite3_close (db_handle);
    spatialite_cleanup();
    sqlite3_reset_auto_extension();
    
    return 0;
}
Пример #8
0
void init_sql(void)
{
	sqlite3 *db = NULL;
	char *errmsg = 0;
	int rc;
	if((rc = sqlite3_open("foodmenu.db",&db))){
		perror("sqlite3_open");
		sqlite3_close(db);
		exit(1);
	}
	else printf("hello,you are succeed!\n");
	char *sql = "create table fdmenu(id1 integer primary key,varities text,foodname text,prices int);";
	rc = sqlite3_exec(db,sql,0,0,&errmsg);
	if(rc != SQLITE_OK) {
		printf("errmsg %s\n",errmsg);
	}
	sql = "INSERT INTO fdmenu VALUES(1,'��Ʒ��','����',50);";
	rc = sqlite3_exec(db,sql,0,0,&errmsg);
	if(rc != SQLITE_OK) {
		printf("errmsg %s\n",errmsg);
	}
	sql = "INSERT INTO fdmenu VALUES(2,'��Ʒ��','�Ϲϱ�',60);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql	= "INSERT INTO fdmenu VALUES(3,'��Ʒ��','Ŵ������',40);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(4,'�����','����',88);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(5,'�����','����',99);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(6,'�����','ţ��',98);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(7,'������','��������˿',30);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(8,'������','�ཷ����',40);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(9,'������','С�ײ�',25);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(10,'������','����ƹ�',20);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(11,'������','����',22);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(12,'������','��Ƥ',25);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(13,'����','�ϲ˵���',26);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(14,'����','�Ź���',50);";
	sqlite3_exec(db,sql,0,0,&errmsg);
	sql = "INSERT INTO fdmenu VALUES(15,'����','������',33);";
	sqlite3_exec(db,sql,0,0,&errmsg);

	int nrow = 0;
	int ncolumn = 0;
	char **result;

	//sql = "select * from fdmenu where id2=2";
	int re;	
	re=sqlite3_get_table(db,"select * from fdmenu where varities like '����' ",&result,&nrow,&ncolumn,&errmsg);
	if(re != SQLITE_OK){
	 perror("sqlite3_get_table");
	 sqlite3_close(db);
	 exit(1);
	}
	int i =0;
	printf("row:%d\tcolumn:%d\n",nrow,ncolumn);
	printf("\nThe result of querying is :\n");
#if 1
	for(i=0;i<(nrow+1)*ncolumn;i++){
	if((i+1)%5 != 0){
			printf("%s\t",result[i]);
	}
	else
		printf("%s\n",result[i]);}
#endif
	sqlite3_free_table(result);

#ifndef _DEBUG_
	printf("errmsg=%s\n",errmsg);
#endif
	sqlite3_close(db);

}
int main (int argc, char *argv[])
{
#ifndef OMIT_FREEXL		/* only if FreeXL is supported */
    sqlite3 *db_handle = NULL;
    char *sql_statement;
    int ret;
    char *err_msg = NULL;
    int i;
    char **results;
    int rows;
    int columns;

    spatialite_init (0);

    ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
	sqlite3_close (db_handle);
	db_handle = NULL;
	return -1;
    }
    
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE xltest USING VirtualXL(\"testcase1.xls\");", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -2;
    }

    asprintf(&sql_statement, "select col_2, col_4, col_5, col_7, rowid from xltest WHERE col_2 = \"Canal Creek\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -10;
    }
    if ((rows != 2) || (columns != 5)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -11;
    }
    if (strcmp(results[0], "col_2") != 0) {
	fprintf (stderr, "Unexpected error: header() bad result: %s.\n", results[0]);
	return  -12;
    }
    if (strcmp(results[5], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: name5() bad result: %s.\n", results[5]);
	return  -13;
    }
    if (strncmp(results[6], "-27.86667", 9) != 0) {
	fprintf (stderr, "Unexpected error: lat1() bad result: %s.\n", results[6]);
	return  -14;
    }
    if (strncmp(results[7], "151.51667", 9) != 0) {
	fprintf (stderr, "Unexpected error: lon2() bad result: %s.\n", results[7]);
	return  -15;
    }
    if (strcmp(results[10], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: name10() bad result: %s.\n", results[10]);
	return  -16;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (db_handle, "BEGIN;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "BEGIN error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -21;
    }

    ret = sqlite3_exec (db_handle, "DELETE FROM xltest WHERE col_14 > 100000;", NULL, NULL, &err_msg);
    if (ret != SQLITE_READONLY) {
	fprintf (stderr, "UPDATE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -21;
    }
    sqlite3_free (err_msg);

    ret = sqlite3_exec (db_handle, "ROLLBACK;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "ROLLBACK error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -22;
    }
 
    for (i = 0; i < NUMSTEPS; ++i) {
	ret = sqlite3_get_table (db_handle, steps[i].sql, &results, &rows, &columns, &err_msg);
	if (ret != SQLITE_OK) {
	    fprintf (stderr, "Error: %s\n", err_msg);
	    sqlite3_free (err_msg);
	    return -23;
	}
	if (rows != steps[i].num_rows) {
	    fprintf (stderr, "Unexpected num of rows for test %i: %i.\n", i, rows);
	    return  -24;
	}
	sqlite3_free_table (results);
    }
    
    ret = sqlite3_exec (db_handle, "DROP TABLE xltest;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -25;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nosuchworksheet USING VirtualXL(\"testcase1.xls\", 3);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -26;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE nosuchworksheet;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -27;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nosuchfile USING VirtualXL(\"not_a_file.xls\", 3);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -28;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE nosuchfile;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -29;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE sheet2 USING VirtualXL(\"testcase1.xls\", 1, 1);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -30;
    }
    asprintf(&sql_statement, "select row_no, place, lat, lon, rowid from sheet2 WHERE place = \"Canal Creek\";");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -31;
    }
    if ((rows != 4) || (columns != 5)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -32;
    }
    if (strcmp(results[0], "row_no") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result: %s.\n", results[0]);
	return  -33;
    }
    if (strcmp(results[6], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result place: %s.\n", results[6]);
	return  -34;
    }
    if (strncmp(results[7], "-27.86667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lat: %s.\n", results[7]);
	return  -35;
    }
    if (strncmp(results[8], "151.51667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lon: %s.\n", results[8]);
	return  -36;
    }
    if (strcmp(results[11], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result place2: %s.\n", results[11]);
	return  -37;
    }
    sqlite3_free_table (results);

    asprintf(&sql_statement, "select row_no, place, lat, lon, rowid from sheet2 WHERE row_no = 16");
    ret = sqlite3_get_table (db_handle, sql_statement, &results, &rows, &columns, &err_msg);
    free(sql_statement);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "Error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -38;
    }
    if ((rows != 1) || (columns != 5)) {
	fprintf (stderr, "Unexpected error: select columns bad result: %i/%i.\n", rows, columns);
	return  -39;
    }
    if (strcmp(results[0], "row_no") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result: %s.\n", results[0]);
	return  -40;
    }
    if (strcmp(results[6], "Canal Creek") != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result place: %s.\n", results[6]);
	return  -41;
    }
    if (strncmp(results[7], "-27.86667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lat: %s.\n", results[7]);
	return  -42;
    }
    if (strncmp(results[8], "151.51667", 9) != 0) {
	fprintf (stderr, "Unexpected error: sheet2() bad result lon: %s.\n", results[8]);
	return  -43;
    }
    sqlite3_free_table (results);

    ret = sqlite3_exec (db_handle, "DROP TABLE sheet2;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -44;
    }

    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE noquote USING VirtualXL(testcase1.xls);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -45;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE noquote;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -46;
    }
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE noheader USING VirtualXL(\"testcase1.xls\", 0, 0);", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "VirtualXL error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -47;
    }
    ret = sqlite3_exec (db_handle, "DROP TABLE noheader;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK) {
	fprintf (stderr, "DROP TABLE error: %s\n", err_msg);
	sqlite3_free (err_msg);
	return -48;
    }
    ret = sqlite3_exec (db_handle, "create VIRTUAL TABLE nofile USING VirtualXL();", NULL, NULL, &err_msg);
    if (ret != SQLITE_ERROR) {
	fprintf (stderr, "VirtualXL unexpected result: %i\n", ret);
	return -49;
    }
    sqlite3_free (err_msg);

    sqlite3_close (db_handle);
    spatialite_cleanup();
#endif	/* end FreeXL conditional */
    
    return 0;
}
Пример #10
0
/******************************************************************************
 *                                                                            *
 * Function: zbx_db_vselect                                                   *
 *                                                                            *
 * Purpose: execute a select statement                                        *
 *                                                                            *
 * Return value: data, NULL (on error) or (DB_RESULT)ZBX_DB_DOWN              *
 *                                                                            *
 ******************************************************************************/
DB_RESULT	zbx_db_vselect(const char *fmt, va_list args)
{
	char		*sql = NULL;
	DB_RESULT	result = NULL;
	double		sec = 0;

#if defined(HAVE_IBM_DB2)
	int		i;
	SQLRETURN	ret = SQL_SUCCESS;
#elif defined(HAVE_ORACLE)
	sword		err = OCI_SUCCESS;
	ub4		counter;
#elif defined(HAVE_POSTGRESQL)
	char		*error = NULL;
#elif defined(HAVE_SQLITE3)
	int		ret = FAIL;
	char		*error = NULL;
#endif

	if (CONFIG_LOG_SLOW_QUERIES)
		sec = zbx_time();

	sql = zbx_dvsprintf(sql, fmt, args);

	zabbix_log(LOG_LEVEL_DEBUG, "query [txnlev:%d] [%s]", txn_level, sql);

#if defined(HAVE_IBM_DB2)
	result = zbx_malloc(result, sizeof(ZBX_IBM_DB2_RESULT));
	memset(result, 0, sizeof(ZBX_IBM_DB2_RESULT));

	/* allocate a statement handle */
	if (SUCCEED != zbx_ibm_db2_success(ret = SQLAllocHandle(SQL_HANDLE_STMT, ibm_db2.hdbc, &result->hstmt)))
		goto error;

	/* directly execute the statement */
	if (SUCCEED != zbx_ibm_db2_success(ret = SQLExecDirect(result->hstmt, (SQLCHAR *)sql, SQL_NTS)))
		goto error;

	/* identify the number of output columns */
	if (SUCCEED != zbx_ibm_db2_success(ret = SQLNumResultCols(result->hstmt, &result->ncolumn)))
		goto error;

	if (0 == result->ncolumn)
		goto error;

	result->nalloc = 0;
	result->values = zbx_malloc(result->values, sizeof(char *) * result->ncolumn);
	result->values_cli = zbx_malloc(result->values_cli, sizeof(char *) * result->ncolumn);
	result->values_len = zbx_malloc(result->values_len, sizeof(SQLINTEGER) * result->ncolumn);

	for (i = 0; i < result->ncolumn; i++)
	{
		/* get the display size for a column */
		if (SUCCEED != zbx_ibm_db2_success(ret = SQLColAttribute(result->hstmt, (SQLSMALLINT)(i + 1),
				SQL_DESC_DISPLAY_SIZE, NULL, 0, NULL, &result->values_len[i])))
		{
			goto error;
		}

		result->values_len[i] += 1; /* '\0'; */

		/* allocate memory to bind a column */
		result->values_cli[i] = zbx_malloc(NULL, result->values_len[i]);
		result->nalloc++;

		/* bind columns to program variables, converting all types to CHAR */
		if (SUCCEED != zbx_ibm_db2_success(ret = SQLBindCol(result->hstmt, (SQLSMALLINT)(i + 1),
				SQL_C_CHAR, result->values_cli[i], result->values_len[i], &result->values_len[i])))
		{
			goto error;
		}
	}
error:
	if (SUCCEED != zbx_ibm_db2_success(ret) || 0 == result->ncolumn)
	{
		zbx_ibm_db2_log_errors(SQL_HANDLE_DBC, ibm_db2.hdbc);
		zbx_ibm_db2_log_errors(SQL_HANDLE_STMT, result->hstmt);

		IBM_DB2free_result(result);

		result = (SQL_CD_TRUE == IBM_DB2server_status() ? NULL : (DB_RESULT)ZBX_DB_DOWN);
	}
#elif defined(HAVE_MYSQL)
	if (NULL == conn)
	{
		zabbix_errlog(ERR_Z3003);
		result = NULL;
	}
	else
	{
		if (0 != mysql_query(conn, sql))
		{
			zabbix_errlog(ERR_Z3005, mysql_errno(conn), mysql_error(conn), sql);
			switch (mysql_errno(conn))
			{
				case CR_CONN_HOST_ERROR:
				case CR_SERVER_GONE_ERROR:
				case CR_CONNECTION_ERROR:
				case CR_SERVER_LOST:
				case ER_SERVER_SHUTDOWN:
				case ER_ACCESS_DENIED_ERROR: /* wrong user or password */
				case ER_ILLEGAL_GRANT_FOR_TABLE: /* user without any privileges */
				case ER_TABLEACCESS_DENIED_ERROR:/* user without some privilege */
				case ER_UNKNOWN_ERROR:
					result = (DB_RESULT)ZBX_DB_DOWN;
					break;
				default:
					result = NULL;
					break;
			}
		}
		else
			result = mysql_store_result(conn);
	}
#elif defined(HAVE_ORACLE)
	result = zbx_malloc(NULL, sizeof(ZBX_OCI_DB_RESULT));
	memset(result, 0, sizeof(ZBX_OCI_DB_RESULT));

	err = OCIHandleAlloc((dvoid *)oracle.envhp, (dvoid **)&result->stmthp, OCI_HTYPE_STMT, (size_t)0, (dvoid **)0);

	if (OCI_SUCCESS == err)
	{
		err = OCIStmtPrepare(result->stmthp, oracle.errhp, (text *)sql, (ub4)strlen((char *)sql),
				(ub4)OCI_NTV_SYNTAX, (ub4)OCI_DEFAULT);
	}

	if (OCI_SUCCESS == err)
	{
		err = OCIStmtExecute(oracle.svchp, result->stmthp, oracle.errhp, (ub4)0, (ub4)0,
				(CONST OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_COMMIT_ON_SUCCESS);
	}

	if (OCI_SUCCESS == err)
	{
		/* get the number of columns in the query */
		err = OCIAttrGet((void *)result->stmthp, OCI_HTYPE_STMT, (void *)&result->ncolumn,
				  (ub4 *)0, OCI_ATTR_PARAM_COUNT, oracle.errhp);
	}

	if (OCI_SUCCESS != err)
		goto error;

	assert(0 < result->ncolumn);

	result->values = zbx_malloc(NULL, result->ncolumn * sizeof(char *));
	memset(result->values, 0, result->ncolumn * sizeof(char *));

	for (counter = 1; OCI_SUCCESS == err && counter <= result->ncolumn; counter++)
	{
		OCIParam	*parmdp = NULL;
		OCIDefine	*defnp = NULL;
		ub4		char_semantics;
		ub2		col_width;

		/* request a parameter descriptor in the select-list */
		err = OCIParamGet((void *)result->stmthp, OCI_HTYPE_STMT, oracle.errhp, (void **)&parmdp, (ub4)counter);

		if (OCI_SUCCESS == err)
		{
			/* retrieve the length semantics for the column */
			char_semantics = 0;
			err = OCIAttrGet((void *)parmdp, (ub4)OCI_DTYPE_PARAM, (void *)&char_semantics, (ub4 *)0,
					(ub4)OCI_ATTR_CHAR_USED, (OCIError *)oracle.errhp);
		}

		if (OCI_SUCCESS == err)
		{
			col_width = 0;
			if (char_semantics)
			{
				/* retrieve the column width in characters */
				err = OCIAttrGet((void *)parmdp, (ub4)OCI_DTYPE_PARAM, (void *)&col_width, (ub4 *)0,
						(ub4)OCI_ATTR_CHAR_SIZE, (OCIError *)oracle.errhp);
			}
			else
			{
				/* retrieve the column width in bytes */
				err = OCIAttrGet((void *)parmdp, (ub4)OCI_DTYPE_PARAM, (void *)&col_width, (ub4 *)0,
						(ub4)OCI_ATTR_DATA_SIZE, (OCIError *)oracle.errhp);
			}
		}
		col_width++;

		result->values[counter - 1] = zbx_malloc(NULL, col_width);
		memset(result->values[counter - 1], 0, col_width);

		if (OCI_SUCCESS == err)
		{
			/* represent any data as characters */
			err = OCIDefineByPos(result->stmthp, &defnp, oracle.errhp, counter,
					(dvoid *)result->values[counter - 1], col_width, SQLT_STR,
					(dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT);
		}

		/* free cell descriptor */
		OCIDescriptorFree(parmdp, OCI_DTYPE_PARAM);
		parmdp = NULL;
	}

error:
	if (OCI_SUCCESS != err)
	{
		zabbix_errlog(ERR_Z3005, err, zbx_oci_error(err), sql);

		OCI_DBfree_result(result);

		result = (OCI_SERVER_NORMAL == OCI_DBserver_status() ? NULL : (DB_RESULT)ZBX_DB_DOWN);
	}
#elif defined(HAVE_POSTGRESQL)
	result = zbx_malloc(NULL, sizeof(ZBX_PG_DB_RESULT));
	result->pg_result = PQexec(conn, sql);
	result->values = NULL;
	result->cursor = 0;
	result->row_num = 0;

	if (NULL == result->pg_result)
		zabbix_errlog(ERR_Z3005, 0, "result is NULL", sql);

	if (PGRES_TUPLES_OK != PQresultStatus(result->pg_result))
	{
		error = zbx_dsprintf(error, "%s:%s",
				PQresStatus(PQresultStatus(result->pg_result)),
				PQresultErrorMessage(result->pg_result));
		zabbix_errlog(ERR_Z3005, 0, error, sql);
		zbx_free(error);

		PG_DBfree_result(result);
		result = (CONNECTION_OK == PQstatus(conn) ? NULL : (DB_RESULT)ZBX_DB_DOWN);
	}
	else	/* init rownum */
		result->row_num = PQntuples(result->pg_result);
#elif defined(HAVE_SQLITE3)
	if (0 == txn_level && PHP_MUTEX_OK != php_sem_acquire(&sqlite_access))
	{
		zabbix_log(LOG_LEVEL_CRIT, "ERROR: unable to create lock on SQLite database");
		exit(FAIL);
	}

	result = zbx_malloc(NULL, sizeof(ZBX_SQ_DB_RESULT));
	result->curow = 0;

lbl_get_table:
	if (SQLITE_OK != (ret = sqlite3_get_table(conn,sql, &result->data, &result->nrow, &result->ncolumn, &error)))
	{
		if (SQLITE_BUSY == ret)
			goto lbl_get_table;

		zabbix_errlog(ERR_Z3005, 0, error, sql);
		sqlite3_free(error);

		SQ_DBfree_result(result);

		switch (ret)
		{
			case SQLITE_ERROR:	/* SQL error or missing database; assuming SQL error, because if we
						   are this far into execution, zbx_db_connect() was successful */
			case SQLITE_NOMEM:	/* a malloc() failed */
			case SQLITE_MISMATCH:	/* data type mismatch */
				result = NULL;
				break;
			default:
				result = (DB_RESULT)ZBX_DB_DOWN;
				break;
		}
	}

	if (0 == txn_level)
		php_sem_release(&sqlite_access);
#endif	/* HAVE_SQLITE3 */

	if (CONFIG_LOG_SLOW_QUERIES)
	{
		sec = zbx_time() - sec;
		if (sec > (double)CONFIG_LOG_SLOW_QUERIES / 1000.0)
			zabbix_log(LOG_LEVEL_WARNING, "slow query: " ZBX_FS_DBL " sec, \"%s\"", sec, sql);
	}

	zbx_free(sql);
	return result;
}
Пример #11
0
int main(int argc, const char *argv[])
{
	sqlite3 *dbp;
	char *errmsg;
	int flag = 0;

	char **pazResult;
	int row;
	int column;

	int i,j;
	char buf[128];
	int id = 10005;
	char name[64] = "xiaoming";
	float english = 99;

	if(SQLITE_OK != sqlite3_open("my.db",&dbp) ){
		puts(sqlite3_errmsg(dbp));
		return -1;
	}

	if(SQLITE_OK != sqlite3_exec(dbp,
				"create table stu(id integer primary key,name text,english real NULL);",NULL,NULL,&errmsg) ){
		puts(errmsg);
	}
	if(SQLITE_OK != sqlite3_exec(dbp,
				"insert into stu (id,name,english) values(10000,'li',59);",NULL,NULL,&errmsg) ){
		puts(errmsg);
	}
	if(SQLITE_OK != sqlite3_exec(dbp,
				"insert into stu (id,name,english) values(10001,'xiaoming',100);",NULL,NULL,&errmsg) ){
		puts(errmsg);
	}

	if(SQLITE_OK != sqlite3_exec(dbp,
				"insert into stu (id,name,english) values(10002,'xiaoli',70);",NULL,NULL,&errmsg) ){
		puts(errmsg);
	}

	bzero(buf,sizeof(buf));
	sprintf(buf,"insert into stu (id,name,english) values(%d,'%s',%.2f);",id,name,english);
	if(SQLITE_OK != sqlite3_exec(dbp,
				buf,NULL,NULL,&errmsg) ){
		puts(errmsg);
	}

	insert_sql(dbp,10007,"zhangsan",57);

	flag = 0;
	if(SQLITE_OK != sqlite3_exec(dbp,
				"select * from stu;",callback,(void *)&flag,&errmsg) ){
		puts(errmsg);
	}
	flag = 0;
	if(SQLITE_OK != sqlite3_exec(dbp,
				"select * from stu;",callback,(void *)&flag,&errmsg) ){
		puts(errmsg);
	}

	puts("/**************************/");


	if(SQLITE_OK != sqlite3_get_table(dbp,
				"select * from stu;",&pazResult,&row,&column,&errmsg)){
		puts(errmsg);
	}



	for(j = 0;j <= row;j ++){
		for(i = column * j;i < column *(j + 1);i ++ ){
			printf("%s\t",pazResult[i]);
		}
		printf("\n");
	}

	sqlite3_free_table(pazResult);



	if(SQLITE_OK != sqlite3_close(dbp) ){
		puts(sqlite3_errmsg(dbp));
		return -1;
	}

	return 0;
}
Пример #12
0
bool QgsOSMXmlImport::createDatabase()
{
  char **results;
  int rows, columns;
  if ( QgsSLConnect::sqlite3_open_v2( mDbFileName.toUtf8().data(), &mDatabase, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0 ) != SQLITE_OK )
    return false;

  bool above41 = false;
  int ret = sqlite3_get_table( mDatabase, "select spatialite_version()", &results, &rows, &columns, NULL );
  if ( ret == SQLITE_OK && rows == 1 && columns == 1 )
  {
    QString version = QString::fromUtf8( results[1] );
    QStringList parts = version.split( " ", QString::SkipEmptyParts );
    if ( parts.size() >= 1 )
    {
      QStringList verparts = parts[0].split( ".", QString::SkipEmptyParts );
      above41 = verparts.size() >= 2 && ( verparts[0].toInt() > 4 || ( verparts[0].toInt() == 4 && verparts[1].toInt() >= 1 ) );
    }
  }
  sqlite3_free_table( results );

  const char* sqlInitStatements[] =
  {
    "PRAGMA cache_size = 100000", // TODO!!!
    "PRAGMA synchronous = OFF", // TODO!!!
    above41 ? "SELECT InitSpatialMetadata(1)" : "SELECT InitSpatialMetadata()",
    "CREATE TABLE nodes ( id INTEGER PRIMARY KEY, lat REAL, lon REAL )",
    "CREATE TABLE nodes_tags ( id INTEGER, k TEXT, v TEXT )",
    "CREATE TABLE ways ( id INTEGER PRIMARY KEY )",
    "CREATE TABLE ways_nodes ( way_id INTEGER, node_id INTEGER, way_pos INTEGER )",
    "CREATE TABLE ways_tags ( id INTEGER, k TEXT, v TEXT )",
  };

  int initCount = sizeof( sqlInitStatements ) / sizeof( const char* );
  for ( int i = 0; i < initCount; ++i )
  {
    char* errMsg;
    if ( sqlite3_exec( mDatabase, sqlInitStatements[i], 0, 0, &errMsg ) != SQLITE_OK )
    {
      mError = QString( "Error executing SQL command:\n%1\nSQL:\n%2" )
               .arg( QString::fromUtf8( errMsg ) ).arg( QString::fromUtf8( sqlInitStatements[i] ) );
      sqlite3_free( errMsg );
      closeDatabase();
      return false;
    }
  }

  const char* sqlInsertStatements[] =
  {
    "INSERT INTO nodes ( id, lat, lon ) VALUES (?,?,?)",
    "INSERT INTO nodes_tags ( id, k, v ) VALUES (?,?,?)",
    "INSERT INTO ways ( id ) VALUES (?)",
    "INSERT INTO ways_nodes ( way_id, node_id, way_pos ) VALUES (?,?,?)",
    "INSERT INTO ways_tags ( id, k, v ) VALUES (?,?,?)"
  };
  sqlite3_stmt** sqliteInsertStatements[] =
  {
    &mStmtInsertNode,
    &mStmtInsertNodeTag,
    &mStmtInsertWay,
    &mStmtInsertWayNode,
    &mStmtInsertWayTag
  };
  Q_ASSERT( sizeof( sqlInsertStatements ) / sizeof( const char* ) == sizeof( sqliteInsertStatements ) / sizeof( sqlite3_stmt** ) );
  int insertCount = sizeof( sqlInsertStatements ) / sizeof( const char* );

  for ( int i = 0; i < insertCount; ++i )
  {
    if ( sqlite3_prepare_v2( mDatabase, sqlInsertStatements[i], -1, sqliteInsertStatements[i], 0 ) != SQLITE_OK )
    {
      const char* errMsg = sqlite3_errmsg( mDatabase ); // does not require free
      mError = QString( "Error preparing SQL command:\n%1\nSQL:\n%2" )
               .arg( QString::fromUtf8( errMsg ) ).arg( QString::fromUtf8( sqlInsertStatements[i] ) );
      closeDatabase();
      return false;
    }
  }

  return true;
}
Пример #13
0
static void *recover_entry(void *arg)
{
	d_task_t *d_task = (d_task_t *)arg;
	file_info_t file;

	char **results;
	int row,col;
	char *err_msg;
	char sql_buf[256];
	int ret, i, parts;
	int per_part_len, last_part_len;

	snprintf(sql_buf, sizeof(sql_buf), SQL_QUERY_TABLE, d_task->file_saved_path);
	ret = sqlite3_get_table(d_task->dm->db_key, sql_buf, &results, &row, &col, &err_msg);
	if (ret != SQLITE_OK)
	{
		fprintf(stderr, "SQL error: %s\n", err_msg);
		sqlite3_free(err_msg);
		return ERR_RET_VAL;
	}
	if (row != 1 || col != 8)
		return ERR_RET_VAL;
	i = col + 1;

	// file_url
	if (parse_url(results[i++], &file.d_url) < 0)
		return ERR_RET_VAL;
	switch(file.d_url.proto)
	{
		case HTTP:
			d_task->request_part_file = http_request_part_file;
			break;
		case FTP:
			d_task->request_part_file = ftp_request_part_file;
			break;
	}

	// file_saved_path
	strncpy(d_task->file_saved_path, results[i++], PATH_MAX);

	// file_length
	sscanf(results[i++], "%d", &file.length);

	// tmp_file_name_fmt
	strncpy(d_task->tmp_file_name_fmt, results[i++], PATH_MAX);

	// parts
	sscanf(results[i++], "%d", &parts);

	// average_len
	sscanf(results[i++], "%d", &per_part_len);

	// last_part_len
	sscanf(results[i++], "%d", &last_part_len);
	
	sqlite3_free_table(results);

	// create unique downloading file
	if (_create_unique_file(d_task, &file) != 0)
	{
		fprintf(stderr, "error when create download file\n");
		return ERR_RET_VAL;
	}
	dispatch_part_download(d_task, &file, per_part_len, last_part_len, parts);
}
Пример #14
0
int main( void )
{
    sqlite3 *db=NULL;
    char *zErrMsg = 0;

    int rc;

    rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
    if( rc )
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    }
    else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun !  ^-^ \n");


    //创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
    char *sql = "	CREATE TABLE SensorData(\
				 	ID INTEGER PRIMARY KEY,\
					SensorID INTEGER,\
				 	SiteNum INTEGER,\
				 	Time VARCHAR(12),\
				 	SensorParameter REAL\
				 	);" ;
    sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );

#ifdef _DEBUG_
    printf("zErrMsg = %s \n", zErrMsg);
#endif

    //插入数据
    sql = "INSERT INTO \"SensorData\" VALUES(NULL , 1 , 1 , '200605011206', 18.9 );" ;
    sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );

    sql = "INSERT INTO \"SensorData\" VALUES(NULL , 23 , 45 , '200605011306', 16.4 );" ;
    sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );

    sql = "INSERT INTO \"SensorData\" VALUES(NULL , 34 , 45 , '200605011306', 15.4 );" ;
    sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );


    int nrow = 0, ncolumn = 0;
    char **azResult; //二维数组存放结果

    //查询数据
    sql = "SELECT * FROM SensorData ";
    sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
    int i = 0 ;
    printf( "row:%d column=%d \n" , nrow , ncolumn );
    printf( "\nThe result of querying is : \n" );
    for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
        printf( "azResult[%d] = %s\n", i , azResult[i] );
    //释放掉	 azResult 的内存空间
    sqlite3_free_table( azResult );

    //删除数据
    sql = "DELETE FROM SensorData WHERE SensorID = 1 ;" ;
    sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
#ifdef _DEBUG_
    printf("zErrMsg = %s \n", zErrMsg);
#endif
    sql = "SELECT * FROM SensorData ";
    sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
    printf( "\n\n\n\nrow:%d column=%d " , nrow , ncolumn );
    printf( "\nAfter deleting , the result of querying is : \n" );
    for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
        printf( "azResult[%d] = %s\n", i , azResult[i] );
    //释放掉	 azResult 的内存空间
    sqlite3_free_table( azResult );


    printf( "\nSATRT UPDATE  \n" );
    //更新数据
    sql = "UPDATE SensorData SET  SensorParameter = 100.0,SiteNum = 90 WHERE SensorID = 23 ;" ;
    sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
#ifdef _DEBUG_
    printf("zErrMsg = %s \n", zErrMsg);
#endif
    sql = "SELECT * FROM SensorData ";
    sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
    printf( "\n\n\n\nrow:%d column=%d " , nrow , ncolumn );
    printf( "\nAfter UPDATE , the result of querying is : \n" );
    for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
        printf( "azResult[%d] = %s\n", i , azResult[i] );


    //释放掉	 azResult 的内存空间
    sqlite3_free_table( azResult );

#ifdef _DEBUG_
    printf("zErrMsg = %s \n", zErrMsg);
#endif

    sqlite3_close(db); //关闭数据库
    return 0;

}
Пример #15
0
__declspec(dllexport) int WINAPI sqlite3_get_table_interop(sqlite3 *db, const char *sql, char ***resultp, int *nrow, int *ncolumn, char **errmsg, int *plen)
{
  int n = sqlite3_get_table(db, sql, resultp, nrow, ncolumn, errmsg);
  *plen = (*errmsg != 0) ? strlen((char *)*errmsg) : 0;
  return n;
}
Пример #16
0
void lp_config_parse(LpConfig *lpconfig){
	int nrow = 0, ncolumn = 0;
	char **sectionResult; //二维数组存放结果
	int nIndex=0;
	LpSection *cur=NULL;

	char szSQL[] = "SELECT DISTINCT section FROM ua_config;";
	if(sqlite3_get_table(lpconfig->db, szSQL ,&sectionResult ,&nrow ,&ncolumn ,0) == SQLITE_OK)
	{
		int index = ncolumn;
		int i,j;
		for(i=0;i<nrow;i++){
			for(j=0;j<ncolumn;j++)
			{			  
				if ( strcmp(sectionResult[j],"section") == 0)
				{
					int itemnrow = 0, itemncolumn = 0;
					char **itemResult; //二维数组存放结果
					int itemIndex=0;
					char szSQLitem[128];

					cur = lp_section_new(sectionResult[index]);
					lp_config_add_section(lpconfig,cur);

					sprintf(szSQLitem,"SELECT var_name, var_value FROM ua_config WHERE section = '%s';",sectionResult[index]);

					if(sqlite3_get_table(lpconfig->db, szSQLitem ,&itemResult ,&itemnrow ,&itemncolumn ,0) == SQLITE_OK)
					{
						int idx = itemncolumn;
						int k,l;
						char *val_name;
						char *val_value;
						val_name = val_value = NULL;

						for(k=0;k<itemnrow;k++){
							for(l=0;l<itemncolumn;l++)
							{	
								if ( strcmp(itemResult[l],"var_name") == 0)
								{
									val_name = itemResult[idx];
								}

								if ( strcmp(itemResult[l],"var_value") == 0)
								{
									val_value = itemResult[idx];
								}

								++idx;
							}
							ms_message("lp_config_parse section: %s, var_name: %s, var_value: %s",cur->name,val_name,val_value);
							lp_section_add_item(cur,lp_item_new(val_name,val_value,FALSE));

						}

						sqlite3_free_table(itemResult);
					}

				}
				++index;
			}
		}

		sqlite3_free_table(sectionResult);
	}


}
Пример #17
0
int
main (int argc, char *argv[])
{
#ifndef OMIT_ICONV		/* only if ICONV is supported */
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    int row_count;
    char **results;
    int rows;
    int columns;
    int pt;
    void *cache = spatialite_alloc_connection ();

    if (argc > 1 || argv[0] == NULL)
	argc = 1;		/* silencing stupid compiler warnings */

    ret =
	sqlite3_open_v2 (":memory:", &handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open in-memory database: %s\n",
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return -1;
      }

    spatialite_init_ex (handle, cache, 0);

    ret =
	sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL,
		      &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -2;
      }

    ret = load_shapefile (handle, "shp/foggia/local_councils", "Councils",
			  "CP1252", 23032, "geom", 1, 0, 1, 0, &row_count,
			  err_msg);
    if (!ret)
      {
	  fprintf (stderr, "load_shapefile() error: %s\n", err_msg);
	  sqlite3_close (handle);
	  return -3;
      }
    if (row_count != 61)
      {
	  fprintf (stderr, "unexpected number of rows loaded: %i\n", row_count);
	  sqlite3_close (handle);
	  return -4;
      }

    ret =
	sqlite3_get_table (handle,
			   "SELECT lc_name FROM Councils WHERE MbrWithin(geom, BuildMbr(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -5;
      }
    if ((rows != 22) || (columns != 1))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -6;
      }
    if (strcmp (results[1], "Ascoli Satriano") != 0)
      {
	  fprintf (stderr, "unexpected result at 1: %s\n", results[1]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -7;
      }
    if (strcmp (results[22], "Zapponeta") != 0)
      {
	  fprintf (stderr, "unexpected result at 22: %s\n", results[22]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -8;
      }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "SELECT CreateMbrCache('Councils', 'geom');",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CreateMbrCache error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -9;
      }

    rows = 0;
    columns = 0;
    ret =
	sqlite3_get_table (handle,
			   "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -14;
      }
    if ((rows != 22) || (columns != 1))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad cache result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -15;
      }
    if (strcmp (results[1], "Ascoli Satriano") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -16;
      }
    if (strcmp (results[22], "Zapponeta") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 22: %s\n", results[22]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -17;
      }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret =
	sqlite3_get_table (handle,
			   "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrContains(997226.750031, 4627372.000018, 997226.750031, 4627372.000018)) ORDER BY lc_name;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error in Mbr SELECT Contains: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -18;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad cache result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -19;
      }
    if (strcmp (results[1], "Carlantino") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -20;
      }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret =
	sqlite3_get_table (handle,
			   "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrIntersects(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -21;
      }
    if ((rows != 35) || (columns != 1))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad cache result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -22;
      }
    if (strcmp (results[1], "Apricena") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -23;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_exec (handle,
		      "DELETE FROM Councils WHERE lc_name = 'Zapponeta';", NULL,
		      NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "DELETE error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -24;
      }

    rows = 0;
    columns = 0;
    ret =
	sqlite3_get_table (handle,
			   "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -25;
      }
    if ((rows != 21) || (columns != 1))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad cache result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -26;
      }
    if (strcmp (results[1], "Ascoli Satriano") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -27;
      }
    if (strcmp (results[21], "Vieste") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -28;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_exec (handle,
		      "INSERT INTO Councils (lc_name, geom) VALUES ('Quairading', GeomFromText('MULTIPOLYGON(((997226.750031 4627372.000018, 997301.750031 4627332.000018, 997402.500031 4627344.000018, 997541.500031 4627326.500018,997226.750031 4627372.000018)))', 23032));",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "INSERT error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -29;
      }

    rows = 0;
    columns = 0;
    ret =
	sqlite3_get_table (handle,
			   "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error in Mbr SELECT2: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -30;
      }
    if ((rows != 21) || (columns != 1))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad cache result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -31;
      }
    if (strcmp (results[1], "Ascoli Satriano") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -32;
      }
    if (strcmp (results[21], "Vieste") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -33;
      }
    sqlite3_free_table (results);

    ret =
	sqlite3_exec (handle,
		      "UPDATE Councils SET geom = GeomFromText('MULTIPOLYGON(((987226.750031 4627372.000018, 997301.750031 4627331.000018, 997402.500032 4627344.000018, 997541.500031 4627326.500018, 987226.750031 4627372.000018)))', 23032) WHERE lc_name = \"Quairading\";",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "UPDATE error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -34;
      }

    rows = 0;
    columns = 0;
    ret =
	sqlite3_get_table (handle,
			   "SELECT lc_name FROM Councils WHERE ROWID IN (SELECT rowid FROM cache_Councils_geom WHERE mbr = FilterMbrWithin(1040523, 4010000, 1140523, 4850000)) ORDER BY lc_name;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error in Mbr SELECT3: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -35;
      }
    if ((rows != 21) || (columns != 1))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad cache result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -36;
      }
    if (strcmp (results[1], "Ascoli Satriano") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 1: %s\n", results[1]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -37;
      }
    if (strcmp (results[21], "Vieste") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 21: %s\n", results[21]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -38;
      }
    sqlite3_free_table (results);

    rows = 0;
    columns = 0;
    ret =
	sqlite3_get_table (handle,
			   "SELECT rowid, mbr FROM cache_Councils_geom;",
			   &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error in Mbr SELECT: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -39;
      }
    if ((rows != 61) || (columns != 2))
      {
	  fprintf (stderr,
		   "Unexpected error: select lc_name bad cache2 result: %i/%i.\n",
		   rows, columns);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -40;
      }
    if (strcmp (results[2], "1") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 1: %s\n", results[2]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -41;
      }
    if (strcmp (results[12], "6") != 0)
      {
	  fprintf (stderr, "unexpected mbr result at 6: %s\n", results[12]);
	  sqlite3_free_table (results);
	  sqlite3_close (handle);
	  return -42;
      }
    sqlite3_free_table (results);

    ret = sqlite3_exec (handle, "DROP TABLE Councils;", NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "DROP TABLE Councils error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -44;
      }

/* creating and feeding a Point table */
    ret =
	sqlite3_exec (handle, "CREATE TABLE pt (id INTEGER);", NULL, NULL,
		      &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE TABLE pt error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -45;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('pt', 'g', 4326, 1, 'XY');",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -46;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 2.5);",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -47;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 'XY', 0.5);",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -48;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('pt', 'g', 4326, 'DUMMY', 'XY');",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -49;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 'DUMMY');",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -50;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('pt', 'g', 4326, 'POINT', 2);",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometryColumn pt error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -51;
      }
    ret =
	sqlite3_exec (handle, "SELECT CreateMbrCache('pt', 'g');", NULL, NULL,
		      &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CreateMbrCache pt.g error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -52;
      }
    for (pt = 0; pt < 10000; pt++)
      {
	  /* inserting Points */
	  char sql[1024];
	  sprintf (sql,
		   "INSERT INTO pt (id, g) VALUES (%d, GeomFromText('POINT(%1.2f %1.2f)', 4326));",
		   pt, 11.0 + (pt / 10000.0), 43.0 + (pt / 10000.0));
	  ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
	  if (ret != SQLITE_OK)
	    {
		fprintf (stderr, "INSERT INTO pt error: %s\n", err_msg);
		sqlite3_free (err_msg);
		return -53;
	    }
      }
    for (pt = 5000; pt < 6000; pt++)
      {
	  /* updating Points */
	  char sql[1024];
	  sprintf (sql,
		   "UPDATE pt SET g  = GeomFromText('POINT(%1.2f %1.2f)', 4326) WHERE id = %d;",
		   12.0 + (pt / 10000.0), 42.0 + (pt / 10000.0), pt);
	  ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
	  if (ret != SQLITE_OK)
	    {
		fprintf (stderr, "UPDATE pt error: %s\n", err_msg);
		sqlite3_free (err_msg);
		return -54;
	    }
      }
    for (pt = 7000; pt < 8000; pt++)
      {
	  /* deleting Points */
	  char sql[1024];
	  sprintf (sql, "DELETE FROM pt WHERE id = %d;", pt);
	  ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
	  if (ret != SQLITE_OK)
	    {
		fprintf (stderr, "UPDATE pt error: %s\n", err_msg);
		sqlite3_free (err_msg);
		return -55;
	    }
      }

    ret = sqlite3_exec (handle, "SELECT CreateMbrCache(1, 'geom');",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "invalid CreateMbrCache error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -56;
      }
    ret = sqlite3_exec (handle, "SELECT CreateMbrCache('Councils', 2);",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "invalid CreateMbrCache error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -57;
      }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin('a', 2, 3, 4);",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -58;
      }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin(1, 'a', 3, 4);",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -59;
      }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin(1, 2, 'a', 4);",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -60;
      }
    ret = sqlite3_exec (handle, "SELECT FilterMbrWithin(1, 2, 3, 'a');",
			NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "invalid FilterMbrWithin error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -61;
      }

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "sqlite3_close() error: %s\n",
		   sqlite3_errmsg (handle));
	  return -62;
      }

    spatialite_cleanup_ex (cache);
#endif /* end ICONV conditional */

    return 0;
}
Пример #18
0
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_TMFILEREADER, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TMFILEREADER));


	// hack location while bootstrapping items. just to call into the api
	// Make some sql lite calls.
	sqlite3 *pDb;

	char sqlLiteOutputFile[] = "testdb.db"; //  If f5 in Visual studio this will get created in the TMFileReader folder;
	remove(sqlLiteOutputFile);

	int result = sqlite3_open(
		sqlLiteOutputFile,   /* Database filename (UTF-8) */
		&pDb          /* OUT: SQLite db handle */
		);

	if (0 != result)
	{
		// todo: test file not found behavior and error message handling.
		// bad
	}


	// Troop master intersperse
	tmDb *ptmDb;
	tm_open("C:\\Troopmaster Software\\TM4\\DATA", &ptmDb); // todo: enforce slash or add??

	// TODO: check open error code and for NULL == ptmDb

	// select with the callback
	/* Create SQL statement */
	const char *sql = "SELECT * from test";

	// hack put table names in lower case for now since that is what recognize.
	/* Execute SQL statement */
	char *zErrMsg;
	// sqlite3_exec(pDb, sql, callback, (void*)3 /* data */, &zErrMsg);

	CallbackData *pCallbackData = new CallbackData();

	// global 
	pCallbackData->pDb = pDb;

	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "mbcdata";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from mbcdata", callback, (void*)pCallbackData/* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);



	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "attend";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from attend", callback, (void*)pCallbackData /* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);

	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "parentdata";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from parentdata", callback, (void*)pCallbackData/* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);


	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "scoutdata";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from scoutdata", callback, (void*)pCallbackData /* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);

	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "activity";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from activity", callback, (void*)pCallbackData/* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);


	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "adultdata";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from adultdata", callback, (void*)pCallbackData/* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);

	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "advancement";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from advancement", callback, (void*)pCallbackData/* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);

	sqlite3_exec(pDb, "BEGIN TRANSACTION;", NULL, NULL, NULL);
	pCallbackData->createdTabled = false;
	pCallbackData->pTableName = "pocdata";
	pCallbackData->recordCount = 0;
	tm_exec(ptmDb, "select * from pocdata", callback, (void*)pCallbackData/* data */, &zErrMsg);
	sqlite3_exec(pDb, "END TRANSACTION;", NULL, NULL, NULL);


	// display a table
	const char *sqlSelect = "SELECT * FROM Test;";

	char **results = NULL;
	int rows, columns;
	char *error; // language??
	int rc = sqlite3_get_table(pDb, sqlSelect, &results, &rows, &columns, &error);
	if (rc)
	{
		//cerr << "Error executing SQLite3 query: " << sqlite3_errmsg(db) << endl << endl;
		sqlite3_free(error);
	}
	else
	{
		// Display Table
		for (int rowCtr = 0; rowCtr <= rows; ++rowCtr)
		{
			for (int colCtr = 0; colCtr < columns; ++colCtr) // first row is the Column Names???
			{
				// Determine Cell Position
				int cellPosition = (rowCtr * columns) + colCtr;
				char *pResultText = results[cellPosition]; // todo how does this read different DataType.

				// Display Cell Value
			//	cout.width(12);
			//	cout.setf(ios::left);
			//	cout << results[cellPosition] << " ";
			}

			// End Line
			//cout << endl;

			// Display Separator For Header
			if (0 == rowCtr)
			{
				for (int colCtr = 0; colCtr < columns; ++colCtr)
				{
				//	cout.width(12);
				//	cout.setf(ios::left);
				//	cout << "~~~~~~~~~~~~ ";
				}
				// cout << endl;
			}
		}
	}
	sqlite3_free_table(results);

	// query the table.

	sqlite3_close(pDb);

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}
Пример #19
0
int main(int argc, char** argv)
{
   sqlite3 *db = NULL;
   char *errMsg = 0;
   char queryname[200];
   int nrow = 0;
   int ncolumn = 0;
   int i;
   char fileid[10];
   char filename[100], errinfo[200];
   char **selectResult;
   FILE *fp;
   int opendb = sqlite3_open("/tmp/ramdisk0/namebundle.db", &db);
   if(opendb)
   {
       printf("ERROR");
       return 1;
   }
   fp = fopen("/tmp/ramdisk0/querylog.txt", "a");
   strcpy(queryname, argv[1]); 
   //certain query section
   char selectsql[100] = "SELECT ID from BundleNameID where NAME=\"";
   strcat(selectsql, queryname);
   strcat(selectsql, "\";");
   sqlite3_get_table(db, selectsql, &selectResult, &nrow, &ncolumn, &errMsg);
   if(errMsg != NULL)
   {
      fprintf(fp, "select errMsg:%s\n", errMsg);
      strcpy(errinfo, errMsg);
      if(strcmp(errinfo, "database is locked") ==0)
      {
         while(errMsg != NULL)
         {
            usleep(100000);
            fprintf(fp, "select error retry message:%s\n", errinfo);
            sqlite3_get_table(db, selectsql, &selectResult, &nrow, &ncolumn, &errMsg);
         } 
      } 
   }
   if(nrow == 0)
   {
      printf("No");
      sqlite3_free_table(selectResult);
      sqlite3_close(db);
      exit(0);
   }
   for( i=1 ; i<( nrow + 1 ) * ncolumn ; i++ )
   {
      strcpy(fileid, selectResult[i]);
      strcpy(filename, "/tmp/ramdisk0/dtn/bundles/bundle_");
      strcat(filename, fileid);
      strcat(filename, ".dat");
      if( access( filename, F_OK ) != -1 )
      {
          printf("%s", filename);
          sqlite3_free_table(selectResult);
          sqlite3_close(db);
          exit(0);
      }
      else
      {
         char deletesql[100] = "DELETE from BundleNameID where ID=";
         strcat(deletesql, fileid);
         strcat(deletesql, ";");
         sqlite3_exec(db, deletesql, 0, 0, &errMsg);
         if(errMsg != NULL)
         {
            fprintf(fp, "delete errMsg:%s\n", errMsg);
            strcpy(errinfo, errMsg);
            if(strcmp(errinfo, "database is locked") ==0)
            {
               while(errMsg != NULL)
               {
                  usleep(100000);
                  fprintf(fp, "delete error retry message:%s\n", errinfo);
                  sqlite3_exec(db, deletesql, 0, 0, &errMsg);
               } 
            } 
         }
      }
   }
   printf("No");
   sqlite3_free_table(selectResult);
   sqlite3_close(db);
   fclose(fp);
   exit(0);
}
Пример #20
0
static tb_bool_t tb_database_sqlite3_done(tb_database_sql_impl_t* database, tb_char_t const* sql)
{
    // check
    tb_database_sqlite3_t* sqlite = tb_database_sqlite3_cast(database);
    tb_assert_and_check_return_val(sqlite && sqlite->database && sql, tb_false);

    // done
    tb_bool_t ok = tb_false;
    do
    {
        // exit result first if exists
        if (sqlite->result.result) sqlite3_free_table(sqlite->result.result);
        sqlite->result.result = tb_null;

        // clear the lasr statement first
        sqlite->result.statement = tb_null;

        // clear the result row count first
        sqlite->result.count = 0;

        // clear the result col count first
        sqlite->result.row.count = 0;

        // done sql
        tb_int_t    row_count = 0;
        tb_int_t    col_count = 0;
        tb_char_t*  error = tb_null;
        if (SQLITE_OK != sqlite3_get_table(sqlite->database, sql, &sqlite->result.result, &row_count, &col_count, &error))
        {
            // save state
            sqlite->base.state = tb_database_sqlite3_state_from_errno(sqlite3_errcode(sqlite->database));

            // trace
            tb_trace_e("done: sql: %s failed, error[%d]: %s", sql, sqlite3_errcode(sqlite->database), error);

            // exit error
            if (error) sqlite3_free(error);
            break;
        }

        // no result?
        if (!row_count)
        {
            // exit result
            if (sqlite->result.result) sqlite3_free_table(sqlite->result.result);
            sqlite->result.result = tb_null;

            // trace
            tb_trace_d("done: sql: %s: ok", sql);

            // ok
            ok = tb_true;
            break;
        }

        // save the result iterator mode
        sqlite->result.itor.mode = TB_ITERATOR_MODE_RACCESS | TB_ITERATOR_MODE_READONLY;

        // save result row count
        sqlite->result.count = row_count;

        // save result col count
        sqlite->result.row.count = col_count;

        // trace
        tb_trace_d("done: sql: %s: ok", sql);

        // ok
        ok = tb_true;
    
    } while (0);
    
    // ok?
    return ok;
}
Пример #21
0
/*
 * Execute SQL statement. For select statements only.
 * If fails, program terminates.
 */
DB_RESULT zbx_db_vselect(const char *fmt, va_list args)
{
	char	*sql = NULL;
	DB_RESULT result;

/*	double	sec;*/

#ifdef	HAVE_ORACLE
	sqlo_stmt_handle_t sth;
#endif
#ifdef	HAVE_SQLITE3
	int ret = FAIL;
	char *error=NULL;
#endif
#ifdef	HAVE_POSTGRESQL
	char	*error = NULL;
#endif

/*	sec = zbx_time();*/

	sql = zbx_dvsprintf(sql, fmt, args);

	zabbix_log( LOG_LEVEL_DEBUG, "Query [%s]", sql);

#ifdef	HAVE_MYSQL
	if(!conn)
	{
		zabbix_errlog(ERR_Z3003);
		result = NULL;
	}
	else
	{
		if(mysql_query(conn,sql) != 0)
		{
			zabbix_errlog(ERR_Z3005, mysql_errno(conn), mysql_error(conn), sql);
			switch(mysql_errno(conn)) {
				case 	CR_CONN_HOST_ERROR:
				case	CR_SERVER_GONE_ERROR:
				case	CR_CONNECTION_ERROR:
				case	CR_SERVER_LOST:
				case	ER_SERVER_SHUTDOWN:
				case	ER_UNKNOWN_ERROR:
					result = (DB_RESULT)ZBX_DB_DOWN;
					break;
				default:
					result = NULL;
					break;
			}
		}
		else
		{
			result = mysql_store_result(conn);
		}
	}
#endif
#ifdef	HAVE_POSTGRESQL
	result = zbx_malloc(NULL, sizeof(ZBX_PG_DB_RESULT));
	result->pg_result = PQexec(conn, sql);
	result->values = NULL;
	result->cursor = 0;
	result->row_num = 0;

	if (NULL == result->pg_result)
	{
		zabbix_errlog(ERR_Z3005, 0, "Result is NULL", sql);
	}
	if (PGRES_TUPLES_OK != PQresultStatus(result->pg_result))
	{
		error = zbx_dsprintf(error, "%s:%s",
				PQresStatus(PQresultStatus(result->pg_result)),
				PQresultErrorMessage(result->pg_result));
		zabbix_errlog(ERR_Z3005, 0, error, sql);
		zbx_free(error);
	}
	else	/* init rownum */
		result->row_num = PQntuples(result->pg_result);

#endif
#ifdef	HAVE_ORACLE
	if(0 > (sth = (sqlo_open(oracle, sql,0,NULL))))
	{
		zabbix_errlog(ERR_Z3005, 0, sqlo_geterror(oracle), sql);
		exit(FAIL);
	}
	result = sth;
#endif
#ifdef HAVE_SQLITE3
	if(!sqlite_transaction_started)
	{
		php_sem_acquire(&sqlite_access);
	}

	result = zbx_malloc(NULL, sizeof(ZBX_SQ_DB_RESULT));
	result->curow = 0;

lbl_get_table:
	if(SQLITE_OK != (ret = sqlite3_get_table(conn,sql,&result->data,&result->nrow, &result->ncolumn, &error)))
	{
		if(ret == SQLITE_BUSY) goto lbl_get_table; /* attention deadlock!!! */

		zabbix_errlog(ERR_Z3005, 0, error, sql);
		sqlite3_free(error);
		if(!sqlite_transaction_started)
		{
			php_sem_release(&sqlite_access);
		}
		exit(FAIL);
	}

	if(!sqlite_transaction_started)
	{
		php_sem_release(&sqlite_access);
	}
#endif

/*	sec = zbx_time() - sec;
	if(sec > 0.1)
		zabbix_log( LOG_LEVEL_WARNING, "Long query: " ZBX_FS_DBL " sec, \"%s\"", sec, sql);*/

	zbx_free(sql);
	return result;
}
Пример #22
0
void
DatabaseSqlite::get_nearest_station(double lat, double lon,
                                    std::string& country, std::string& region,
                                    std::string& code, std::string& name,
                                    double& latitude, double& longitude)
{
    char sql[512];
    int rc;
    char *errMsg = NULL;
    char **result;
    int nrow, ncol;
    double  distance,
            min_distance = 40000.0;

#ifdef DEBUGFUNCTIONCALL
    START_FUNCTION;
#endif
    if (!db)
        return; /* database doesn't open */
    snprintf(sql,
             sizeof(sql) - 1,
             "select regions.name, stations.code, stations.name, stations.latitude, stations.longititude, countries.name \
             from regions left join stations on regions.id=stations.region_id \
             left join countries on regions.country_id=countries.id \
             where regions.latitudemax>%f and regions.latitudemin<%f and regions.longititudemax>%f and regions.longititudemin<%f",
             lat, lat, lon, lon);
    std::cerr << "sql = " << sql << std::endl;

    rc = sqlite3_get_table(db,
                           sql,
                           &result,
                           &nrow,
                           &ncol,
                           &errMsg);

    //rc = sqlite3_exec(db, sql, callback, 0, &errMsg);
    if(rc != SQLITE_OK){
#ifndef RELEASE
        std::cerr << errMsg << std::endl;
#endif
        sqlite3_free(errMsg);
        return;
    }

    std::cerr << (ncol*nrow) << " " << ncol << " " << nrow  << std::endl;
    for (int i=0; i<ncol*nrow; i=i+6){
        distance = DatabaseSqlite::calculate_distance(lat, lon, atof(result[ncol+i+3]), atof(result[ncol+i+4]));
        if (distance < min_distance){
            min_distance = distance;
            country = result[ncol+i+5];
            region = result[ncol+i+0];
            code = result[ncol+i+1];
            name = result[ncol+i+2];
            latitude = atof(result[ncol+i+3]);
            longitude = atof(result[ncol+i+4]);
        }
    }
    sqlite3_free_table(result);

#ifdef DEBUGFUNCTIONCALL
    END_FUNCTION;
#endif

}
Пример #23
0
void OGRSQLiteViewLayer::BuildWhere()

{
    osWHERE = "";

    if( m_poFilterGeom != NULL && bHasSpatialIndex )
    {
        OGREnvelope  sEnvelope;

        m_poFilterGeom->getEnvelope( &sEnvelope );

        /* We first check that the spatial index table exists */
        if (!bHasCheckedSpatialIndexTable)
        {
            bHasCheckedSpatialIndexTable = TRUE;
            char **papszResult;
            int nRowCount, nColCount;
            char *pszErrMsg = NULL;

            CPLString osSQL;
            osSQL.Printf("SELECT name FROM sqlite_master "
                        "WHERE name='idx_%s_%s'",
                        pszEscapedUnderlyingTableName, osUnderlyingGeometryColumn.c_str());

            int  rc = sqlite3_get_table( poDS->GetDB(), osSQL.c_str(),
                                        &papszResult, &nRowCount,
                                        &nColCount, &pszErrMsg );

            if( rc != SQLITE_OK )
            {
                CPLError( CE_Failure, CPLE_AppDefined, "Error: %s",
                        pszErrMsg );
                sqlite3_free( pszErrMsg );
                bHasSpatialIndex = FALSE;
            }
            else
            {
                if (nRowCount != 1)
                {
                    bHasSpatialIndex = FALSE;
                }

                sqlite3_free_table(papszResult);
            }
        }

        if (bHasSpatialIndex)
        {
            osWHERE.Printf("WHERE %s IN ( SELECT pkid FROM 'idx_%s_%s' WHERE "
                           "xmax > %.12f AND xmin < %.12f AND ymax > %.12f AND ymin < %.12f) ",
                            pszFIDColumn,
                            pszEscapedUnderlyingTableName, osUnderlyingGeometryColumn.c_str(),
                            sEnvelope.MinX - 1e-11, sEnvelope.MaxX + 1e-11,
                            sEnvelope.MinY - 1e-11, sEnvelope.MaxY + 1e-11);
        }
        else
        {
            CPLDebug("SQLITE", "Count not find idx_%s_%s layer. Disabling spatial index",
                     pszEscapedUnderlyingTableName, osUnderlyingGeometryColumn.c_str());
        }

    }

    if( m_poFilterGeom != NULL && bSpatialiteLoaded && !bHasSpatialIndex )
    {
        OGREnvelope  sEnvelope;

        m_poFilterGeom->getEnvelope( &sEnvelope );

        /* A bit inefficient but still faster than OGR filtering */
        osWHERE.Printf("WHERE MBRIntersects(\"%s\", BuildMBR(%.12f, %.12f, %.12f, %.12f)) ",
                       osGeomColumn.c_str(),
                       sEnvelope.MinX - 1e-11, sEnvelope.MinY - 1e-11,
                       sEnvelope.MaxX + 1e-11, sEnvelope.MaxY + 1e-11);
    }

    if( strlen(osQuery) > 0 )
    {
        if( strlen(osWHERE) == 0 )
        {
            osWHERE.Printf( "WHERE %s ", osQuery.c_str()  );
        }
        else	
        {
            osWHERE += "AND (";
            osWHERE += osQuery;
            osWHERE += ")";
        }
    }
}
jobject Java_com_tencent_mobileqq_persistence_FTSDatatbaseDao_queryFTSGroups(JNIEnv* env, jobject thiz, jstring jsql, jstring jclasspath)
{
    // 获取ArrayList class类
    jclass list_clazz = (*env)->FindClass(env, "java/util/ArrayList");

    // 获取ArrayList类构造函数ID
    jmethodID list_init = (*env)->GetMethodID(env, list_clazz , "<init>", "()V");

    // 获取ArrayList类add函数ID
    jmethodID list_add  = (*env)->GetMethodID(env, list_clazz, "add", "(Ljava/lang/Object;)Z");

    // 构造ArrayList对象
    jobject list_obj = (*env)->NewObject(env, list_clazz , list_init);

    // 获取FTSMsgGroupItem class类
    const char* classpath = (*env)->GetStringUTFChars(env, jclasspath, NULL);
    jclass group_clazz = (*env)->FindClass(env, classpath);
    (*env)->ReleaseStringUTFChars(env, jclasspath, classpath);

    // 获取FTSMsgGroupItem类构造函数ID
    jmethodID group_init = (*env)->GetMethodID(env, group_clazz , "<init>", "(JII)V");

    // 搜索
    char** result;
    int nrows;
    int ncols;
    const char* sql = (*env)->GetStringUTFChars(env, jsql, NULL);
    int rc = sqlite3_get_table(db, sql, &result, &nrows, &ncols, NULL);
    (*env)->ReleaseStringUTFChars(env, jsql, sql);
    if (rc != SQLITE_OK)
    {
        logError("Can't query groups, ", sqlite3_errmsg(db));

        return list_obj;
    }

    // 搜索结果为空
    if (nrows == 0)
    {
        logWarn("FTS queryFTSGroups: nrows = 0", NULL);

        sqlite3_free_table(result);
        return list_obj;
    }

    // SQL查询语句,SELECT三个字段:uin、istroop、counts
    if (ncols != 3)
    {
        logWarn("FTS queryFTSGroups: ncols != 3", NULL);

        sqlite3_free_table(result);
        return list_obj;
    }

    int i;
    for (i = 0; i < nrows; ++i)
    {
        // 注意:java long和c/c++ long,不一样,小心掉坑里!!
        long long uin = atoll(result[(i + 1) * ncols + 0]);

        int istroop = atoi(result[(i + 1) * ncols + 1]);

        int counts = atoi(result[(i + 1) * ncols + 2]);

        // 构造FTSMsgGroupItem对象
        jobject group_obj = (*env)->NewObject(env, group_clazz, group_init, uin, istroop, counts);

        (*env)->CallBooleanMethod(env, list_obj, list_add, group_obj);

        // 避免 local reference table overflow (max=512) 错误
        (*env)->DeleteLocalRef(env, group_obj);
    }

    sqlite3_free_table(result);
    return list_obj;
}
jobject Java_com_tencent_mobileqq_persistence_FTSDatatbaseDao_queryFTSMsgs(JNIEnv* env, jobject thiz, jstring jsql, jstring jclasspath, jlong juin, jint jistroop, jobjectArray jstringarray)
{
    // 获取ArrayList class类
    jclass list_clazz = (*env)->FindClass(env, "java/util/ArrayList");

    // 获取ArrayList类构造函数ID
    jmethodID list_init = (*env)->GetMethodID(env, list_clazz , "<init>", "()V");

    // 获取ArrayList类add函数ID
    jmethodID list_add  = (*env)->GetMethodID(env, list_clazz, "add", "(Ljava/lang/Object;)Z");

    // 构造ArrayList对象
    jobject list_obj = (*env)->NewObject(env, list_clazz , list_init);

    // 获取FTSMsgItem class类
    const char* classpath = (*env)->GetStringUTFChars(env, jclasspath, NULL);
    jclass msg_clazz = (*env)->FindClass(env, classpath);
    (*env)->ReleaseStringUTFChars(env, jclasspath, classpath);

    // 获取FTSMsgItem类构造函数ID
    jmethodID msg_init = (*env)->GetMethodID(env, msg_clazz , "<init>", "(JIJJLjava/lang/String;)V");

    // 搜索
    char** result;
    int nrows;
    int ncols;
    const char* sql = (*env)->GetStringUTFChars(env, jsql, NULL);
    int rc = sqlite3_get_table(db, sql, &result, &nrows, &ncols, NULL);
    (*env)->ReleaseStringUTFChars(env, jsql, sql);
    if (rc != SQLITE_OK)
    {
        logError("Can't query msgs, ", sqlite3_errmsg(db));

        return list_obj;
    }

    // 搜索结果为空
    if (nrows == 0)
    {
        logWarn("FTS queryFTSMsgs: nrows = 0", NULL);

        sqlite3_free_table(result);
        return list_obj;
    }

    // SQL查询语句,SELECT五个字段:uin、istroop、time、shmsgseq,msg
    if (ncols != 5)
    {
        logWarn("FTS queryFTSMsgs: ncols != 5", NULL);

        sqlite3_free_table(result);
        return list_obj;
    }

    long long queryUin = (long long) juin;
    int queryIstroop = (int) jistroop;

    jsize len = (*env)->GetArrayLength(env, jstringarray);
    char** pstr = (char**) malloc(len * sizeof(char*));
    int i = 0;
    for (i = 0; i < len; i++)
    {
        jstring jstr = (*env)->GetObjectArrayElement(env, jstringarray, i);
        pstr[i] = (char*)(*env)->GetStringUTFChars(env, jstr, 0);

        // 避免 local reference table overflow (max=512) 错误
        (*env)->DeleteLocalRef(env, jstr);
    }

    for (i = 0; i < nrows; ++i)
    {
        // 注意:java long和c/c++ long,不一样,小心掉坑里!!
        long long uin = atoll(result[(i + 1) * ncols + 0]);

        int istroop = atoi(result[(i + 1) * ncols + 1]);

        if (queryUin != uin || queryIstroop != istroop)
        {
            continue;
        }

        long long time = atoll(result[(i + 1) * ncols + 2]);

        long long shmsgseq = atoll(result[(i + 1) * ncols + 3]);

        char* msg = result[(i + 1) * ncols + 4];

        // 后续fix,英文大小写的坑
        int ret = -1;
        int j = 0;
        for (j = 0; j < len; ++j)
        {
            if (strstr(msg, pstr[j]) != NULL)
            {
                ret = 0;
            }
        }
        if (ret == -1)
        {
            continue;
        }

        jstring jmsg = (*env)->NewStringUTF(env, msg);

        // 构造FTSMsgItem对象
        jobject msg_obj = (*env)->NewObject(env, msg_clazz, msg_init, uin, istroop, time, shmsgseq, jmsg);

        (*env)->CallBooleanMethod(env, list_obj, list_add, msg_obj);

        // 避免 local reference table overflow (max=512) 错误
        (*env)->DeleteLocalRef(env, msg_obj);
        (*env)->DeleteLocalRef(env, jmsg);
    }

    free(pstr);

    return list_obj;
}
static int
do_test (sqlite3 * handle, const void *p_cache)
{
    int ret;
    char *err_msg = NULL;
    int row_count;
    const char *sql;
    char **results;
    int rows;
    int columns;
    double tic;
    gaiaVectorLayersListPtr list;

    ret =
	sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL,
		      &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -2;
      }

    ret =
	load_shapefile (handle, "shp/merano-3d/polygons", "polygons", "CP1252",
			25832, "geom", 0, 1, 1, 0, &row_count, err_msg);
    if (!ret)
      {
	  fprintf (stderr, "load_shapefile() error: %s\n", err_msg);
	  sqlite3_close (handle);
	  return -3;
      }

    ret = update_layer_statistics (handle, "polygons", "geom");
    if (!ret)
      {
	  fprintf (stderr, "update_layer_statistics() error %s\n", err_msg);
	  sqlite3_close (handle);
	  return -8;
      }

    ret =
	load_shapefile (handle, "shp/merano-3d/roads", "roads", "CP1252", 25832,
			"geom", 0, 0, 1, 0, &row_count, err_msg);
    if (!ret)
      {
	  fprintf (stderr, "load_shapefile() error: %s\n", err_msg);
	  sqlite3_close (handle);
	  return -4;
      }

    ret =
	load_shapefile (handle, "shp/merano-3d/points", "points", "CP1252",
			25832, "geom", 0, 0, 1, 0, &row_count, err_msg);
    if (!ret)
      {
	  fprintf (stderr, "load_shapefile() error: %s\n", err_msg);
	  sqlite3_close (handle);
	  return -5;
      }

    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polygons (FEATURE_ID, DATUM, HAUSNR) VALUES (1250000, 0.1, 'alpha')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "INSERT polygons (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -14;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polygons (FEATURE_ID, DATUM, HAUSNR) VALUES (1250000, 0.1, 'alpha')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "INSERT polygons (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -15;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polygons (FEATURE_ID, DATUM, TEXT_I) VALUES (1250000, 0.1, 'alpha')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "INSERT polygons (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -16;
      }

    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE polyg_xy (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE polyg_xy error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -17;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('polyg_xy', 'geom', 25832, 'POLYGON', 'XY')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeomety polyg_xy error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -17;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polyg_xy (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXY(geom) FROM polygons WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding polyg_xy (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -18;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polyg_xy (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXY(geom) FROM polygons WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding polyg_xy (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -19;
      }
    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE polyg_xym (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE polyg_xym error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -20;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('polyg_xym', 'geom', 25832, 'POLYGON', 'XYM')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometry polyg_xym error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -21;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polyg_xym (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYM(geom) FROM polygons WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding polyg_xym (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -22;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polyg_xym (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYM(geom) FROM polygons WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding polyg_xym (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -23;
      }
    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE polyg_xyz (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE polyg_xyz error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -24;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('polyg_xyz', 'geom', 25832, 'POLYGON', 'XYZ')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometry polyg_xyz error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -25;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polyg_xyz (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZ(geom) FROM polygons WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding polyg_xyz (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -26;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO polyg_xyz (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZ(geom) FROM polygons WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding polyg_xyz (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -27;
      }

    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE roads_xyz (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE roads_xyz error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -28;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('roads_xyz', 'geom', 25832, 'LINESTRING', 'XYZ')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeomety roads_xyz error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -29;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO roads_xyz (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZ(geom) FROM roads WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding roads_xyz (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -30;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO roads_xyz (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZ(geom) FROM roads WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding roads_xyz (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -31;
      }
    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE roads_xym (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE roads_xym error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -32;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('roads_xym', 'geom', 25832, 'LINESTRING', 'XYM')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometry roads_xym error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -33;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO roads_xym (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYM(geom) FROM roads WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding roads_xym (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -34;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO roads_xym (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYM(geom) FROM roads WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding roads_xym (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -35;
      }
    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE roads_xyzm (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE roads_xyzm error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -36;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('roads_xyzm', 'geom', 25832, 'LINESTRING', 'XYZM')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometry roads_xyzm error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -37;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO roads_xyzm (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZM(geom) FROM roads WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding roads_xyzm (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -38;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO roads_xyzm (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZM(geom) FROM roads WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding roads_xyzm (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -39;
      }

    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE points_xyz (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE points_xyz error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -40;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('points_xyz', 'geom', 25832, 'POINT', 'XYZ')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeomety points_xyz error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -41;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO points_xyz (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZ(geom) FROM points WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding points_xyz (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -42;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO points_xyz (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZ(geom) FROM points WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding points_xyz (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -43;
      }
    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE points_xym (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE points_xym error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -44;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('points_xym', 'geom', 25832, 'POINT', 'XYM')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometry points_xym error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -45;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO points_xym (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYM(geom) FROM points WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding points_xym (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -46;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO points_xym (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYM(geom) FROM points WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding points_xym (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -47;
      }
    ret =
	sqlite3_exec (handle,
		      "CREATE TABLE points_xyzm (pk_elem INTEGER PRIMARY KEY AUTOINCREMENT, FEATURE_ID INTEGER, DATUM DOUBLE, TEXT_I TEXT, BLOB_I BLOB)",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE points_xyzm error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -48;
      }
    ret =
	sqlite3_exec (handle,
		      "SELECT AddGeometryColumn('points_xyzm', 'geom', 25832, 'POINT', 'XYZM')",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometry points_xyzm error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -49;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO points_xyzm (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZM(geom) FROM points WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding points_xyzm (1) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -50;
      }
    ret =
	sqlite3_exec (handle,
		      "INSERT INTO points_xyzm (pk_elem, FEATURE_ID, DATUM, TEXT_I, BLOB_I, geom) SELECT NULL, FEATURE_ID, DATUM, 'alpha', zeroblob(20), CastToXYZM(geom) FROM points WHERE geom IS NOT NULL",
		      NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "feeding points_xyzm (2) error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -51;
      }

    elementary_geometries (handle, "points", "geom", "elem_point", "pk_elem",
			   "mul_id");
    elementary_geometries (handle, "roads", "geom", "elem_linestring",
			   "pk_elem", "mul_id");
    elementary_geometries (handle, "polygons", "geom", "elem_poly", "pk_elem",
			   "mul_id");

    elementary_geometries (handle, "polyg_xy", "geom", "elem_poly_xy",
			   "pk_elem1", "mul_id");
    elementary_geometries (handle, "polyg_xym", "geom", "elem_poly_xym",
			   "pk_elem1", "mul_id");
    elementary_geometries (handle, "polyg_xyz", "geom", "elem_poly_xyz",
			   "pk_elem1", "mul_id");

    elementary_geometries (handle, "roads_xyz", "geom", "elem_roads_xy",
			   "pk_elem1", "mul_id");
    elementary_geometries (handle, "roads_xym", "geom", "elem_roads_xym",
			   "pk_elem1", "mul_id");
    elementary_geometries (handle, "roads_xyzm", "geom", "elem_roads_xyz",
			   "pk_elem1", "mul_id");

    elementary_geometries (handle, "points_xyz", "geom", "elem_points_xy",
			   "pk_elem1", "mul_id");
    elementary_geometries (handle, "points_xym", "geom", "elem_points_xym",
			   "pk_elem1", "mul_id");
    elementary_geometries (handle, "points_xyzm", "geom", "elem_points_xyz",
			   "pk_elem1", "mul_id");

    remove_duplicated_rows (handle, "polyg_xy");
    remove_duplicated_rows (handle, "polyg_xyz");
    remove_duplicated_rows (handle, "polyg_xym");
    remove_duplicated_rows (handle, "roads_xyz");
    remove_duplicated_rows (handle, "roads_xym");
    remove_duplicated_rows (handle, "roads_xyzm");
    remove_duplicated_rows (handle, "points_xyz");
    remove_duplicated_rows (handle, "points_xym");
    remove_duplicated_rows (handle, "points_xyzm");

    sql = "CREATE VIEW test_view AS "
	"SELECT ROWID AS ROWID, pk_elem AS id, geom AS geometry FROM roads_xyz";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE test_view error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -52;
      }

    sql =
	"INSERT INTO views_geometry_columns (view_name, view_geometry, view_rowid, "
	"f_table_name, f_geometry_column, read_only) VALUES "
	"('test_view', 'geometry', 'rowid', 'roads_xyz', 'geom', 1)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Register SpatialView error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -53;
      }

    ret = update_layer_statistics (handle, NULL, NULL);
    if (!ret)
      {
	  fprintf (stderr, "update_layer_statistics() error %s\n", err_msg);
	  sqlite3_close (handle);
	  return -54;
      }

    sql = "SELECT row_count, extent_min_x, extent_max_y "
	"FROM geometry_columns_statistics "
	"WHERE f_table_name LIKE 'roads' " "AND f_geometry_column LIKE 'geom'";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -55;
      }
    if (rows != 1)
      {
	  fprintf (stderr,
		   "Unexpected num of rows for GEOMETRY_COLUMNS_STATISTICS.\n");
	  return -56;
      }
    if (atoi (results[columns + 0]) != 18)
      {
	  fprintf (stderr,
		   "Unexpected error: GEOMETRY_COLUMNS_STATISTICS bad result row_count: %s\n",
		   results[columns + 0]);
	  return -57;
      }
    tic = fabs (atof (results[columns + 1]) - 666057.648017325);
    if (tic >= 0.00000002)
      {
	  fprintf (stderr,
		   "Unexpected error: GEOMETRY_COLUMNS_STATISTICS bad result extent_min_x: %s\n",
		   results[columns + 1]);
	  return -58;
      }
    tic = fabs (atof (results[columns + 2]) - 5170671.31627796);
    if (tic >= 0.0000002)
      {
	  fprintf (stderr,
		   "Unexpected error: GEOMETRY_COLUMNS_STATISTICS bad result extent_max_y: %s\n",
		   results[columns + 2]);
	  return -59;
      }
    sqlite3_free_table (results);

    ret = gaiaDropTable (handle, "polygons");
    if (!ret)
      {
	  fprintf (stderr, "DROP polygons error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -60;
      }

/* checking gaiaGetVectorLayersList() - ALL */
    list =
	gaiaGetVectorLayersList (handle, NULL, NULL,
				 GAIA_VECTORS_LIST_OPTIMISTIC);
    gaiaFreeVectorLayersList (list);

#ifdef ENABLE_LWGEOM		/* only if LWGEOM is supported */

    if (p_cache == NULL)
	ret = check_all_geometry_columns (handle, "./report", NULL, NULL);
    else
	ret =
	    check_all_geometry_columns_r (p_cache, handle, "./report", NULL,
					  NULL);
    if (!ret)
      {
	  fprintf (stderr, "check_all_geometry_columns() error\n");
	  sqlite3_close (handle);
	  return -61;
      }

    if (p_cache == NULL)
	ret =
	    sanitize_all_geometry_columns (handle, "tmp_", "./report", NULL,
					   NULL);
    else
	ret =
	    sanitize_all_geometry_columns_r (p_cache, handle, "tmp_",
					     "./report", NULL, NULL);
    if (!ret)
      {
	  fprintf (stderr, "sanitize_all_geometry_columns() error\n");
	  sqlite3_close (handle);
	  return -62;
      }

#endif /* end LWGEOM conditionals */

/* checking gaiaGetVectorLayersList() - Table */
    list =
	gaiaGetVectorLayersList (handle, "elem_points_xyz", NULL,
				 GAIA_VECTORS_LIST_OPTIMISTIC);
    gaiaFreeVectorLayersList (list);

/* checking gaiaGetVectorLayersList() - Table and Geometry */
    list =
	gaiaGetVectorLayersList (handle, "elem_roads_xy", "geom",
				 GAIA_VECTORS_LIST_OPTIMISTIC);
    gaiaFreeVectorLayersList (list);

    return 0;
}
Пример #27
0
static void doOne(sqlite3 *sqlite, int bid, int tid, int aid, int delta)
{
    int aBalance = 0;
    int nrows, ncols, rc, retries = 500, intrans;
    char **rows, *sql = 0;

    if (sqlite == NULL) {
        incrementFailedTransactionCount();
	return;
    }
again:
    intrans = 0;
    if (transactions) {
        rc = sqlite3_exec(sqlite, begtrans, NULL, NULL, NULL);
	if (rc != SQLITE_OK) {
	    stat_counts[0] += 1;
	    goto transfail;
	}
	intrans = 1;
    }
    sql = sqlite3_mprintf("UPDATE accounts "
			  "SET Abalance = Abalance + %d WHERE Aid = %d",
			  delta, aid);
    rc = sqlite3_exec(sqlite, sql, NULL, NULL, NULL);
    if (rc != SQLITE_OK) {
        stat_counts[1] += 1;
        goto transfail;
    }
    sqlite3_free(sql);
    sql = sqlite3_mprintf("SELECT Abalance "
			  "FROM accounts WHERE Aid = %d", aid);
    rc = sqlite3_get_table(sqlite, sql, &rows, &nrows, &ncols, NULL);
    if (rc != SQLITE_OK) {
        stat_counts[2] += 1;
        goto transfail;
    }
    sqlite3_free(sql);
    if (nrows == 1 && ncols == 1 && rows && rows[1]) {
        aBalance = strtol(rows[1], NULL, 0);
    }
    if (rows) {
        sqlite3_free_table(rows);
    }
    sql = sqlite3_mprintf("UPDATE tellers "
			  "SET Tbalance = Tbalance + %d WHERE Tid = %d",
			  delta, tid);
    rc = sqlite3_exec(sqlite, sql, NULL, NULL, NULL);
    if (rc != SQLITE_OK) {
        stat_counts[3] += 1;
        goto transfail;
    }
    sqlite3_free(sql);
    sql = sqlite3_mprintf("UPDATE branches "
			  "SET Bbalance = Bbalance + %d WHERE Bid = %d",
			  delta, bid);
    rc = sqlite3_exec(sqlite, sql, NULL, NULL, NULL);
    if (rc != SQLITE_OK) {
        stat_counts[4] += 1;
        goto transfail;
    }
    sqlite3_free(sql);
    sql = sqlite3_mprintf("INSERT INTO history"
			  "(Tid, Bid, Aid, delta) VALUES"
			  "(%d, %d, %d, %d)",
			  tid, bid, aid, delta);
    rc = sqlite3_exec(sqlite, sql,  NULL, NULL, NULL);
    if (rc != SQLITE_OK) {
        stat_counts[5] += 1;
        goto transfail;
    }
    sqlite3_free(sql);
    sql = 0;
    if (transactions) {
try_commit:
        rc = sqlite3_exec(sqlite, "COMMIT TRANSACTION", NULL, NULL, NULL);
	if (rc == SQLITE_BUSY && --retries > 0) {
	    stat_counts[9] += 1;
#ifdef _WIN32
	    Sleep(10);
#else
	    usleep(10000);
#endif
	    goto try_commit;
	}
	if (rc != SQLITE_OK) {
	    stat_counts[6] += 1;
	    goto transfail;
	}
    }
    return;
transfail:
    if (sql) {
        sqlite3_free(sql);
	sql = 0;
    }
    if (rc == SQLITE_BUSY && --retries > 0) {
        if (intrans) {
	    sqlite3_exec(sqlite, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
	    stat_counts[7] += 1;
	}
	stat_counts[8] += 1;
#ifdef _WIN32
	Sleep(10);
#else
	usleep(10000);
#endif
        goto again;
    }
    incrementFailedTransactionCount();
    if (intrans) {
	stat_counts[10] += 1;
	sqlite3_exec(sqlite, "ROLLBACK TRANSACTION", NULL, NULL, NULL);
    }
}
Пример #28
0
static void createDatabase()
{
    sqlite3 *sqlite;
    int dotrans = 0;
    int accountsnb = 0;
    int i, nrows, ncols;
    char **rows;

    if (sqlite3_open(dbname, &sqlite) != SQLITE_OK) {
        fprintf(stderr, "unable to connect to %s\n", dbname);
	exit(1);
    }
#ifdef TRACE_DBINIT
    sqlite3_trace(sqlite, dbtrace, NULL);
#endif
    if (sqlite3_exec(sqlite, begtrans, NULL, NULL, NULL)
	== SQLITE_OK) {
        dotrans = 1;
    }
    if (sqlite3_get_table(sqlite, "SELECT count(*) FROM accounts",
			  &rows, &ncols, &nrows, NULL)
	== SQLITE_OK) {
        if (rows && ncols == 1 && nrows == 1 && rows[1]) {
	    accountsnb = strtol(rows[1], NULL, 0);
	}
	if (rows) {
	    sqlite3_free_table(rows);
	}
	if (dotrans) {
	    sqlite3_exec(sqlite, "COMMIT TRANSACTION", NULL, NULL, NULL);
	}
	if (accountsnb == naccounts * tps) {
            fprintf(stdout, "Already initialized\n");
	    fflush(stdout);
	    sqlite3_close(sqlite);
	    return;
        }
    }
    fprintf(stdout, "Drop old tables if they exist\n");
    fflush(stdout);
    if (sqlite3_exec(sqlite, "DROP TABLE history; "
		     "DROP TABLE accounts; "
		     "DROP TABLE tellers; "
		     "DROP TABLE branches; ", NULL, NULL, NULL)
	== SQLITE_OK) {
	if (dotrans) {
	    sqlite3_exec(sqlite, combegtrans, NULL, NULL, NULL);
	}
    }
    fprintf(stdout, "Create tables\n");
    fflush(stdout);
    sqlite3_exec(sqlite, "CREATE TABLE branches ("
		 "Bid INTEGER NOT NULL PRIMARY KEY, "
		 "Bbalance INTEGER, "
		 "filler CHAR(88))", NULL, NULL, NULL);
    sqlite3_exec(sqlite, "CREATE TABLE tellers ("
		 "Tid INTEGER NOT NULL PRIMARY KEY, "
		 "Bid INTEGER, "
		 "Tbalance INTEGER, "
		 "filler CHAR(84))", NULL, NULL, NULL);
    sqlite3_exec(sqlite, "CREATE TABLE accounts ("
		 "Aid INTEGER NOT NULL PRIMARY KEY, "
		 "Bid INTEGER, "
		 "Abalance INTEGER, "
		 "filler CHAR(84))", NULL, NULL, NULL);
    sqlite3_exec(sqlite, "CREATE TABLE history ("
		 "Tid INTEGER, "
		 "Bid INTEGER, "
		 "Aid INTEGER, "
		 "delta INTEGER, "
		 "tstime TIMESTAMP, "
		 "filler CHAR(22))", NULL, NULL, NULL);
    fprintf(stdout, "Delete elements in table in case DROP didn't work\n");
    fflush(stdout);
    sqlite3_exec(sqlite, "DELETE FROM history; "
		 "DELETE FROM accounts; "
		 "DELETE FROM tellers; "
		 "DELETE FROM branches ", NULL, NULL, NULL);
    if (dotrans) {
        sqlite3_exec(sqlite, combegtrans, NULL, NULL, NULL);
    }
    fprintf(stdout, "Insert data in branches table\n");
    fflush(stdout);
    for (i = 0; i < nbranches * tps; i++) {
        char *sql = sqlite3_mprintf("INSERT INTO branches(Bid,Bbalance) "
				    "VALUES (%d,0)", i);

        sqlite3_exec(sqlite, sql, NULL, NULL, NULL);
	sqlite3_free(sql);
	if (i % 100 == 0 && dotrans) {
	    sqlite3_exec(sqlite, combegtrans, NULL, NULL, NULL);
	}
    }
    if (dotrans) {
        sqlite3_exec(sqlite, combegtrans, NULL, NULL, NULL);
    }
    fprintf(stdout, "Insert data in tellers table\n");
    fflush(stdout);
    for (i = 0; i < ntellers * tps; i++) {
        char *sql = sqlite3_mprintf("INSERT INTO tellers(Tid,Bid,Tbalance) "
				    "VALUES (%d,%d,0)",
				    i, i / ntellers);

        sqlite3_exec(sqlite, sql, NULL, NULL, NULL);
	sqlite3_free(sql);
	if (i % 100 == 0 && dotrans) {
	    sqlite3_exec(sqlite, combegtrans, NULL, NULL, NULL);
	}
    }
    if (dotrans) {
        sqlite3_exec(sqlite, combegtrans, NULL, NULL, NULL);
    }
    fprintf(stdout, "Insert data in accounts table\n");
    fflush(stdout);
    for (i = 0; i < naccounts * tps; i++) {
        char *sql = sqlite3_mprintf("INSERT INTO accounts(Aid,Bid,Abalance) "
				    "VALUES (%d,%d,0)",
				    i, i / naccounts);

        sqlite3_exec(sqlite, sql, NULL, NULL, NULL);
	sqlite3_free(sql);
	if (i % 10000 == 0 && dotrans) {
	    sqlite3_exec(sqlite, combegtrans, NULL, NULL, NULL);
	}
	if (i > 0 && i % 10000 == 0) {
	    fprintf(stdout,"\t%d\trecords inserted\n", i);
	    fflush(stdout);
	}
    }
    if (dotrans) {
        sqlite3_exec(sqlite, "COMMIT TRANSACTION", NULL, NULL, NULL);
    }
    fprintf(stdout, "\t%d\trecords inserted\n", naccounts * tps);
    fflush(stdout);
    sqlite3_close(sqlite);
}
/* set locale in the android_metadata table, install localized collators, and rebuild indexes */
static void native_setLocale(JNIEnv* env, jobject object, jstring localeString, jint flags)
{
    if ((flags & NO_LOCALIZED_COLLATORS)) return;

    int err;
    char const* locale8 = env->GetStringUTFChars(localeString, NULL);
    sqlite3 * handle = (sqlite3 *)env->GetIntField(object, offset_db_handle);
    sqlite3_stmt* stmt = NULL;
    char** meta = NULL;
    int rowCount, colCount;
    char* dbLocale = NULL;

    // create the table, if necessary and possible
    if (!(flags & OPEN_READONLY)) {
        static const char *createSql ="CREATE TABLE IF NOT EXISTS " ANDROID_TABLE " (locale TEXT)";
        err = sqlite3_exec(handle, createSql, NULL, NULL, NULL);
        if (err != SQLITE_OK) {
            LOGE("CREATE TABLE " ANDROID_TABLE " failed\n");
            throw_sqlite3_exception(env, handle);
            goto done;
        }
    }

    // try to read from the table
    static const char *selectSql = "SELECT locale FROM " ANDROID_TABLE " LIMIT 1";
    err = sqlite3_get_table(handle, selectSql, &meta, &rowCount, &colCount, NULL);
    if (err != SQLITE_OK) {
        LOGE("SELECT locale FROM " ANDROID_TABLE " failed\n");
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    dbLocale = (rowCount >= 1) ? meta[1 * colCount + 0] : NULL;

    if (dbLocale != NULL && !strcmp(dbLocale, locale8)) {
        // database locale is the same as the desired locale; set up the collators and go
        err = register_localized_collators(handle, locale8, UTF16_STORAGE);
        if (err != SQLITE_OK) throw_sqlite3_exception(env, handle);
        goto done;   // no database changes needed
    }

    if ((flags & OPEN_READONLY)) {
        // read-only database, so we're going to have to put up with whatever we got
        err = register_localized_collators(handle, dbLocale ? dbLocale : locale8, UTF16_STORAGE);
        if (err != SQLITE_OK) throw_sqlite3_exception(env, handle);
        goto done;
    }

    // need to update android_metadata and indexes atomically, so use a transaction...
    err = sqlite3_exec(handle, "BEGIN TRANSACTION", NULL, NULL, NULL);
    if (err != SQLITE_OK) {
        LOGE("BEGIN TRANSACTION failed setting locale\n");
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    err = register_localized_collators(handle, dbLocale ? dbLocale : locale8, UTF16_STORAGE);
    if (err != SQLITE_OK) {
        LOGE("register_localized_collators() failed setting locale\n");
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    err = sqlite3_exec(handle, "DELETE FROM " ANDROID_TABLE, NULL, NULL, NULL);
    if (err != SQLITE_OK) {
        LOGE("DELETE failed setting locale\n");
        throw_sqlite3_exception(env, handle);
        goto rollback;
    }

    static const char *sql = "INSERT INTO " ANDROID_TABLE " (locale) VALUES(?);";
    err = sqlite3_prepare_v2(handle, sql, -1, &stmt, NULL);
    if (err != SQLITE_OK) {
        LOGE("sqlite3_prepare_v2(\"%s\") failed\n", sql);
        throw_sqlite3_exception(env, handle);
        goto rollback;
    }

    err = sqlite3_bind_text(stmt, 1, locale8, -1, SQLITE_TRANSIENT);
    if (err != SQLITE_OK) {
        LOGE("sqlite3_bind_text() failed setting locale\n");
        throw_sqlite3_exception(env, handle);
        goto rollback;
    }

    err = sqlite3_step(stmt);
    if (err != SQLITE_OK && err != SQLITE_DONE) {
        LOGE("sqlite3_step(\"%s\") failed setting locale\n", sql);
        throw_sqlite3_exception(env, handle);
        goto rollback;
    }

    err = sqlite3_exec(handle, "REINDEX LOCALIZED", NULL, NULL, NULL);
    if (err != SQLITE_OK) {
        LOGE("REINDEX LOCALIZED failed\n");
        throw_sqlite3_exception(env, handle);
        goto rollback;
    }

    // all done, yay!
    err = sqlite3_exec(handle, "COMMIT TRANSACTION", NULL, NULL, NULL);
    if (err != SQLITE_OK) {
        LOGE("COMMIT TRANSACTION failed setting locale\n");
        throw_sqlite3_exception(env, handle);
        goto done;
    }

rollback:
    sqlite3_exec(handle, "ROLLBACK TRANSACTION", NULL, NULL, NULL);

done:
    if (locale8 != NULL) env->ReleaseStringUTFChars(localeString, locale8);
    if (stmt != NULL) sqlite3_finalize(stmt);
    if (meta != NULL) sqlite3_free_table(meta);
}
int
main (int argc, char *argv[])
{
    int ret;
    sqlite3 *handle;
    char *err_msg = NULL;
    const char *sql;
    char **results;
    int rows;
    int columns;
    void *cache = spatialite_alloc_connection ();

    if (argc > 1 || argv[0] == NULL)
	argc = 1;		/* silencing stupid compiler warnings */

    ret =
	sqlite3_open_v2 (":memory:", &handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open in-memory database: %s\n",
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return -1;
      }

    spatialite_init_ex (handle, cache, 0);

    ret =
	sqlite3_exec (handle, "SELECT InitSpatialMetadata(1)", NULL, NULL,
		      &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  sqlite3_close (handle);
	  return -2;
      }
/* creating a Point XY table */
    sql = "CREATE TABLE pt_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -3;
      }

/* creating a Point XYZ table */
    sql = "CREATE TABLE pt_xyz (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -4;
      }

/* creating a Point XYM table */
    sql = "CREATE TABLE pt_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -5;
      }

/* creating a Point XYZM table */
    sql = "CREATE TABLE pt_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -6;
      }

/* creating a Linestring XY table */
    sql = "CREATE TABLE ln_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -7;
      }

/* creating a Linestring XYZ table */
    sql = "CREATE TABLE ln_xyz (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -8;
      }

/* creating a Linestring XYM table */
    sql = "CREATE TABLE ln_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -9;
      }

/* creating a Linestring XYZM table */
    sql = "CREATE TABLE ln_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -10;
      }

/* creating a Polygon XY table */
    sql = "CREATE TABLE pg_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -11;
      }

/* creating a Polygon XYZ table */
    sql = "CREATE TABLE pg_xyz (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -12;
      }

/* creating a Polygon XYM table */
    sql = "CREATE TABLE pg_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -13;
      }

/* creating a Polygon XYZM table */
    sql = "CREATE TABLE pg_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -14;
      }

/* creating a MultiPoint XY table */
    sql = "CREATE TABLE mpt_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -15;
      }

/* creating a MultiPoint XYZ table */
    sql = "CREATE TABLE mpt_XYZ (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -16;
      }

/* creating a MultiPoint XYM table */
    sql = "CREATE TABLE mpt_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -17;
      }

/* creating a MultiPoint XYZM table */
    sql = "CREATE TABLE mpt_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -18;
      }

/* creating a MultiLinestring XY table */
    sql = "CREATE TABLE mln_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -19;
      }

/* creating a MultiLinestring XYZ table */
    sql = "CREATE TABLE mln_xyz (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -20;
      }

/* creating a MultiLinestring XYM table */
    sql = "CREATE TABLE mln_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -21;
      }

/* creating a MultiLinestring XYZM table */
    sql = "CREATE TABLE mln_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -22;
      }

/* creating a MultiPolygon XY table */
    sql = "CREATE TABLE mpg_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -23;
      }

/* creating a MultiPolygon XYZ table */
    sql = "CREATE TABLE mpg_xyz (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -24;
      }

/* creating a MultiPolygon XYM table */
    sql = "CREATE TABLE mpg_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -25;
      }

/* creating a MultiPolygon XYZM table */
    sql = "CREATE TABLE mpg_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -26;
      }

/* creating a GeometryCollection XY table */
    sql = "CREATE TABLE gcoll_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -27;
      }

/* creating a GeometryCollection XYZ table */
    sql = "CREATE TABLE gcoll_xyz (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -28;
      }

/* creating a GeometryCollection XYM table */
    sql = "CREATE TABLE gcoll_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -29;
      }

/* creating a GeometryCollection XYZM table */
    sql = "CREATE TABLE gcoll_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -30;
      }


/* creating a Geometry XY table */
    sql = "CREATE TABLE geom_xy (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -31;
      }

/* creating a Geometry XYZ table */
    sql = "CREATE TABLE geom_xyz (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -32;
      }

/* creating a Geometry XYM table */
    sql = "CREATE TABLE geom_xym (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -33;
      }

/* creating a Geometry XYZM table */
    sql = "CREATE TABLE geom_xyzm (id INTEGER, g BLOB)";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -34;
      }

/* Inserting into pt_xy */
    sql =
	"INSERT INTO pt_xy (id, g) VALUES (1, GeomFromText('POINT(1 2)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -35;
      }

/* Inserting into pt_xyz */
    sql =
	"INSERT INTO pt_xyz (id, g) VALUES (1, GeomFromText('POINTZ(1 2 3)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -36;
      }

/* Inserting into pt_xym */
    sql =
	"INSERT INTO pt_xym (id, g) VALUES (1, GeomFromText('POINTM(1 2 10)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -37;
      }

/* Inserting into pt_xyzm */
    sql =
	"INSERT INTO pt_xyzm (id, g) VALUES (1, GeomFromText('POINTZM(1 2 3 10)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -38;
      }

/* Inserting into ln_xy */
    sql =
	"INSERT INTO ln_xy (id, g) VALUES (1, GeomFromText('LINESTRING(1 2, 4 5)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -39;
      }

/* Inserting into ln_xyz */
    sql =
	"INSERT INTO ln_xyz (id, g) VALUES (1, GeomFromText('LINESTRINGZ(1 2 3, 4 5 6)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -40;
      }

/* Inserting into ln_xym */
    sql =
	"INSERT INTO ln_xym (id, g) VALUES (1, GeomFromText('LINESTRINGM(1 2 10, 4 5 11)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -41;
      }

/* Inserting into ln_xyzm */
    sql =
	"INSERT INTO ln_xyzm (id, g) VALUES (2, GeomFromText('LINESTRINGZM(1 2 3 10, 4 5 6 11)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -42;
      }

/* Inserting into pg_xy */
    sql =
	"INSERT INTO pg_xy (id, g) VALUES (1, GeomFromText('POLYGON((10 10, 15 10, 15 15, 10 15, 10 10), (11 11, 12 11, 1 12, 11 12, 11 11))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -43;
      }

/* Inserting into pg_xyz */
    sql =
	"INSERT INTO pg_xyz (id, g) VALUES (1, GeomFromText('POLYGONZ((10 10 100, 15 10 101, 15 15 102, 10 15 103, 10 10 100), (11 11 100, 12 11 101, 1 12 102, 11 12 103, 11 11 100))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -44;
      }

/* Inserting into pg_xym */
    sql =
	"INSERT INTO pg_xym (id, g) VALUES (1, GeomFromText('POLYGONM((10 10 10, 15 10 11, 15 15 12, 10 15 13, 10 10 10), (11 11 10, 12 11 11, 1 12 12, 11 12 13, 11 11 10))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -45;
      }

/* Inserting into pg_xyzm */
    sql =
	"INSERT INTO pg_xyzm (id, g) VALUES (1, GeomFromText('POLYGONZM((10 10 100 10, 15 10 101 11, 15 15 102 12, 10 15 103 13, 10 10 100 10), (11 11 100 10, 12 11 101 11, 1 12 102 12, 11 12 103 13, 11 11 100 10))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -46;
      }

/* Inserting into mpt_xy */
    sql =
	"INSERT INTO mpt_xy (id, g) VALUES (1, GeomFromText('MULTIPOINT(1 2, 4 5)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -47;
      }

/* Inserting into mpt_xyz */
    sql =
	"INSERT INTO mpt_xyz (id, g) VALUES (1, GeomFromText('MULTIPOINTZ(1 2 3, 4 5 6)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -48;
      }

/* Inserting into mpt_xym */
    sql =
	"INSERT INTO mpt_xym (id, g) VALUES (1, GeomFromText('MULTIPOINTM(1 2 10, 4 5 11)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -49;
      }

/* Inserting into mpt_xyzm */
    sql =
	"INSERT INTO mpt_xyzm (id, g) VALUES (1, GeomFromText('MULTIPOINTZM(1 2 3 10, 4 5 6 10)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -50;
      }

/* Inserting into mln_xy */
    sql =
	"INSERT INTO mln_xy (id, g) VALUES (1, GeomFromText('MULTILINESTRING((1 2, 4 5), (7 8, 10 11))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -51;
      }

/* Inserting into mln_xyz */
    sql =
	"INSERT INTO mln_xyz (id, g) VALUES (1, GeomFromText('MULTILINESTRINGZ((1 2 3, 4 5 6), (7 8 9, 10 11 12))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -52;
      }

/* Inserting into mln_xym */
    sql =
	"INSERT INTO mln_xym (id, g) VALUES (1, GeomFromText('MULTILINESTRINGM((1 2 10, 4 5 11), (7 8 12, 10 11 13))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -53;
      }

/* Inserting into mln_xyzm */
    sql =
	"INSERT INTO mln_xyzm (id, g) VALUES (1, GeomFromText('MULTILINESTRINGZM((1 2 3 10, 4 5 6 11), (7 8 9 12, 10 11 12 13))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -54;
      }

/* Inserting into mpg_xy */
    sql =
	"INSERT INTO mpg_xy (id, g) VALUES (1, GeomFromText('MULTIPOLYGON(((10 10, 15 10, 15 15, 10 15, 10 10), (11 11, 12 11, 1 12, 11 12, 11 11)), ((0 0, 1 0, 1 1, 0 1, 0 0)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -55;
      }

/* Inserting into mpg_xyz */
    sql =
	"INSERT INTO mpg_xyz (id, g) VALUES (1, GeomFromText('MULTIPOLYGONZ(((10 10 100, 15 10 101, 15 15 102, 10 15 103, 10 10 100), (11 11 100, 12 11 101, 1 12 102, 11 12 103, 11 11 100)), ((0 0 1, 1 0 2, 1 1 3, 0 1 4, 0 0 1)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -56;
      }

/* Inserting into mpg_xym */
    sql =
	"INSERT INTO mpg_xym (id, g) VALUES (1, GeomFromText('MULTIPOLYGONM(((10 10 11, 15 10 12, 15 15 13, 10 15 14, 10 10 11), (11 11 5, 12 11 6, 12 12 7, 11 12 8, 11 11 5)), ((0 0 11, 1 0 12, 1 1 13, 0 1 14, 0 0 11)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -57;
      }

/* Inserting into mpg_xyzm */
    sql =
	"INSERT INTO mpg_xyzm (id, g) VALUES (1, GeomFromText('MULTIPOLYGONZM(((10 10 100 11, 15 10 101 12, 15 15 102 13, 10 15 103 14, 10 10 100 11), (11 11 100 5, 12 11 101 6, 1 12 102 7, 11 12 103 8, 11 11 100 5)), ((0 0 1 11, 1 0 2 12, 1 1 3 13, 0 1 4 14, 0 0 1 11)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -58;
      }

/* Inserting into gcoll_xy */
    sql =
	"INSERT INTO gcoll_xy (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), LINESTRING(5 5, 6 6), POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -59;
      }

/* Inserting into gcoll_xyz */
    sql =
	"INSERT INTO gcoll_xyz (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTIONZ(POINTZ(10 10 100), LINESTRINGZ(5 5 10, 6 6 11), POLYGONZ((0 0 1, 1 0 2, 1 1 3, 0 1 4, 0 0 1)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -60;
      }

/* Inserting into gcoll_xym */
    sql =
	"INSERT INTO gcoll_xym (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTIONM(POINTM(10 10 100), LINESTRINGM(5 5 10, 6 6 11), POLYGONM((0 0 1, 1 0 2, 1 1 3, 0 1 4, 0 0 1)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -61;
      }

/* Inserting into gcoll_xyzm */
    sql =
	"INSERT INTO gcoll_xyzm (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTIONZM(POINTZM(10 10 100 11), LINESTRINGZM(5 5 10 11, 6 6 11 12), POLYGONZM((0 0 1 10, 1 0 2 11, 1 1 3 12, 0 1 4 13, 0 0 1 10)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -62;
      }

/* Inserting into geom_xy */
    sql =
	"INSERT INTO geom_xy (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), LINESTRING(5 5, 6 6), POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -63;
      }
    sql =
	"INSERT INTO geom_xy (id, g) VALUES (2, GeomFromText('POINT(10 10)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -64;
      }

/* Inserting into geom_xyz */
    sql =
	"INSERT INTO geom_xyz (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTIONZ(POINTZ(10 10 100), LINESTRINGZ(5 5 10, 6 6 11), POLYGONZ((0 0 1, 1 0 2, 1 1 3, 0 1 4, 0 0 1)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -65;
      }
    sql =
	"INSERT INTO geom_xyz (id, g) VALUES (2, GeomFromText('POINTZ(10 10 100)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -66;
      }

/* Inserting into geom_xym */
    sql =
	"INSERT INTO geom_xym (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTIONM(POINTM(10 10 100), LINESTRINGM(5 5 10, 6 6 11), POLYGONM((0 0 1, 1 0 2, 1 1 3, 0 1 4, 0 0 1)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -67;
      }
    sql =
	"INSERT INTO geom_xym (id, g) VALUES (2, GeomFromText('POINTM(10 10 100)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -68;
      }

/* Inserting into geom_xyzm */
    sql =
	"INSERT INTO geom_xyzm (id, g) VALUES (1, GeomFromText('GEOMETRYCOLLECTIONZM(POINTZM(10 10 100 11), LINESTRINGZM(5 5 10 11, 6 6 11 12), POLYGONZM((0 0 1 10, 1 0 2 11, 1 1 3 12, 0 1 4 13, 0 0 1 10)))', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -69;
      }
    sql =
	"INSERT INTO geom_xyzm (id, g) VALUES (2, GeomFromText('POINTZM(10 10 100 11)', 4326))";
    ret = sqlite3_exec (handle, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -70;
      }

/* recovering pt_xy */
    sql = "SELECT RecoverGeometryColumn('pt_xy', 'g', 4326, 'POINT', 2);";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -71;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -72;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -73;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -74;
      }
    sqlite3_free_table (results);

/* recovering pt_xyz */
    sql = "SELECT RecoverGeometryColumn('pt_xyz', 'g', 4326, 'POINT', 3);";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -75;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -76;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -77;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -78;
      }
    sqlite3_free_table (results);

/* recovering pt_xym */
    sql = "SELECT RecoverGeometryColumn('pt_xym', 'g', 4326, 'POINT', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -79;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -80;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -81;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -82;
      }
    sqlite3_free_table (results);

/* recovering pt_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('pt_xyzm', 'g', 4326, 'POINT', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -83;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -84;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -85;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -86;
      }
    sqlite3_free_table (results);

/* recovering ln_xy */
    sql =
	"SELECT RecoverGeometryColumn('ln_xy', 'g', 4326, 'LINESTRING', 'XY');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -87;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -88;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -89;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -90;
      }
    sqlite3_free_table (results);

/* recovering ln_xyz */
    sql =
	"SELECT RecoverGeometryColumn('ln_xyz', 'g', 4326, 'LINESTRING', 'XYZ');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -91;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -92;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -93;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -94;
      }
    sqlite3_free_table (results);

/* recovering ln_xym */
    sql =
	"SELECT RecoverGeometryColumn('ln_xym', 'g', 4326, 'LINESTRING', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -95;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -96;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -97;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -98;
      }
    sqlite3_free_table (results);

/* recovering ln_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('ln_xyzm', 'g', 4326, 'LINESTRING', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -99;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -100;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -101;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -102;
      }
    sqlite3_free_table (results);

/* recovering pg_xy */
    sql = "SELECT RecoverGeometryColumn('pg_xy', 'g', 4326, 'POLYGON', 'XY');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -103;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -104;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -105;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -106;
      }
    sqlite3_free_table (results);

/* recovering pg_xyz */
    sql =
	"SELECT RecoverGeometryColumn('pg_xyz', 'g', 4326, 'POLYGON', 'XYZ');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -107;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -108;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -109;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -110;
      }
    sqlite3_free_table (results);

/* recovering pg_xym */
    sql =
	"SELECT RecoverGeometryColumn('pg_xym', 'g', 4326, 'POLYGON', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -111;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -112;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -113;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -114;
      }
    sqlite3_free_table (results);

/* recovering pg_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('pg_xyzm', 'g', 4326, 'POLYGON', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -115;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -116;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -117;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -118;
      }
    sqlite3_free_table (results);

/* recovering mpt_xy */
    sql =
	"SELECT RecoverGeometryColumn('mpt_xy', 'g', 4326, 'MULTIPOINT', 'XY');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -119;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -120;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -121;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -122;
      }
    sqlite3_free_table (results);

/* recovering mpt_xyz */
    sql =
	"SELECT RecoverGeometryColumn('mpt_xyz', 'g', 4326, 'MULTIPOINT', 'XYZ');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -123;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -124;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -125;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -126;
      }
    sqlite3_free_table (results);

/* recovering mpt_xym */
    sql =
	"SELECT RecoverGeometryColumn('mpt_xym', 'g', 4326, 'MULTIPOINT', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -127;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -128;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -129;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -130;
      }
    sqlite3_free_table (results);

/* recovering mpt_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('mpt_xyzm', 'g', 4326, 'MULTIPOINT', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -131;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -132;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -133;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -134;
      }
    sqlite3_free_table (results);

/* recovering mln_xy */
    sql =
	"SELECT RecoverGeometryColumn('mln_xy', 'g', 4326, 'MULTILINESTRING', 'XY');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -135;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -136;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -137;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -138;
      }
    sqlite3_free_table (results);

/* recovering mln_xyz */
    sql =
	"SELECT RecoverGeometryColumn('mln_xyz', 'g', 4326, 'MULTILINESTRING', 'XYZ');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -139;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -140;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -141;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -142;
      }
    sqlite3_free_table (results);

/* recovering mln_xym */
    sql =
	"SELECT RecoverGeometryColumn('mln_xym', 'g', 4326, 'MULTILINESTRING', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -143;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -144;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -145;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -146;
      }
    sqlite3_free_table (results);

/* recovering mln_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('mln_xyzm', 'g', 4326, 'MULTILINESTRING', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -147;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -148;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -149;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -150;
      }
    sqlite3_free_table (results);

/* recovering mpg_xy */
    sql =
	"SELECT RecoverGeometryColumn('mpg_xy', 'g', 4326, 'MULTIPOLYGON', 'XY');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -151;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -152;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -153;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -154;
      }
    sqlite3_free_table (results);

/* recovering mpg_xyz */
    sql =
	"SELECT RecoverGeometryColumn('mpg_xyz', 'g', 4326, 'MULTIPOLYGON', 'XYZ');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -155;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -156;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -157;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -158;
      }
    sqlite3_free_table (results);

/* recovering mpg_xym */
    sql =
	"SELECT RecoverGeometryColumn('mpg_xym', 'g', 4326, 'MULTIPOLYGON', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -159;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -160;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -161;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -162;
      }
    sqlite3_free_table (results);

/* recovering mpg_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('mpg_xyzm', 'g', 4326, 'MULTIPOLYGON', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -163;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -164;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -165;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -166;
      }
    sqlite3_free_table (results);

/* recovering gcoll_xy */
    sql =
	"SELECT RecoverGeometryColumn('gcoll_xy', 'g', 4326, 'GEOMETRYCOLLECTION', 'XY');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -167;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -168;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -169;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -170;
      }
    sqlite3_free_table (results);

/* recovering gcoll_xyz */
    sql =
	"SELECT RecoverGeometryColumn('gcoll_xyz', 'g', 4326, 'GEOMETRYCOLLECTION', 'XYZ');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -171;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -172;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -173;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -174;
      }
    sqlite3_free_table (results);

/* recovering gcoll_xym */
    sql =
	"SELECT RecoverGeometryColumn('gcoll_xym', 'g', 4326, 'GEOMETRYCOLLECTION', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -175;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -176;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -177;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -178;
      }
    sqlite3_free_table (results);

/* recovering gcoll_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('gcoll_xyzm', 'g', 4326, 'GEOMETRYCOLLECTION', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -179;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -180;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -181;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -182;
      }
    sqlite3_free_table (results);

/* recovering geom_xy */
    sql =
	"SELECT RecoverGeometryColumn('geom_xy', 'g', 4326, 'GEOMETRY', 'XY');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -183;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -184;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -185;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -186;
      }
    sqlite3_free_table (results);

/* recovering geom_xyz */
    sql =
	"SELECT RecoverGeometryColumn('geom_xyz', 'g', 4326, 'GEOMETRY', 'XYZ');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -187;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -188;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -189;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -190;
      }
    sqlite3_free_table (results);

/* recovering geom_xym */
    sql =
	"SELECT RecoverGeometryColumn('geom_xym', 'g', 4326, 'GEOMETRY', 'XYM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -191;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -192;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -193;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -194;
      }
    sqlite3_free_table (results);

/* recovering geom_xyzm */
    sql =
	"SELECT RecoverGeometryColumn('geom_xyzm', 'g', 4326, 'GEOMETRY', 'XYZM');";
    ret = sqlite3_get_table (handle, sql, &results, &rows, &columns, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "Error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return -195;
      }
    if ((rows != 1) || (columns != 1))
      {
	  fprintf (stderr, "Unexpected error: bad result: %i/%i.\n", rows,
		   columns);
	  return -196;
      }
    if (results[1] == NULL)
      {
	  fprintf (stderr, "Unexpected error: NULL result\n");
	  return -197;
      }
    if (strcmp (results[1], "1") != 0)
      {
	  fprintf (stderr, "Unexpected error: invalid result\n");
	  return -198;
      }
    sqlite3_free_table (results);

    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "sqlite3_close() error: %s\n",
		   sqlite3_errmsg (handle));
	  return -199;
      }

    spatialite_cleanup_ex (cache);

    return 0;
}