Query SQL database

Easy Save 2 has been replaced by Easy Save 3, so is no longer supported.
Locked
Bellesgames
Posts: 1
Joined: Fri Mar 20, 2015 7:22 pm

Query SQL database

Post by Bellesgames »

Hello All!
I am currently using Unity3d, Playmaker, and EasySave2.
I would like to ask the community here if they have any solutions to querying information on a SQLite database. Is there a way to save/query information directly to SQL instead of using a key? maybe like a raw save/get?
I would like to be able to save each player's information in one entry. Is there such thing as having multiple entries per key in easy save 2?
It would also be great to have a score table / leader board. any solutions to that?
any input or thought is greatly appreciated!
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Query SQL database

Post by Joel »

Hi there,

I'll post what I just emailed to you incase it's useful to anyone else.

Also just for clarification, there is an UploadRaw method which allows you to upload a UTF8 string to the database. Note that if uploading a raw string, you should change the data column of MySQL table to a VARCHAR if you want to be able to view the string.
Easy Save’s MySQL functionality is only intended as a data store, so if you wanted to sort and return data you would need to upload each value as a string using ES2Web.UploadRaw and then modify the SQL statements in ES2’s PHP file so that they sort the data.

Due to how Unity’s own web functionality works, Easy Save only supports uploading of one tag at a time. However, you can download multiple tags as a single file by saving each piece data with the same webfilename, and then when downloading, only specify the webfilename instead of specifying a webfilename and a tag. Then you can load individual tags from the data using the ES2Web.Load methods.
All the best,
Joel
doppelmonster
Posts: 13
Joined: Thu Jan 08, 2015 2:34 pm

Re: Query SQL database

Post by doppelmonster »

Hi,
i have a question in the same direction: I would like to make a call to the SQL database and retrieve a list (names of the *.bytes) of the stored entries. Is this possible?

background:
I am storing level-packs in the SQL database. Each level-pack is one single .bytes file. I would like to create an automatically updating overview of all the level-packs currently stored in the database.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Query SQL database

Post by Joel »

Hi there,

You can use ES2Web.GetFilenames to get an array containing the filenames stored on the database.

All the best,
Joel
doppelmonster
Posts: 13
Joined: Thu Jan 08, 2015 2:34 pm

Re: Query SQL database

Post by doppelmonster »

Thanks for the hint!
Looks like exactly what i need :-)
doppelmonster
Posts: 13
Joined: Thu Jan 08, 2015 2:34 pm

Re: Query SQL database

Post by doppelmonster »

Hi,
i am trying to convert "ES2Web.GetFilenames" into a playmaker action based on Jean Fabres Easy Save ArrayMaker actions.

But i get the following error message:

"Unity has reported an upload error: 404 Not Found"


Its weird I am using the same php credentials all over the game and its working perfectly. So I believe i have done something wrong in code.

I successfully reach the debug.log in line 90, but then it finishes because of the web error in Line 100.

Any suggestions?
//	(c) Jean Fabre, 2011-2013 All rights reserved.
//	http://www.fabrejean.net

// INSTRUCTIONS
// Drop a PlayMakerArrayList script onto a GameObject, and define a unique name for reference if several PlayMakerArrayList coexists on that GameObject.
// In this Action interface, link that GameObject in "arrayListObject" and input the reference name if defined. 
// Note: You can directly reference that GameObject or store it in an Fsm variable or global Fsm variable

