SQL queries using GROUP BY
#2

Not using quotes works but that's not really the point.

Quote:
SQLite adds new keywords from time to time when it takes on new features. So to prevent your code from being broken by future enhancements, you should normally quote any identifier that is an English language word, even if you do not have to.
Code:
#include <a_samp>

public OnFilterScriptInit()
{
	new DB:db = db_open("test.sqlite");
	if(db) {
		db_free_result(db_query(db, "DROP TABLE IF EXISTS `Test`; CREATE TABLE `Test` ( `a` TEXT NOT NULL DEFAULT '', `b` TEXT NOT NULL DEFAULT '' , `c` TEXT NOT NULL DEFAULT '' , `d` TEXT NOT NULL DEFAULT '' , `e` TEXT NOT NULL DEFAULT '' )"));

		db_free_result(db_query(db, "INSERT INTO `Test` ( `a` , `b` ,`c` , `d` ,`e` ) VALUES ( 'abc' , 'def', 'ghi' , 'jkl' , 'mno' )"));

		new DBResult:result = db_query(db, "SELECT a , `b` , [c] , \"d\" , `e` AS `e` FROM Test GROUP BY `a`"),
			rows = db_num_rows(result),
			string[128];
		for(new i; i < rows; i++) {
			new fields = db_num_fields(result);
			format(string, sizeof(string), "%sRow %i\n", string, i);
			for(new j; j < fields; j++) {
				new field_name[20],
					field_value[20];
				db_field_name(result, j, field_name, sizeof(field_name));
				db_get_field(result, j, field_value, sizeof(field_value));
				format(string, sizeof(string), "%s %s: %s\n", string, field_name, field_value);
			}
			db_next_row(result);
		}
		print(string);
		db_free_result(result);
		db_close(db);
	}
	return 1;
}
Quote:

Row 0
a: abc
`b`: def
[c]: ghi
"d": jkl
e: mno

For some reason sa-mp isn't striping the quotes from the field name unless AS is used.
Reply


Messages In This Thread
SQL queries using GROUP BY - by Dabombber - 05.10.2009, 10:47
Re: SQL queries using GROUP BY - by Dabombber - 05.10.2009, 19:47

Forum Jump:


Users browsing this thread: 1 Guest(s)