How to make this code short?
#1

hello.

As you can see i'm having trouble here, It is because the PAWN cannot compile the script due the code i added has very long lengths and off limits in PAWN.

How can i shorten this code?

pawn Код:
db_query(Database,
    "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, ip VARCHAR(16) COLLATE NOCASE, password VARCHAR(129), score INTEGER DEFAULT 0 NOT NULL, money INTEGER DEFAULT 5000 NOT NULL hours INTEGER DEFAULT 0 NOT NULL, minutes INTEGER DEFAULT 0 NOT NULL, seconds INTEGER DEFAULT 0 NOT NULL, admin INTEGER DEFAULT 0 NOT NULL, vip INTEGER DEFAULT 0 NOT NULL, speedboost INTEGER DEFAULT 1 NOT NULL, cookies INTEGER DEFAULT 0 NOT NULL, mb INTEGER DEFAULT 0 NOT NULL)");
Reply
#2

strcat
Reply
#3

-Too late, konstantinos got ahead by many milliseconds-
Reply
#4

I've try this

pawn Код:
strcat(string, "CREATE TABLE IF NOT EXISTS users ");
    strcat(string, " (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, ip VARCHAR(16) COLLATE NOCASE, password VARCHAR(129),");
    strcat(string, " score INTEGER DEFAULT 0 NOT NULL, money INTEGER DEFAULT 5000 NOT NULL hours INTEGER DEFAULT 0 NOT NULL, minutes INTEGER DEFAULT 0 NOT NULL, seconds INTEGER DEFAULT 0 NOT NULL,");
    strcat(string, " admin INTEGER DEFAULT 0 NOT NULL, vip INTEGER DEFAULT 0 NOT NULL, speedboost INTEGER DEFAULT 1 NOT NULL, cookies INTEGER DEFAULT 0 NOT NULL,");
    strcat(string, " mb INTEGER DEFAULT 0 NOT NULL)");
    db_query(Database, string);
But didn't work, It always create a empty .db file like what i experience before in my previous topic.
Reply
#5

The lenght is not enough then and it fails to execute the query.

Do:
pawn Код:
strcat(string, "CREATE TABLE IF NOT EXISTS users ");
    strcat(string, " (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) COLLATE NOCASE, ip VARCHAR(16) COLLATE NOCASE, password VARCHAR(129),");
    strcat(string, " score INTEGER DEFAULT 0 NOT NULL, money INTEGER DEFAULT 5000 NOT NULL hours INTEGER DEFAULT 0 NOT NULL, minutes INTEGER DEFAULT 0 NOT NULL, seconds INTEGER DEFAULT 0 NOT NULL,");
    strcat(string, " admin INTEGER DEFAULT 0 NOT NULL, vip INTEGER DEFAULT 0 NOT NULL, speedboost INTEGER DEFAULT 1 NOT NULL, cookies INTEGER DEFAULT 0 NOT NULL,");
    strcat(string, " mb INTEGER DEFAULT 0 NOT NULL)");
    db_query(Database, string);
    print( string ); // Let's see the output
And see if it cuts the query.
Reply
#6

Finally works.



I've to look carefully next time, It is just one missing , WTF.
Reply
#7

You can do this i think

pawn Код:
db_query(Database,
    "CREATE TABLE IF NOT EXISTS users (userid INTEGER PRIMARY KEY AUTOINCREMENT, username VARCHAR(24) \
COLLATE NOCASE, ip VARCHAR(16) COLLATE NOCASE, password VARCHAR(129), score INTEGER DEFAULT 0 NOT NULL,\
 money INTEGER DEFAULT 5000 NOT NULL hours INTEGER DEFAULT 0 NOT NULL, minutes INTEGER DEFAULT 0 NOT \
NULL, seconds INTEGER DEFAULT 0 NOT NULL, admin INTEGER DEFAULT 0 NOT NULL, vip INTEGER DEFAULT 0 NOT \
NULL, speedboost INTEGER DEFAULT 1 NOT NULL, cookies INTEGER DEFAULT 0 NOT NULL, mb INTEGER DEFAULT 0 NOT NULL)"
);
Reply
#8

That strcat thing is a bad suggestion. In order for strcat to work, it must count through every character in the string (every time, starting from 0, even when you know it's gonna be bigger than it was for the last strcat) until it finds the end and then start copying characters from the source to that end position, essentially expanding the string. It's also extra useless since all this data is constant and doesn't need variables at all.

However, since the compiler is apparently hating the long constant string length (which is a pretty annoying compiler trait) I guess I can't suggest any better alternative (std:tring's aren't an option sadly lol) but I'm sure there must be something if you want to aim for perfection (*hint*).
Reply
#9

Quote:
Originally Posted by Deji
Посмотреть сообщение
That strcat thing is a bad suggestion. In order for strcat to work, it must count through every character in the string (every time, starting from 0, even when you know it's gonna be bigger than it was for the last strcat) until it finds the end and then start copying characters from the source to that end position, essentially expanding the string. It's also extra useless since all this data is constant and doesn't need variables at all.

However, since the compiler is apparently hating the long constant string length (which is a pretty annoying compiler trait) I guess I can't suggest any better alternative (std:tring's aren't an option sadly lol) but I'm sure there must be something if you want to aim for perfection (*hint*).
Actually using strcat for that - it IS faster than some other methods which using format which is pretty slow.
Reply
#10

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Actually using strcat for that - it IS faster than some other methods which using format which is pretty slow.
Have you tried that theory in all situations? You couldn't have, that would take more time than the universe has (I assume the universe clock will face a 2038-type problem eventually and wrap around to negative time where it's life will begin again).

Sure, scanning one string for input and working on another for output will be incredibly slow most of the time. But with the simplicity of the format used in this case (it just has to check two characters and it knows what to do) and the fact that the function knows where the end of the string is and doesn't have to count it over time, it would mean that over longer strings with more concatenations the format should actually gain efficiency over repeated strcat's which have to count strings of longer and longer lengths repeatedly.

Just a theory but the logic makes sense. I won't go to great lengths to test it now but if I were to be faced with the possibility to use both in a particular situation where optimisation is a must, I would test both methods with my particular code instead of going with the popular belief unless I was certain that one way was better... Even then, who knows, it might just be that under the current circumstances, there is a surprising exception.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)