using System.IO;
using System.Xml.Serialization;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
	[ActionCategory("Easy Save 2")]
	[Tooltip("Loads a PlayMaker Array List Proxy component of data entry namesFrom MySQL Server via ES2.php file. See moodkie.com/easysave/WebSetup.php for how to set up MySQL.")]
	public class ArrayListDownloadDatabaseEntryList : ArrayListActions
	{
		
		[ActionSection("Set up")]
		
		[RequiredField]
		[Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")]
		[CheckForComponent(typeof(PlayMakerArrayListProxy))]
		public FsmOwnerDefault gameObject;
		
		[Tooltip("Author defined Reference of the PlayMaker ArrayList Proxy component (necessary if several component coexists on the same GameObject)")]
		[UIHint(UIHint.FsmString)]
		public FsmString reference;
		
		
		[ActionSection("Upload Set up")]
		
		[RequiredField]
		[Tooltip("The URL to our ES2.PHP file. See http://www.moodkie.com/easysave/WebSetup.php for more information on setting up ES2Web")]
		public FsmString urlToPHPFile = "http://www.mysite.com/ES2.php";
		[RequiredField]
		[Tooltip("The username that you have specified in your ES2.php file.")]
		public FsmString username = "ES2";
		[RequiredField]
		[Tooltip("The password that you have specified in your ES2.php file.")]
		public FsmString password = "00000";
		
		
		[ActionSection("Result")]
		[Tooltip("The Event to send if Download succeeded.")]
		public FsmEvent isDownloaded;
		[Tooltip("The event to send if Download failed.")]
		public FsmEvent isError;
		[Tooltip("Where any errors thrown will be stored. Set this to a variable, or leave it blank.")]
		public FsmString errorMessage = "";
		[Tooltip("Where any error codes thrown will be stored. Set this to a variable, or leave it blank.")]
		public FsmString errorCode = "";
		
		private ES2Web web = null;
		
		private string _tag;
		
		public override void Reset()
		{
			gameObject = null;
			reference = null;
			
			urlToPHPFile = "http://www.mysite.com/ES2.php";
			web = null;
			errorMessage = "";
			errorCode = "";
		}
		
			
		
		public override void OnEnter()
		{
			if ( SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(gameObject),reference.Value) )
			{
				DownloadArrayList();
			}
		}
		
		private void DownloadArrayList()
		{
			if (! isProxyValid() ) 
				return;
			
			web = new ES2Web(urlToPHPFile+"&webpassword="+password.Value+"&webusername="+username.Value);
			
			this.Fsm.Owner.StartCoroutine(web.DownloadFilenames());
			Debug.Log("Downloading from" +urlToPHPFile.Value);
		}
		
		public override void OnUpdate()
		{
			if(web.isError)
			{
				errorMessage = web.error;
				errorCode = web.errorCode;
				Fsm.Event(isError);
				Finish();
			}
			else if(web.isDone)
			{
				Fsm.Event(isDownloaded);
				
	
	
				
				string[] source = web.GetFilenames();
				
				//List<string> source = web.GetFilenames();
				proxy.arrayList.Clear();
				
				foreach(string element in source){
					proxy.arrayList.Add(PlayMakerUtils.ParseValueFromString(element));
				}
					
				
				Finish();
			}
		}
		
	}
}
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Query SQL database

Post by Joel »

Hi there,

The only thing I can see which is wrong is that:

urlToPHPFile+"&webPassword="

Should be:

urlToPHPFile+"?webPassword="

However, if that doesn't solve your problem then you might want to get in touch with Jean, as all Arraymaker integration is done through him.

All the best,
Joel
doppelmonster
Posts: 13
Joined: Thu Jan 08, 2015 2:34 pm

Re: Query SQL database

Post by doppelmonster »

Thanks a lot Joel!
that was the problem indeed!

I had to fix some more things but now this action works as intended:

//	(c) Jean Fabre, 2011-2013 All rights reserved.
//	http://www.fabrejean.net

// INSTRUCTIONS
// Drop a PlayMakerArrayList script onto a GameObject, and define a unique name for reference if several PlayMakerArrayList coexists on that GameObject.
// In this Action interface, link that GameObject in "arrayListObject" and input the reference name if defined. 
// Note: You can directly reference that GameObject or store it in an Fsm variable or global Fsm variable

