Creation/Dev/Gscript/Using SQLite: Difference between revisions

From Graal Bible
No edit summary
Line 60: Line 60:
Data is stored into tables. Like any table, these tables have '''columns''' and '''rows'''. The simplest way to think of the setup is that a row is one piece of data, and columns are different parts of that data. An example of a table might look like this:
Data is stored into tables. Like any table, these tables have '''columns''' and '''rows'''. The simplest way to think of the setup is that a row is one piece of data, and columns are different parts of that data. An example of a table might look like this:


{| border="1"
<center>
{| border="1" cellpadding="5"
|-
|-
!  account
!  account
Line 74: Line 75:
|  3
|  3
|}
|}
</center>
When you request data from the database, you receive a list of "rows", based on what you ask for. The data in these rows is stored in columns.


== How do I tell SQLite what to do? ==
== How do I tell SQLite what to do? ==

Revision as of 22:32, 5 December 2009

Preamble

Target Audience

This guide will not teach you how to script. It is directed toward already-competent scripters who wish to incorporate SQLite into their scripts.

Required Skills and Knowledge

  • Basic understanding of GS2
  • Basic understanding of storing data in traditional ways (flatfile)
  • Basic understanding of the way tables work (for example, Microsoft Excel)

Introduction

What is SQLite?

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

In English, it is a script that stores data into tables, the way any SQL language works (e.g. MySQL).

Why use SQLite?

There are advantages and disadvantages to using SQLite for storing data. Like any data storage method, there are times where it is practical to use it, and times when it isn't so practical.

The main advantage to using SQLite is that all of your data is stored in a central location. This allows you to compare the data quickly and easily, without a lot of extra code.

This isn't going to make a lot of sense until later, but let's pretend I have a table with two columns: "account" and "hours". If I wanted to figure out which players have more than 5 hours and less then 10, I can simply run a query like this:

SELECT * FROM players WHERE hours > 5

Using traditional storage methods, such as flatfile, the file might look something like this:

(npcserver)=6
Stefan=10
unixmad=3

In order to figure out which of these players has more than 5 hours, I would need to do something to this effect:

temp.file.loadVars("data/file.txt");
temp.pls = {};


for (temp.pl : file.getDynamicVarNames()) {
  if (file.(@ pl) > 5) { // has more than 5 hours
    pls.add({pl, file.(@ pl)});
  }
}

The equivalent code for a properly organized SQL table might be:

temp.pls = req("SELECT * FROM players WHERE hours > 5");


In essence, SQLite is a fast, easy, and organized way to store your data.

Preparation

Getting started with SQLite is extremely easy, but for the purpose of this tutorial, I'm going to point you toward a tool that will help you greatly. Before that, though, you will need access to a server where you can learn. If you don't already have access to some server, you can sign up for the Testbed Server

If you aren't using the Testbed Server, you will need to upload a tool to help with this tutorial

  • Go to this forum post and download the text of the "SQL Explorer".
  • Create a new weapon, and name it "Tools/SQL Explorer" (or any other name). Paste the text of the SQL explorer in here.
    • Before you save the weapon, change line 29, to "false". This will be discussed later on.
  • Add yourself the weapon

If you are using the Testbed Server, just add yourself the weapon "Tools/SQL Explorer".

Getting Started

How is data stored?

Data is stored into tables. Like any table, these tables have columns and rows. The simplest way to think of the setup is that a row is one piece of data, and columns are different parts of that data. An example of a table might look like this:

account hours
(npcserver) 6
Stefan 10
unixmad 3

When you request data from the database, you receive a list of "rows", based on what you ask for. The data in these rows is stored in columns.

How do I tell SQLite what to do?

To tell SQLite what to do, you pass commands as strings, rather than using a set of functions. These strings are called "queries". The query contains a set of functions that tell SQLite what to do. Queries start with the base command, and are followed by command-specific syntax. As you use SQLite, you'll become more accustomed to how it works.

Note: There are security issues associated with user input in queries, which are discussed later in the tutorial. For this reason, you should not start using SQLite for actual content until you understand the risks and how to avoid them.