Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 09-25-2011, 03:12 AM
Ohk4y Ohk4y is offline
Registered User
Ohk4y's Avatar
Join Date: Jun 2011
Posts: 43
Ohk4y is an unknown quantity at this point
Send a message via AIM to Ohk4y
SQL Help

Hello there!

I did some research and finally figured out how to create a table with SQL Explorer, as well as make some columns and rows. I have a table that has Accounts as the name that has some columns that display:
PHP Code:
rowid Username Password EZ Level X Y Exp 
If I go to the Execute SQL tab and type in:
PHP Code:
SELECT Username,Password,EZ,Level,X,Y,Exp FROM Accounts 
and it outputs with some information that I manually typed in:
PHP Code:
3 rows returned
|Exp|EZ |Level         |Password|Username|||
------------------------
|
56 |975|forest_test.nw|Test1   |Ohk4y   |30|30|
|
80 |200|forest_test.nw|Test1   |Test    |28|31|
|
0  |0  |0             |0       |Player  ||
My question here is; How can I request information from the table with a Weapon Script? Also, How could I write information, as well as add rows to it with a Script.

Thanks,
Ohk4y
__________________
Reply With Quote
  #2  
Old 09-25-2011, 03:31 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
My preferred method is to create a DB-NPC called SQL.

PHP Code:
/**
    SQL Query Interface
*/

