Kind Reader,
It has taken me far too long to produce what is a second and even more brief overview of the functionality of GAE; particularly in this case the Google Cloud SQL and connection to it. Though I hope it is useful, I apologise for it not being anywhere near the quality of my previous tutorial, and I shall no doubt revise it as and when I have time.
_________________________________________________________________
Contents
1.1: Requirements/Resources
1.2: Setting up the Database
– 1.2.1: Setting up the Cloud SQL Access Point
– 1.2.2: Creating the Database
– 1.2.3: Getting Data from the Cloud
1.3: Final Notes
– 1.5.1: Program Overview
– 1.5.3: Good luck and Have Fun!
_________________________________________________________________
1.1: Requirements/Resources
(If you have followed the previous tutorial you’ll know this bit.)This app is being developed in Google App Engine 1.8.5, available here. It requires Python to be installed on your computer, at this time version 2.7 seems to be the most stable with the app engine so that’s what I’m using. If you don’t have it, it’s around about here.
1.2: Setting up the Database
1.2.1: Setting up the Cloud SQL Access Point
First, go to https://cloud.google.com/console and log in, this is the Google Cloud console and from there you can set up an SQL project to connect your app to.
Click the red button near the top that says Create Project and enter a project name and ID to create your app’s very own space on the Google Cloud.
On the left hand side there’s a menu, the third option from the bottom is called Cloud SQL, click it to set up the SQL Instance – basically the connection settings for using SQL from your app.
Now click the red button at the top labelled New Instance.
Here you create the instance – basically rent space on the Google Cloud – useful, but sadly not free. Fill in each of the boxes; enter an Instance ID, Region (this is the physical location of the Google servers you want to use, not your location), then select billing options and add any authorization specifics.
At the bottom make sure that your Google app engine project is listed under Authorized App Engine Applications.
1.2.2: Creating the Database
Now go into the menu option on the left called Cloud Datastore and click the red button at the top called Create Entity. Give it a ‘Kind’, which basically seems to be the database name(?). Then you have a drop down called Key, and you can either have this as ID, in which case its name will be a randomly assigned number, or you can change that to Name, and give the Key a name yourself. Keys, as far as I can tell, are much like tables, for the purposes of my database I will make a table called Players, who will have a name and a score.
You add the fields of the table, in my case name and score, at the bottom of the form where there is a blank text box followed by ‘a string’ or whatever, then indexed or not by blank. Not the best way to lay it out, perhaps.
Anyway, the first blank box to the left of ‘a string’ is the name of the field; my first one is called name. You can then change the ‘a string’ drop down to whatever is the applicable variable type – in my case I keep it as ‘a string’ since it is just text. If you want to index your data you can. To add more fields, simply press the plus symbol at the end of each field.
Sometimes there seems to be a problem saving fields, if this happens then try changing the variable type. Once you’re done, just press the blue Create Entity button at the bottom of the form.
1.2.3: Getting Data from the Cloud
You can connect to your database using PDO, MySQL, and MySQLi; being most comfortable with MySQL, I’m going to quickly explain how to implement the connection in your app in that language, but Google explain all of the options fairly well here.
Go into your PHP app file, if you’re following on from my last tutorial then in the main.php that we created earlier, somewhere in <?php ?> tags add SQL code similar to mine that selects whatever SQL Instance/Database fields you’ve set.
$db_connect = mysql_connect(":/cloudsql/mikesbrandnewapp:sqlinstance", "<google username>", "<google password>");
if (!$db_connect) {
die('Ah shoot, this happened: ' . mysql_error());
}
Now you’re connected to the database, you can query and search for your fields or upload data. I should note that I have not played around much with this code, and for further Cloud SQL help this tutorial looks very helpful. Since Google Cloud PHP/SQL isn’t particularly well documented at the moment, it may not be the best option for major projects; I plan to do a Google Datastore tutorial soon.
1.3: Final Notes
1.3.1: Program Overview
While GAE is a very sensible option for app development, especially apps that interact with the web, it still has a way to go as far as documentation is concerned, and that is the major weakness of it since if you have a problem there isn’t a great deal of expert knowledge widely available. That’s part of the reason why this tutorial has taken so long to put together and why it is much briefer than I’d like. (Possibly Appcelerator, Android Studio, and PhoneGap tutorials to come – I make no promises though with a dissertation to finish! Also, Unity app development is a fairly nice experience too.)
Good luck and have fun.