05.10.2009, 10:47
I'm not sure if this is a bug or some SQL thing, but using GROUP BY in SQL queries seems to break db_get_field_assoc.
Using this gives
as expected, but when the SELECT query is changed to
(or grouping by value), it gives
It does work properly when using the query
but shouldn't that be the same?
Using db_field_name without the AS gives "`string`" and "`value`" instead of "string" and "value", maybe this is the problem.
pawn Code:
#include <a_samp>
public OnFilterScriptInit()
{
new DB:db = db_open("test.sqlite");
if(db) {
db_free_result(db_query(db, "CREATE TABLE IF NOT EXISTS `Test` ( `string` TEXT NOT NULL DEFAULT '' , `value` INTEGER NOT NULL DEFAULT 0 ); DELETE FROM `Test`"));
db_free_result(db_query(db, "INSERT INTO `Test` ( `string` , `value` ) VALUES ( 'abc' , 123 )"));
db_free_result(db_query(db, "INSERT INTO `Test` ( `string` , `value` ) VALUES ( 'def' , 456 )"));
db_free_result(db_query(db, "INSERT INTO `Test` ( `string` , `value` ) VALUES ( 'hij' , 789 )"));
new DBResult:result = db_query(db, "SELECT `string` , `value` FROM `Test`"),
rows = db_num_rows(result);
for(new i; i < rows; i++) {
new string[20],
value;
db_get_field_assoc(result, "value", string, sizeof(string));
value = strval(string);
db_get_field_assoc(result, "string", string, sizeof(string));
printf("String: \"%s\", Value: %i", string, value);
db_next_row(result);
}
db_free_result(result);
db_close(db);
}
return 1;
}
Quote:
String: "abc", Value: 123 String: "def", Value: 456 String: "hij", Value: 789 |
pawn Code:
"SELECT `string` , `value` FROM `Test` GROUP BY `string`"
Quote:
String: "", Value: 0 String: "", Value: 0 String: "", Value: 0 |
pawn Code:
"SELECT `string` AS `string` , `value` AS `value` FROM `Test` GROUP BY `string`"
Using db_field_name without the AS gives "`string`" and "`value`" instead of "string" and "value", maybe this is the problem.