Esempio n. 1
0
	/**
	 * Get a database row that contains a given product id.
	 * @param productID Identifies the desired row.
	 * @return A database row if the the productID was found, NULL otherwise.
	 * The result's ownership is passed to the caller.
	 */
	DatabaseProduct* DatabaseManager::getRow(const MAUtil::String& productID)
	{
		DatabaseProduct* product = NULL;
		char buffer[BUF_SIZE];
		sprintf(buffer, "SELECT (date) FROM %s WHERE productid = '%s'",
			TABLE_NAME,
			productID.c_str());
		printf("Select statement: %s", buffer);
		MAHandle cursorx = maDBExecSQL(mDatabase, buffer);
		if (cursorx <= 0)
		{
			return NULL;
		}
		printf("cursorx = %d", cursorx);
		int result = maDBCursorNext(cursorx);
		printf("result maDBCursorNext = %d", result);
		if (result == MA_DB_OK)
		{
			int date;
			result = maDBCursorGetColumnInt(cursorx, 0, &date);
			printf("result maDBCursorGetColumnInt = %d", result);
			if (result == MA_DB_OK)
			{
				printf("date = %d", date);
				product = new DatabaseProduct();
				product->setProductID(productID);
				product->setDate(date);
			}
		}
		maDBCursorDestroy(cursorx);
		return product;
	}
Esempio n. 2
0
int testdb2()
{
	int db = maDBOpen("c:\\cprog\\testdb.db");
	if (db < 1)
	{
		printf("maDBOpen failed\n");
	}

	maDBExecSQL(db, "DROP TABLE pet");

	int result = maDBExecSQL(db,
		"CREATE TABLE pet (name TEXT(50), age INTEGER, playfulness DOUBLE)");
	if (0 != result)
	{
		printf("CREATE TABLE failed\n");
	}

	result = maDBExecSQL(db, "INSERT INTO pet VALUES ('Kurre', 13, 0.65)");
	if (0 != result)
	{
		printf("INSERT 1 failed\n");
	}
	result = maDBExecSQL(db, "INSERT INTO pet VALUES ('Vilma', 10, 0.999)");
	if (0 != result)
	{
		printf("INSERT 2 failed\n");
	}

	// Test to get the number of rows.
	MAHandle cursor1 = maDBExecSQL(db, "SELECT COUNT(*) FROM (SELECT * FROM pet)");
	if (cursor1 < 1)
	{
		printf("SELECT COUNT failed\n");
	}
	maDBCursorNext(cursor1);
	int numberOfRows;
	maDBCursorGetColumnInt(cursor1, 0, &numberOfRows);
	printf("Number of rows: %d\n", numberOfRows);
	maDBCursorDestroy(cursor1);

	MAHandle cursor = maDBExecSQL(db, "SELECT * FROM pet");
	if (cursor < 1)
	{
		printf("SELECT failed\n");
	}

	// Print all rows.
	char name[51];
	int age;
	double playfulness;
	while (0 == maDBCursorNext(cursor))
	{
		int length = maDBCursorGetColumnText(cursor, 0, name, 50);
		printf("length: %d\n", length);
		if (length <= 50)
		{
			name[length] = 0;
		}
		else
		{
			strcpy(name, "ERROR: name buffer too small");
		}
		maDBCursorGetColumnInt(cursor, 1, &age);
		maDBCursorGetColumnDouble(cursor, 2, &playfulness);
		printf("%s %d %f\n", name, age, playfulness);
	}

	// Free the cursor.
	maDBCursorDestroy(cursor);

	// Close the database.
	maDBClose(db);
}