make some transaction with a mysql database without runing a mysql server.

Hi all,
is it possible to export a mysql database with the game and make it work without runing a mysql server??

1 Answer

1

SQLite. Just download the .NET assembly and put it into your assets folder.

SQLite work directly with database-files, but allows you to use SQL to read / write data.

edit(2012.02.26)

Here's a small example how to use SQLite:

// C#
// Of course you need to include the namespace at the top
using System.Data.SQLite;

// open or "create and open" the database-file and open the connection
SQLiteConnection con = new SQLiteConnection("Data Source=QLTest.sqlite");
con.Open();

// Create a SQLiteCommand and set the desired SQL statement
SQLiteCommand C = con.CreateCommand();
C.CommandText = "CREATE TABLE test (id INT PRIMARY KEY, name TEXT)";    
// Execute the statement without a return value
C.ExecuteNonQuery();

// ExecuteScalar can be used when you expect only one single result value.
// NOTE: it has to be casted into the correct type.
int queryID = 1;
C = con.CreateCommand();
C.CommandText = "SELECT name FROM test WHERE id='" + queryID + "' LIMIT 1";
string name = (string)C.ExecuteScalar();

// For every other "normal" query you would use .ExecuteReader() which returns a reader object
C = con.CreateCommand();
C.CommandText = "SELECT * FROM test";
SQLiteDataReader DR = C.ExecuteReader();
while (DR.Read())
{
    string row = "";
    for (int i = 0; i < DR.FieldCount; i++)
    {
        if (i != 0)
            row += ", ";
        row += DR.GetString(i);
    }
    Debug.Log(row);
}

// when done, close the connection.
con.Close();

Also keep in mind that the SQLiteDataReader supports dedicated Get-functions for various types:

public override DateTime GetDateTime(int i);
public override decimal GetDecimal(int i);
public override double GetDouble(int i);
public override float GetFloat(int i);
public override Guid GetGuid(int i);
public override short GetInt16(int i);
public override int GetInt32(int i);
public override long GetInt64(int i);
public override string GetString(int i);
public override object GetValue(int i);

And there are some special functions to retrive multiple fields as array, to check a field for "null" or to determine the field type:

public override int GetValues(object[] values);
public override bool IsDBNull(int i);
public override Type GetFieldType(int i);

You will need to import them into your scripts I beleive. In C# you would do using System.Data.SQLlite; I think... As for where you would need to put them, anyone care to wade in.

Don't copy the dlls multiple times in your project! Just drop it somewhere in your assets folder.

Just in case you didn't noticed my edit (<a href="http://answers.unity3d.com/questions/220301/why-do-edits-no-longer-appear-on-this-site.html">which got visible after some time</a>). I've added some examples on how to use SQLite.

it´s using Mono.Data.Sqlite;