- Home /
How do i save input data for later use on Android devices?
Hi people, i am pretty new to c# and programming overall. I need to create an Android App for an exhibition we´ll be attending soon. When the visitor got all his infos etc. i want to make him enter his name and E-mail and save it for later use (like sending a newsletter etc.). By now i made one version where i save it to our mySQL Sever and one where i save it to an external txt file via Streamwriter. Both works fine on my PC but as soon as i export it to an Android device, the button that saved the data AND chages the scene just does not do ANYTHING. It does not have to be mySQL or saving to txt. I just need to save the data in any way at all.
If it helps, please see the example of the code i have so far for MySQL:
private const string CONNECTION = "SERVER=ourIP;PORT=ourPort;UID=myName;PASSWORD=myPW;DATABASE=exhibition;";
public void AddSubscriber(){
MySqlConnection con = new MySqlConnection(CONNECTION);
Debug.Log("Connecting to database...", this);
try
{
con.Open();
}
catch(Exception _e)
{
Debug.LogError("Failed to connect to database(" + _e.GetType().Name + ")!\n" + _e.Message, this);
return;
}
Debug.Log("Connected!");
try
{
MySqlCommand cmd = new MySqlCommand("INSERT INTO teilnehmer (EMail, Name) VALUES('" + m_emailInput + "', '" + m_nameInput + "')", con);
cmd.Prepare();
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch(Exception _e)
{
Debug.LogError("Failed to write subscriber to database(" + _e.GetType().Name + ")!\n" + _e.Message, this);
return;
}
Debug.Log("Successfully wrote subscriber to database!", this);
con.Close();
con.Dispose();
}
and for Streamwriter:
public void Write(){
using (StreamWriter writer = new StreamWriter (@"Teilnehmer.txt", true)) {
writer.WriteLine (m_nameInput + ": " + m_emailInput);
writer.Flush ();
}
}
Any help is greatly appreciated since i am kinda running out of time ;)
Answer by Quaker_SDR · Sep 01, 2014 at 12:09 PM
You can use filestream, It will work lyk a charm.
FileStream fs=new FileStream(filepath,FileMode.OpenOrCreate,FileAccess.ReadWrite); fs.Write (System.Text.Encoding.UTF8.GetBytes(datatosave), 0, System.Text.Encoding.UTF8.GetByteCount(datatosave));
Thanks for the answer. Still doesn´t work any better than Streamwriter. I figured maybe i am having trouble importing it to my device since i have never done that before. I am simply building the .apk, drag it into any random folder on the device (in this case "Android") and install/run it from there. Is that even right?
Thanks, i will try to get familiar with PlayerPrefs and come back to you with how it went ;)
Answer by Cpt-Chaos · Sep 02, 2014 at 11:07 AM
Thanks guys. Took me a day to get to know the Playerprefs and make it work but now it does. If anyone bumps into that problem:
void PPref() {
participants += (m_nameInput + ": " + m_emailInput + "\n");
PlayerPrefs.SetString ("Teilnehmer", participants);
}
that will save the value participants in PlayerPrefs "Teilnehmer" and store the values (in my case email and name input) in participants. Then call it in the start to retrieve the value so it is still there when you start up the app again.
private void Start(){
participants = PlayerPrefs.GetString ("Teilnehmer");
}
Hope i am not talking rubbish right now but for me this worked :P Thanks a whole lot guys ;)
That's exactly how to use it ;) $$anonymous$$uch easier then messing around with FileStreams or other stuff
Indeed. It worked marvellously :) Thanks again. Was a very important project for me.
Your answer
![](https://koobas.hobune.stream/wayback/20220613162026im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Downloading data from MySql database not working on Android. 1 Answer
Distribute terrain in zones 3 Answers
PHP Android WWW Unknown Error 1 Answer
Live chat using MySQL? 0 Answers