From 677a7698319685912e0114caf59469d5b656a671 Mon Sep 17 00:00:00 2001 From: Tim Pulver Date: Thu, 12 Mar 2015 12:35:51 +0100 Subject: [PATCH] Added MySQL localhost example --- .../MySQL_localhost_example.pde | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 examples/MySQL_localhost_example/MySQL_localhost_example.pde diff --git a/examples/MySQL_localhost_example/MySQL_localhost_example.pde b/examples/MySQL_localhost_example/MySQL_localhost_example.pde new file mode 100644 index 0000000..c6bfeb3 --- /dev/null +++ b/examples/MySQL_localhost_example/MySQL_localhost_example.pde @@ -0,0 +1,37 @@ +// Library by fjenett +// Example created on 2014-03-12 by Powder +// Tested with Processing 3.0a5 + +import de.bezier.data.sql.*; + +String dbHost = "localhost"; // if you are using a using a local database, this should be fine +String dbPort = "8889"; // replace with database port, MAMP standard is 8889 +String dbUser = "root"; // replace with database username, MAMP standard is "root" +String dbPass = "root"; // replace with database password, MAMP standard is "root" +String dbName = "database_name"; // replace with database name +String tableName = "table_name"; // replace with table name + +MySQL msql; + +void setup() { + msql = new MySQL( this, dbHost + ":" + dbPort, dbName, dbUser, dbPass ); + + if (msql.connect()) { + // get number of rows + msql.query("SELECT COUNT(*) FROM " + tableName); + msql.next(); + println("Number of rows: " + msql.getInt(1)); + println(); + + // access table + msql.query("SELECT * FROM " + tableName); + while (msql.next()){ + // replace "first_name" and "last_name" with column names from your table + String s1 = msql.getString("first_name"); + String s2 = msql.getString("last_name"); + println(s1 + " " + s2); + } + } else { + println("Yikes, there was an error!"); + } +}