Skip to content

Commit e553342

Browse files
author
Jon Wayne Parrott
committed
1 parent 000ff37 commit e553342

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

appengine/cloudsql/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This is an example program showing how to use the native MySQL connections from Google App Engine to Google Cloud SQL.
2+
3+
## Deploying
4+
5+
1. Edit the `unix_socket` in `main.py` to point to a Cloud SQL instance.
6+
7+
2. Upload the app: `appcfg.py update .`.

appengine/cloudsql/app.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
handlers:
6+
- url: /
7+
script: main.app
8+
9+
libraries:
10+
- name: MySQLdb
11+
version: "latest"

appengine/cloudsql/main.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2013 Google Inc. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
import os
15+
16+
import MySQLdb
17+
import webapp2
18+
19+
20+
class IndexPage(webapp2.RequestHandler):
21+
def get(self):
22+
self.response.headers['Content-Type'] = 'text/plain'
23+
24+
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
25+
db = MySQLdb.connect(
26+
unix_socket='/cloudsql/my_project:my_instance',
27+
user='root')
28+
else:
29+
db = MySQLdb.connect(host='localhost', user='root')
30+
31+
cursor = db.cursor()
32+
cursor.execute('SHOW VARIABLES')
33+
for r in cursor.fetchall():
34+
self.response.write('%s\n' % str(r))
35+
36+
37+
app = webapp2.WSGIApplication([
38+
('/', IndexPage),
39+
], debug=True)

0 commit comments

Comments
 (0)