using System.IO;
using System.Xml.Serialization;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
	[ActionCategory("Easy Save 2")]
	[Tooltip("Loads all filenames the SQL database contains into a PlayMaker Array List Proxy component. http://docs.moodkie.com/easy-save-2/api ... filenames/. See moodkie.com/easysave/WebSetup.php for how to set up MySQL.")]
	public class ArrayListGetDatabaseEntries : ArrayListActions
	{
		
		[ActionSection("Set up")]
		
		[RequiredField]
		[Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")]
		[CheckForComponent(typeof(PlayMakerArrayListProxy))]
		public FsmOwnerDefault gameObject;
		
		[Tooltip("Author defined Reference of the PlayMaker ArrayList Proxy component (necessary if several component coexists on the same GameObject)")]
		[UIHint(UIHint.FsmString)]
		public FsmString reference;
		
		
		[ActionSection("Download Set up")]
		
		[RequiredField]
		[Tooltip("The URL to our ES2.PHP file. See http://www.moodkie.com/easysave/WebSetup.php for more information on setting up ES2Web")]
		public FsmString urlToPHPFile = "http://www.mysite.com/ES2.php";
		[RequiredField]
		[Tooltip("The username that you have specified in your ES2.php file.")]
		public FsmString username = "ES2";
		[RequiredField]
		[Tooltip("The password that you have specified in your ES2.php file.")]
		public FsmString password = "";
		
		
		[ActionSection("Result")]
		[Tooltip("The Event to send if Download succeeded.")]
		public FsmEvent isDownloaded;
		[Tooltip("The event to send if Download failed.")]
		public FsmEvent isError;
		[Tooltip("Where any errors thrown will be stored. Set this to a variable, or leave it blank.")]
		public FsmString errorMessage = "";
		[Tooltip("Where any error codes thrown will be stored. Set this to a variable, or leave it blank.")]
		public FsmString errorCode = "";
		
		private ES2Web web = null;
		
		private string _tag;
		
		public override void Reset()
		{
			gameObject = null;
			reference = null;
			username = "";
			password = "";
			urlToPHPFile = "http://www.mysite.com/ES2.php";
			web = null;
			errorMessage = "";
			errorCode = "";
		}
		
			
		
		public override void OnEnter()
		{
			if ( SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(gameObject),reference.Value) )
			{
				DownloadArrayList();
			}
		}
		
		private void DownloadArrayList()
		{
			if (! isProxyValid() ) 
				return;
			
			web = new ES2Web(urlToPHPFile+"?webpassword="+password.Value+"&webusername="+username.Value);
			
			this.Fsm.Owner.StartCoroutine(web.DownloadFilenames());
			Debug.Log("Downloading from" +urlToPHPFile.Value);
		}
		
		public override void OnUpdate()
		{
			if(web.isError)
			{
				errorMessage = web.error;
				errorCode = web.errorCode;
				Fsm.Event(isError);
				Finish();
			}
			else if(web.isDone)
			{
				Fsm.Event(isDownloaded);
				
	
	
				
				string[] source = web.GetFilenames();
				
				
				proxy.arrayList.Clear();
				
				foreach(string element in source){
					proxy.arrayList.Add(element);
					Debug.LogWarning (element);
				}
				
			
				
				Finish();
			}
		}
		
	}
}
Last edited by doppelmonster on Tue Apr 14, 2015 8:16 am, edited 1 time in total.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: Query SQL database

Post by Joel »

Good to hear that fixed the problem! Thanks for posting the code, it should come in useful for some of our users.

- Joel
doppelmonster
Posts: 13
Joined: Thu Jan 08, 2015 2:34 pm

Re: Query SQL database

Post by doppelmonster »

While i am at it. Here is an action listing the local files (e.g. persistent data path):
//	(c) Jean Fabre, 2011-2013 All rights reserved.
//	http://www.fabrejean.net

// INSTRUCTIONS
// Drop a PlayMakerArrayList script onto a GameObject, and define a unique name for reference if several PlayMakerArrayList coexists on that GameObject.
// In this Action interface, link that GameObject in "arrayListObject" and input the reference name if defined. 
// Note: You can directly reference that GameObject or store it in an Fsm variable or global Fsm variable

using System.IO;
using System.Xml.Serialization;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
	[ActionCategory("Easy Save 2")]
	[Tooltip("Returns the filenames of the files contained within the specified folder.Loads them in a PlayMaker Array List Proxy component. http://docs.moodkie.com/easy-save-2/api/es2-getfiles/.]
	public class ArrayListGetFileNames : ArrayListActions
	{
		
		[ActionSection("Set up")]
		
		[RequiredField]
		[Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")]
		[CheckForComponent(typeof(PlayMakerArrayListProxy))]
		public FsmOwnerDefault gameObject;
		
		[Tooltip("Author defined Reference of the PlayMaker ArrayList Proxy component (necessary if several component coexists on the same GameObject)")]
		[UIHint(UIHint.FsmString)]
		public FsmString reference;
		
		[ActionSection("Folder")]
	
		[RequiredField]
		[Tooltip("The path to our folder. leave blank if its in the persistent data path")]
		public FsmString folderPath = "";
		
		
		
		public override void Reset()
		{
			gameObject = null;
			reference = null;
			
			folderPath = "";
	
		}
		
			
		
		public override void OnEnter()
		{
			if ( SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(gameObject),reference.Value) )
			{
				DownloadArrayList();
			}
		}
		
		private void DownloadArrayList()
		{
			if (! isProxyValid() ) 
				return;
			
			string[] source = ES2.GetFiles(folderPath.Value);
			
			
			proxy.arrayList.Clear();
			
			foreach(string element in source)
			{
				proxy.arrayList.Add(element);
				Debug.LogWarning (element);
			}
			
			Finish();
		}
		
		
	}
}
Locked