SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 18/06/2014, 07:08 PM   #1
vannesenn
Gangsta
 
vannesenn's Avatar
 
Join Date: Feb 2011
Posts: 607
Reputation: 15
Default [HELP] SQLite funcs

Hi guys,
can someone give some example how I can open/close and load/update my SQLite database? And how I can later open db in notepad etc.?

Thanks
vannesenn is offline   Reply With Quote
Old 18/06/2014, 07:14 PM   #2
Clad
Banned
 
Join Date: Dec 2013
Location: Detroit
Posts: 1,237
Reputation: 85
Default Re: [HELP] SQLite funcs

This maybe usefull.
Clad is offline   Reply With Quote
Old 18/06/2014, 07:30 PM   #3
vannesenn
Gangsta
 
vannesenn's Avatar
 
Join Date: Feb 2011
Posts: 607
Reputation: 15
Default Re: [HELP] SQLite funcs

I readed that and I still understand :/
How I can make database from notepad and how I can open db in notepad?
vannesenn is offline   Reply With Quote
Old 18/06/2014, 07:34 PM   #4
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,827
Reputation: 1401
Default Re: [HELP] SQLite funcs

You can open a database by using SQLite Database Browser.
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 18/06/2014, 07:35 PM   #5
vannesenn
Gangsta
 
vannesenn's Avatar
 
Join Date: Feb 2011
Posts: 607
Reputation: 15
Default Re: [HELP] SQLite funcs

Ok, but can I make 1 player = 1 database or I must make in 1 db?
vannesenn is offline   Reply With Quote
Old 18/06/2014, 07:39 PM   #6
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,827
Reputation: 1401
Default Re: [HELP] SQLite funcs

For register/login system? No.

1 database > 1 table > players' data (1 row for 1 player)
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 18/06/2014, 07:45 PM   #7
vannesenn
Gangsta
 
vannesenn's Avatar
 
Join Date: Feb 2011
Posts: 607
Reputation: 15
Default Re: [HELP] SQLite funcs

So if I have 69 players, I have 1 SQLite database and 69 rows and one row is one player?
That is like a MySQL

And, is better y_INI or SQLite? What is easy to make and use?
vannesenn is offline   Reply With Quote
Old 18/06/2014, 07:57 PM   #8
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,827
Reputation: 1401
Default Re: [HELP] SQLite funcs

Yes, correct.

Both are easy and good (you might consider using SQLitei (by Slice) instead though in case you pick SQLite). If you want to make top x (e.g top 10 kills), then go with SQL and not files.
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 18/06/2014, 08:00 PM   #9
vannesenn
Gangsta
 
vannesenn's Avatar
 
Join Date: Feb 2011
Posts: 607
Reputation: 15
Default Re: [HELP] SQLite funcs

Yeas, but I think that is SQLite too hard for me....
Lot of job for normal load and save :/

I read on SA:MP wiki about SQLite and I really don't Know why is (`Username` = '%s') in code
http://wiki.sa-mp.com/wiki/Db_query
Maybe is in this problem why I don't understand SQLite and MySQL... :/
vannesenn is offline   Reply With Quote
Old 19/06/2014, 11:31 AM   #10
vannesenn
Gangsta
 
vannesenn's Avatar
 
Join Date: Feb 2011
Posts: 607
Reputation: 15
Default Re: [HELP] SQLite funcs

And how I can make .db file...
I opened your .db file from your tut from singature and I saw some letters but when I open my .db file I saw normal letters...

EDIT: I edit your script from your tut and I don't Know why load/save system don't work...
Can you help me please?

Code:
#include <a_samp>

#undef          MAX_PLAYERS
#define         MAX_PLAYERS             (50)

native WP_Hash( buffer[ ], len, const str[ ] );

enum korisnik_var
{
	Sifra[129],
	Admin,
	Novac
}

new Korisnik[MAX_PLAYERS][korisnik_var];
new bool:logiran[MAX_PLAYERS];

new DB:Database;

main() { print("Ucitan je! :)"); }

public OnGameModeInit( )
{
    SetGameModeText("Basic SQLite Gamemode");

    Database = db_open("server_database.db");
  	db_query( Database, "CREATE TABLE IF NOT EXISTS KORISNICI (Ime VARCHAR(24) COLLATE NOCASE, Sifra VARCHAR(129), Admin INTEGER DEFAULT 0 NOT NULL Novac INTEGER DEFAULT 0 NOT NULL)" );
    return 1;
}

public OnGameModeExit()
{
    db_close(Database);
    return 1;
}

public OnPlayerConnect(playerid)
{
	new name[24];
	new Query[71];
	new DBResult: Result;
	
    GetPlayerName(playerid, name, 24);

    format(Query, sizeof(Query), "SELECT Sifra FROM KORISNICI WHERE Ime = '%s' LIMIT 0, 1", DB_Escape(name));
    Result = db_query(Database, Query);
    
    if(db_num_rows(Result))
    {
        db_get_field_assoc(Result, "Sifra", Korisnik[playerid][Sifra], 129);
        ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "Prijava", "Ukucajte Vašu šifru za prijavu na Server.", "Prijava", "Izađi");
    }
    else ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "Registracija", "Ukucajte Vašu šifru da se registrirate na Server.", "Registracija", "Izađi");
    db_free_result(Result);
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(logiran[playerid] == true)
    {
        new name[24];
        new Query[72];
        
		GetPlayerName(playerid, name, 24);

        format(Query, sizeof(Query), "UPDATE KORISNICI SET Admin = %d WHERE Ime = '%s'", Korisnik[playerid][Admin], DB_Escape(name));
        db_query(Database, Query);
        format(Query, sizeof(Query), "UPDATE KORISNICI SET Novac = %d WHERE Ime = '%s'", GetPlayerMoney(playerid), DB_Escape(name));
        db_query(Database, Query);
    }
    return 1;
}

