Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cpt-Chaos · Sep 01, 2014 at 11:49 AM · c#androidsave datamysql

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 ;)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image renezuidhof · Sep 01, 2014 at 12:00 PM 0
Share

You can save stuff for later use with PlayerPrefs

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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));

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cpt-Chaos · Sep 01, 2014 at 12:42 PM 0
Share

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?

avatar image renezuidhof · Sep 01, 2014 at 01:11 PM 0
Share

Why not use the PlayerPrefs?

avatar image Cpt-Chaos · Sep 01, 2014 at 01:37 PM 0
Share

Thanks, i will try to get familiar with PlayerPrefs and come back to you with how it went ;)

avatar image
0

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 ;)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image renezuidhof · Sep 02, 2014 at 11:42 AM 0
Share

That's exactly how to use it ;) $$anonymous$$uch easier then messing around with FileStreams or other stuff

avatar image Cpt-Chaos · Sep 03, 2014 at 02:50 AM 0
Share

Indeed. It worked marvellously :) Thanks again. Was a very important project for me.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges