2

I'm playing around with the Play Framework 2.0 and what I'm doing is a simple web app that does following : Ask user to input a message which gets stored to a database and displayed on the screen (it's a test app) I'm using Ebean whith H2 (I followed the explanations on the official website to set this up) and at the start of the app I create a table (via 1.sql)

create table message (
  id                        bigint not null,
  created_on                timestamp,
  message                   varchar(255),
  image_url                 varchar(255),
  constraint pk_message primary key (id))
;

It pretty much does what I want it to do but now here's my issue : I have a Python script that grabs lots of "messages" from the internet and the objective is now to add those grabbed messages into the database. But I don't know how to do it, I don't know how to access the database from "outside" of the play framework. Is there a way of updating the database via Python ? If yes, any hint on how to do it ?

tcooc
  • 20,629
  • 3
  • 39
  • 57
Tony J.
  • 81
  • 1
  • 10
  • Check this post out [http://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python][1] [1]: http://stackoverflow.com/questions/372885/how-do-i-connect-to-a-mysql-database-in-python – Sam Nov 26 '13 at 21:46
  • Thanks for your answer, but I've already looked that up and my question is about how to setup the mysql connection (database name, password etc.) – Tony J. Nov 26 '13 at 21:57

2 Answers2

0

You can easily create a REST endpoint which takes JSON and stuffs it into the DB. The Python app can then submit to that endpoint. Another option would be to use Jython and interact with Ebean like that, I think this is harder.

You can also just look at the table layout that the ORM (Ebean?) generates and insert directly into that using INSERT DATA in SQL. This will be most efficient, but it is dependent on your table layout not changing. The other solutions are typed, so I consider them more safe.

Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
  • Thanks for your answer, any hint on how to do the REST method ? Any link explaining that ? I'm still learning so any ressource on that would be great. – Tony J. Nov 26 '13 at 21:59
  • @TonyJ. see http://www.playframework.com/documentation/2.2.x/JavaBodyParsers . It actually does not have to be REST per-se, but that would be elegant. Just define a method like that in your controller, hook it up with a POST route and have the method insert the data into the database. – Janus Troelsen Nov 26 '13 at 23:39
0

This is a simple instruction on how you can connect to mysql with python http://zetcode.com/db/mysqlpython/

Hope it helps

Sam
  • 2,761
  • 3
  • 19
  • 30
  • Thanks for the answer Sam, my issue is how to find the database name, the user, password and everything to create the database connection. Play Framework somehow handles that alone when using Ebean (ORM) and those configurations for the database : db.default.driver=org.h2.Driver db.default.url="jdbc:h2:mem:play" – Tony J. Nov 26 '13 at 22:05