PGTop for Android: Things I learned making this app
-
Upload
mark-wong -
Category
Technology
-
view
1.935 -
download
2
description
Transcript of PGTop for Android: Things I learned making this app
PGTop for AndroidThings I learned making this app
Mark [email protected]
Port1and, Oregon, PostgreSQL Users Group
20 January 2011
PGTop for Android
A PostgresSQL stats monitoring appfor Android:http://github.com/markwkm/PGTop
markwkm (PDXPUG) PGTop for Android 20 January 2011 2 / 37
How did I get started?
Once upon a time I got developer edition G1 from Google...
markwkm (PDXPUG) PGTop for Android 20 January 2011 3 / 37
And it sat idle...
• Postgres JDBC driver for Android platform didn’t work
• Native C development kit too hard for me to just build just the libpqlibrary from the postgres source
markwkm (PDXPUG) PGTop for Android 20 January 2011 4 / 37
And then some motivation...
__ __
/ \~~~/ \ . o O ( PostgreSQL contest! )
,----( oo )
/ \__ __/
/| (\ |(
^ \ /___\ /\ |
|__| |__|-"
markwkm (PDXPUG) PGTop for Android 20 January 2011 5 / 37
\ /
\ /
_.-""""-._
.’ ‘.
/ O O \ . o O ( Free Nexus One! )
__ +----------------+ __
/ \+----------------+/ \
| || || |
| || || |
| || || |
| || || |
\__/| |\__/
\__ __ __/
| | | |
| | | |
\__/ \__/markwkm (PDXPUG) PGTop for Android 20 January 2011 6 / 37
New developments
Dave Cramer builds a version of the postgres JDBC jar that works on Android:http://groups.google.com/group/pgandroid/attach/4ee44940c9f7319f/postgresql.jar?part=2
markwkm (PDXPUG) PGTop for Android 20 January 2011 7 / 37
Slapped together and entered PGTop for Android:
markwkm (PDXPUG) PGTop for Android 20 January 2011 8 / 37
Oh well, continue to make a few improvement...
markwkm (PDXPUG) PGTop for Android 20 January 2011 10 / 37
How to set up a development environment
• Requires 32-bit java development environment• Requires Android sdk
• Android emulator
• Android jars
• Android packaging tools
• Optional to have Eclipse• ui designer gui
• hides xml
• Optional to have Android ndk for developing native C library
markwkm (PDXPUG) PGTop for Android 20 January 2011 12 / 37
How to make a launcher icon
I need to learn how to use Inkscape or Gimp better to make the ascii art iconmeet the recommended guidelines better:
markwkm (PDXPUG) PGTop for Android 20 January 2011 13 / 37
About layouts
Only used scroll view and linear layouts.
markwkm (PDXPUG) PGTop for Android 20 January 2011 14 / 37
About ui widgets
• TextViews (labels)
• EditViews (input text)
• Spinners (selection)
• Buttons
• custom menus
markwkm (PDXPUG) PGTop for Android 20 January 2011 15 / 37
How the threading model works
• child threads don’t have access to ui objects
• use android.os.Handler to pass messages to parent thread
markwkm (PDXPUG) PGTop for Android 20 January 2011 16 / 37
How PGTop uses threads
Create a Handler:
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// Update UI objects...
}
}
Child thread loops continuously to query PostgreSQL database for stats andupdates class variables with data.Pass a message to a handler to call the method that updates the UI:
handler.sendEmptyMessage(0);
markwkm (PDXPUG) PGTop for Android 20 January 2011 17 / 37
How to use SQLite3 interface
To store database connection information (oversimplified sample):
public class PGConnectionOpenHelper extends SQLiteOpenHelper
SQLiteDatabase db = openHelper.getReadableDatabase();
Cursor c = db.rawQuery(SELECT_CONNECTIONS, null);
while (c.moveToNext()) {
c.getString(c.getColumnIndex("database"));
}
markwkm (PDXPUG) PGTop for Android 20 January 2011 18 / 37
How to read the stdout/stderr log
The debugger was too hard for me.
markwkm (PDXPUG) PGTop for Android 20 January 2011 19 / 37
__ __
/ \~~~/ \ . o O ( Code review time! )
,----( oo )
/ \__ __/
/| (\ |(
^ \ /___\ /\ |
|__| |__|-"
markwkm (PDXPUG) PGTop for Android 20 January 2011 20 / 37
PostgreSQL JDBC
Now for some simple examples for connecting to a PostgreSQL database andquerying some data. Warning, code formatted to fit better on these slides. . .
markwkm (PDXPUG) PGTop for Android 20 January 2011 21 / 37
Open a Database Connection
Load the PostgreSQL JDBC driver and open a database connection using SSL:
Class.forName("org.postgresql.Driver");
String url;
url = "jdbc:postgresql://pghost:5432/pgdatabase" +
"?sslfactory=org.postgresql.ssl.NonValidatingFactory" +
"&ssl=true";
Connection conn = DriverManager.getConnection(url,
"pguser",
"pgpass");
\\ Don’t forget to close the connection when you’re done.
\\ conn.close();
markwkm (PDXPUG) PGTop for Android 20 January 2011 22 / 37
Execute a Query
Building on the previous slide, select the name of all relations from pg class
and iterate through every row returned:
String sql;
sql = "SELECT relname FROM pg_class WHERE relkind = ’r’;"
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sql);
while (rs.next()) {
\\ Columns are enumerated starting with 1.
String relname = rs.getString(1);
}
rs.close();
st.close();
markwkm (PDXPUG) PGTop for Android 20 January 2011 23 / 37
Execute a Query with a Bind Value
Building on the previous slide, select the number of all relations from pg class:
String sql = "SELECT COUNT(*) FROM pg_class WHERE relkind = ?;"
PreparedStatement ps = conn.createStatement();
// Bind variables are enumerated starting with 1;
ps.setString(1, "r");
ResultSet rs = ps.executeQuery(sql);
rs.next();
long count = rs.getLong(1);
rs.close();
ps.close();
markwkm (PDXPUG) PGTop for Android 20 January 2011 24 / 37
More JDBC Code Examples
The PostgreSQL JDBC documentation has more examples athttp://jdbc.postgresql.org/documentation/head/
markwkm (PDXPUG) PGTop for Android 20 January 2011 25 / 37
Executing INSERT, UPDATE, DELETE SQL statements
markwkm (PDXPUG) PGTop for Android 20 January 2011 27 / 37
JDBC escapes e.g. strings, outer joins, date-time, scalar functions
markwkm (PDXPUG) PGTop for Android 20 January 2011 31 / 37
PostgreSQL Extensions to the JDBC API
• Geometric data types
• Large objects
• Listen/Notify
• Server prepared statements
markwkm (PDXPUG) PGTop for Android 20 January 2011 32 / 37
Further JDBC Reading
JDBC API Documentation and JDBC Specificationhttp://jdbc.postgresql.org/documentation/head/reading.html
markwkm (PDXPUG) PGTop for Android 20 January 2011 34 / 37
__ __
/ \~~~/ \ . o O ( Thank you! )
,----( oo )
/ \__ __/
/| (\ |(
^ \ /___\ /\ |
|__| |__|-"
markwkm (PDXPUG) PGTop for Android 20 January 2011 35 / 37
Acknowledgements
Hayley Jane Wakenshaw
__ __
/ \~~~/ \
,----( oo )
/ \__ __/
/| (\ |(
^ \ /___\ /\ |
|__| |__|-"
markwkm (PDXPUG) PGTop for Android 20 January 2011 36 / 37
License
This work is licensed under a Creative Commons Attribution 3.0 UnportedLicense. To view a copy of this license, (a) visithttp://creativecommons.org/licenses/by/3.0/us/; or, (b) send aletter to Creative Commons, 171 2nd Street, Suite 300, San Francisco,California, 94105, USA.
markwkm (PDXPUG) PGTop for Android 20 January 2011 37 / 37