//-----------------------------------------------------
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	if(dialogid == 1)
	{
	    if(response)
	    {
	        if(!IsValidPassword(inputtext))
	        {
	            SendClientMessage( playerid, 0xFF0000FF, "[ERROR]: The password is invalid. Valid characters are: A-Z, a-z, 0-9" );
	            ShowPlayerDialog( playerid, 1, DIALOG_STYLE_PASSWORD, "{FFFF00}Register", "Type in a password below to register an account.", "Register", "Leave" );
	            return 1;
	        }
	        if(strlen(inputtext) < 3 || strlen(inputtext) > 24 )
	        {
	            SendClientMessage(playerid, 0xFF0000FF, "[ERROR]: The password is invalid. Its lenght should be 3-24 characters");
	            ShowPlayerDialog(playerid, 1, DIALOG_STYLE_PASSWORD, "{FFFF00}Register", "Type in a password below to register an account.", "Register", "Leave");
	            return 1;
	        }
	        
	        new name[24];
	        new Query[ 208 ];
	        new novac = (random(500-300)-300);
	        
	        GetPlayerName(playerid, name, 24);
	        WP_Hash(Korisnik[playerid][Sifra], 129, inputtext);
	        
	        format(Query, sizeof(Query), "INSERT INTO KORISNICI (Ime, Sifra, Admin, Novac) VALUES ('%s', '%s', '%d', '%d')", DB_Escape(name), DB_Escape(Korisnik[playerid][Sifra]), 1340, novac);
	        db_query(Database, Query);

	        logiran[playerid] = true;
	        Korisnik[playerid][Novac] = novac;
	        Korisnik[playerid][Admin] = 1340;
	        
	        SendClientMessage(playerid, 0x00FF00FF, "[SERVER]: You have just registered to our server! You have been automatically logged in!");
	    }
	    else Kick(playerid);
	}
	
	if(dialogid == 2)
	{
	    if(response)
	    {
     		new buf[129];
	        WP_Hash(buf, 129, inputtext);
	        
	        if(!strcmp(buf, Korisnik[playerid][Sifra], false))
	        {
	            new Query[75],
	                name[24],
	                DBResult: Result;
	                
				GetPlayerName(playerid, name, 24);
	                
	            format(Query, sizeof(Query), "SELECT * FROM KORISNICI WHERE Ime = '%s' LIMIT 0, 1", DB_Escape(name));
	            Result = db_query(Database, Query);
	            if(db_num_rows(Result))
	            {
	                db_get_field_assoc(Result, "Admin", Query, 3);
	                Korisnik[playerid][Admin] = strval(Query);
	                
	                db_get_field_assoc(Result, "Novac", Query, 3);
	                Korisnik[playerid][Novac] = strval(Query);
	                GivePlayerMoney(playerid, strval(Query));

	                logiran[playerid] = true;
	                SendClientMessage( playerid, 0x00FF00FF, "[SERVER]: You have successfully logged in to your account!" );
	            }
	            db_free_result(Result);
	        }
	        else
	        {
	            SendClientMessage(playerid, 0xFF0000FF, "[ERROR]: Incorrect password");
	            ShowPlayerDialog(playerid, 2, DIALOG_STYLE_PASSWORD, "{FFFF00}Login", "Type in your password below to log in.", "Login", "Leave");
	        }
	    }
	    else Kick(playerid);
	}
	return 1;
}

//-----------------------------------------------------

stock DB_Escape(text[ ])
{
    new ret[80 * 2],
        ch,
        i,
        j;
        
    while((ch = text[i++]) && j < sizeof(ret))
    {
        if(ch == '\'')
        {
            if(j < sizeof(ret) - 2)
            {
                ret[j++] = '\'';
                ret[j++] = '\'';
            }
        }
        else if(j < sizeof(ret))
        {
            ret[j++] = ch;
        }
        else
        {
            j++;
        }
    }
    ret[sizeof(ret) - 1] = '\0';
    return ret;
}

stock IsValidPassword(const password[])
{
    for(new i = 0; password[i] != EOS; ++i)
    {
        switch(password[i])
        {
            case '0'..'9', 'A'..'Z', 'a'..'z': continue;
            default: return 0;
        }
    }
    return 1;
}
Size of my .db file is 3 kB but when I open .db in softwere for db files I don't have a table :/
vannesenn is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sqlite BUD Ryan_Obeles Scripting Help 0 21/04/2013 12:22 PM
Sqlite BUD Ryan_Obeles Scripting Help 4 20/04/2013 09:15 AM
strings in custom funcs. EV007 Scripting Help 1 04/03/2013 11:56 AM
asin, acos, atan, atan2 funcs? xomka Help Archive 1 28/10/2009 08:35 AM


All times are GMT. The time now is 04:45 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.