3

I need to save user-entered data like name and age to a text file on Android, in order that I can then save it to MySQL. How can I do this? Any help please thank you.

Richard H
  • 38,037
  • 37
  • 111
  • 138
roro
  • 63
  • 1
  • 1
  • 5
  • Provide some context, please. Do you need to store the data in text file or in MySQL? Or you're asking which is better? What do you want to do to that data (name, age, etc.)? – Seva Alekseyev Apr 16 '11 at 18:52
  • @roro - I have edited your question to make it better understandable. I hope I have translated it correctly, I wasn't sure what you meant by your reference to MySQL. – Richard H Apr 16 '11 at 18:53
  • i need to send data from android to python then save it to MySQL because i have a connection between MySQL and python !!! so i think about store my data in txt file then read it from python and then insert it to MySQL !! did u get me ?? – roro Apr 16 '11 at 18:56
  • Where is the Python code located? Is it on the Android phone? Or is it, by any chance, on the Web server? – Seva Alekseyev Apr 16 '11 at 19:01
  • python is on my pc code normal programming code !! – roro Apr 16 '11 at 19:03
  • @Roro: As a hint: If you want to add more information to a question (or answer) you can just edit the question and add the information. No need to put that into comments. – Heiko Rupp Apr 16 '11 at 19:41

4 Answers4

3

Filesystems of Android phone/emulator and of your computer are separate. Even if it's the emulator, there's no directory that both Python code and Android Java can read.

EDIT based on comment:

So when the game goes live, are you still going to use your (or player's) PC as a server? What you're facing is a very common app design; the Python code should reside on a Web service, you should use HTTP on Android to communicate to it, and forget about text files.

Read up on Android's HttpClient class, and on setting up Python on a Web server.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
2

Use FileWriter: http://developer.android.com/reference/java/io/FileWriter.html

See example like this one: http://blog.mynotiz.de/programmieren/java-text-in-eine-datei-schreiben-450/

Elepferd
  • 156
  • 4
  • It will be created automatically on the filesystem of your phone. Maybe you give it a special name like "FindME.txt" and then search for it, and you will get the location of it ;). – Elepferd Apr 16 '11 at 19:10
  • what if i'm on my pc ?!! where it will be saved ?? – roro Apr 16 '11 at 19:11
  • See Seva Alekseyev' post, he is right. You have to rethink your design. – Elepferd Apr 16 '11 at 19:39
0

Presumably he wants to how to load/save the data to the phone's SD card:

Android how to use Environment.getExternalStorageDirectory()

With that you can read the text file that is placed on the SD card by the user. Python and MySQL don't run on android (not on a stock phone anywyay) so if you want to store the data from the text file into Android's built in database, you need to use SQLite - which is provided by the framework:

http://developer.android.com/guide/topics/data/data-storage.html (SQL info is at the bottom)

A good example of how to use the DB is here:

http://www.screaming-penguin.com/node/7742

Your quesiton wasn't really that specific -- but if you need to send the data from the phone to an external database (ie on some server) then it's a whole different ball game. In that case, it is probably easiest to use some kind of URL based call on the server. (ie. REST)

-- Dan

Community
  • 1
  • 1
debracey
  • 6,517
  • 1
  • 30
  • 56
  • @ dan !! i'm making a game android is my client and python is my server so i need to enter some data for my game example player name !! so this data need to be store in the server database which is mysql this why i think about text file !! so did u get me ?? – roro Apr 16 '11 at 19:32
  • You'd probably want to present some UI where the user can enter the basic info, and then send it off to your server through a URL call (I suggested REST.) A little background on rest is avilable here: http://www.ibm.com/developerworks/webservices/library/ws-restful/ – debracey Apr 16 '11 at 19:47
  • ops !! UI it can simply done edit text and buttons but how to send it ogg to my server please make it some how clear !! – roro Apr 16 '11 at 19:55
0

I would add some sort of "web-interface" in front of the MySQL database that takes some content e.g. encoded in XML or JSON. On the Android side, after the user has entered the data, open a HttpConnection to the server and transmit that data.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119