public function executeSQL(queryresultdoecho) {
  
temp.init timevar2;
  
temp.results requestsql(queryresult);
  if (
result) {
    
// Check Results
    
if (results.error != "") {
      echo(
format("SQL Error: %s"results.error));
      
savelog2("sqlerrors.txt"results.error NL query);
      return 
NULL;
    }
    else if (
results.rows.size() > || query.starts("SELECT")) {
      if (
doecho) echo(format("SQL returned %s rows."results.rows.size()));
    }
    else if (
results.affectedrows.size() > 0) {
      if (
doecho) echo(format("SQL affected %s rows."results.affectedrows.size()));
    }
    else if (
results.completed) {
      if (
doecho) echo(format("SQL %s Query completed successfully."query.tokenize()[0].upper()));
    }
    
temp.taken timevar2 temp.init;
    
// Uncomment to log queries (for checking performance, etc.)
    //savelog2("sqlqueries.txt", query NL "Took " @ temp.taken @ " seconds");
    
return results;
  }

Then in my server-side code I can do the following:

PHP Code:
function onCreated() {
  
temp.query "SELECT Username,Password,EZ,Level,X,Y,Exp FROM Accounts";
  
temp.result SQL.executeSQL(temp.querytrue);
  if (
temp.result.rows.size() > 0) {
    for (
temp.rowtemp.result.rows) {
      echo(
temp.row.Username);
    }
  }

I threw this together quickly thought so I can't guarantee it'll work for your situation.

There's some other links here:
http://public.zodiacdev.com/index.ph...ntro_to_SQLite
__________________
Quote:
Reply With Quote
  #3  
Old 09-25-2011, 04:44 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Personally I'd do something like this instead

PHP Code:
/** 
    SQL Query Interface 
*/ 

public function executeSQL(dbqueryresultdoecho) { 
  
temp.init timevar2
  
temp.results requestsql2(dbqueryresult); 
  if (
result) { 
    
// Check Results 
    
if (results.error != "") { 
      echo(
format("SQL Error: %s"results.error)); 
      
savelog2("sqlerrors.txt"results.error NL query); 
      return 
NULL
    } 
    else if (
results.rows.size() > || query.starts("SELECT")) { 
      if (
doecho) echo(format("SQL returned %s rows."results.rows.size())); 
    } 
    else if (
results.affectedrows.size() > 0) { 
      if (
doecho) echo(format("SQL affected %s rows."results.affectedrows.size())); 
    } 
    else if (
results.completed) { 
      if (
doecho) echo(format("SQL %s Query completed successfully."query.tokenize()[0].upper())); 
    } 
    
temp.taken timevar2 temp.init
    
// Uncomment to log queries (for checking performance, etc.) 
    //savelog2("sqlqueries.txt", query NL "Took " @ temp.taken @ " seconds"); 
    
return results
  } 

Small change, but it lets you use multiple databases.

Example:

PHP Code:
function onCreated() { 
  
temp.query "SELECT Username,Password,EZ,Level,X,Y,Exp FROM Accounts"
  
temp.result SQL.executeSQL("default"temp.querytrue); 
  if (
temp.result.rows.size() > 0) { 
    for (
temp.rowtemp.result.rows) { 
      echo(
temp.row.Username); 
    } 
  } 

__________________
Reply With Quote
  #4  
Old 09-27-2011, 02:03 AM
Ohk4y Ohk4y is offline
Registered User
Ohk4y's Avatar
Join Date: Jun 2011
Posts: 43
Ohk4y is an unknown quantity at this point
Send a message via AIM to Ohk4y
I added this as -Ohk4y/Query
PHP Code:
/** 
    SQL Query Interface 
*/ 

public function executeSQL(dbqueryresultdoecho) { 
  
temp.init timevar2
  
temp.results requestsql2(dbqueryresult); 
  if (
result) { 
    
// Check Results 
    
if (results.error != "") { 
      echo(
format("SQL Error: %s"results.error)); 
      
savelog2("sqlerrors.txt"results.error NL query); 
      return 
NULL
    } 
    else if (
results.rows.size() > || query.starts("SELECT")) { 
      if (
doecho) echo(format("SQL returned %s rows."results.rows.size())); 
    } 
    else if (
results.affectedrows.size() > 0) { 
      if (
doecho) echo(format("SQL affected %s rows."results.affectedrows.size())); 
    } 
    else if (
results.completed) { 
      if (
doecho) echo(format("SQL %s Query completed successfully."query.tokenize()[0].upper())); 
    } 
    
temp.taken timevar2 temp.init
    
// Uncomment to log queries (for checking performance, etc.) 
    //savelog2("sqlqueries.txt", query NL "Took " @ temp.taken @ " seconds"); 
    
return results
  } 

And added this as -Ohk4y/Test3
PHP Code:
function onCreated() { 
  
temp.query "SELECT Username,Password,EZ,Level,X,Y,Exp FROM Accounts"
  
temp.result SQL.executeSQL("default"temp.querytrue); 
  if (
temp.result.rows.size() > 0) { 
    for (
temp.rowtemp.result.rows) { 
      echo(
temp.row.Username);
    } 
  } 

And recieved no echo in rc 0.0
__________________
Reply With Quote
  #5  
Old 09-27-2011, 02:36 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Re-read my post and you'll see I put executeSQL in a DB-NPC named SQL.

So you either need to update the Test script or create the DB-NPC as suggested.
__________________
Quote:
Reply With Quote
  #6  
Old 09-27-2011, 02:38 AM
Ohk4y Ohk4y is offline
Registered User
Ohk4y's Avatar
Join Date: Jun 2011
Posts: 43
Ohk4y is an unknown quantity at this point
Send a message via AIM to Ohk4y
Okay thank you. I put the Query in a dbnpc named SQL and the other script in an accounts weapon, it echos everything fine, though, I can't quite figure out how to write data to the table.
__________________
Reply With Quote
  #7  
Old 09-27-2011, 02:44 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Ohk4y View Post
Okay thank you. I put the Query in a dbnpc named SQL and the other script in an accounts weapon, it echos everything fine, though, I can't quite figure out how to write data to the table.
INSERT and UPDATE. If you google those along with SQLite you should see plenty of resources.
__________________
Reply With Quote
  #8  
Old 09-27-2011, 02:48 AM
Ohk4y Ohk4y is offline
Registered User
Ohk4y's Avatar
Join Date: Jun 2011
Posts: 43
Ohk4y is an unknown quantity at this point
Send a message via AIM to Ohk4y
Thanks.
__________________
Reply With Quote
Reply


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

Forum Jump


All times are GMT +2. The time now is 02:16 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.