Difference between revisions of "Tutorial: SQLite and Processing, Part II"

From dftwiki3
Jump to: navigation, search
(Setup)
(Eclipse)
Line 14: Line 14:
 
* Note: sometimes it is hard to get the '''Build Path''' option for the ''core.jar'' file.  If this option does not appear, a work-around is to right click on the project, and pick '''Properties''', '''Java Build Path''', then click on the '''Libraries''' tab, then '''Add Jar''', pick the '''core.jar''' file, and then click '''Ok'''.
 
* Note: sometimes it is hard to get the '''Build Path''' option for the ''core.jar'' file.  If this option does not appear, a work-around is to right click on the project, and pick '''Properties''', '''Java Build Path''', then click on the '''Libraries''' tab, then '''Add Jar''', pick the '''core.jar''' file, and then click '''Ok'''.
  
=Eclipse=
+
=Skeleton Class=
  
* Start Eclipse
+
* Create a new class in your package, and call it '''Main.java'''.
* Create a '''new project''', call it ''SQLiteTest''
+
* In the '''Edit''' Window, enter the following code:
* Create a '''new package''', call it ''SQlitePackage''
+
<br />
* Create a '''new Java classes''' in the pacakge called '''example1.java''' and copy/paste the contents of the  '''SQLite_example1.pde''' file which you'll find in the example folder of the zip file.
+
<source lang="java">
* Locate the '''SQL.java''' and '''SQLite.java''' programs in the '''source''', '''de''', '''bezier''', '''data''', '''sql''' folders of the zip file, and import them into the package using Eclipse's  '''File''', '''Import''', '''File System''' utility.
+
package tutorial1;
* Edit the 2 SQL*.java  files so that the '''package''' line at the top reads
+
 
 +
import processing.core.*;
 +
 
 +
public class Main extends PApplet {
 +
 
 +
public void setup() {
 +
// define the window size, make graphics softer, and make
 +
// the background white
 +
size(600, 600);
 +
smooth();
 +
background(255);
 +
}
  
    package SQLitePackage;
+
public void draw() {
 +
                // erase screen
 +
background(255);
  
* Edit the contents of '''example1.java''' as illustrated in [[Tutorial: SQLite and Processing Files#example1.java | here]].
+
                // change color of circle paint depending on mouse button
* Import the '''core.jar''' Processing library into your project (see above).
+
if (mousePressed)
* Import the file '''sqlitejdbc-v053-pure.jar''' from the '''library''' folder of the unzipped archive you downloaded, and add it as a library to your project (via the '''build path''').
+
fill(0);
<br />
+
else
<tanbox>'''Note''' that the project also works with the newer '''sqlitejdb-v056.jar''' file available from [http://www.zentus.com/sqlitejdbc/ www.zentus.com/sqlitejdbc].
+
fill(255);
</tanbox>
+
<br />
+
                // draw a circle where the mouse is located
<br />
+
ellipse(mouseX, mouseY, 80, 80);
* Create a '''bin''' folder in your ''SQLiteTest'' project folder.  You may have to use a Terminal window, or Windows Explorer for this.
+
}
* Copy the '''test.db''' file from the archive's '''data''' folder into the newly created '''bin''' folder.
+
}
 +
</source>
 
<br />
 
<br />
<tanbox>
 
'''Note''' that this is the trick for making the project work with the SQLite database files, as by default the JDBC driver will create a blank database file if it doesn't find the one you specify, and when your program attempts to open a table, the driver will invariably return a ''java.sql.SQLException: no such table:'' error message...
 
</tanbox>
 
 
 
<br />
 
<br />
  

Revision as of 08:16, 16 June 2012

--D. Thiebaut 17:01, 30 June 2011 (EDT)


This is the second part of a quick tutorial to accessing an SQLite database from Processing. Check out Part 1 for how to run the example in Processing's IDE.


Setup

  • First follow the steps of this great tutorial for creating Processing applications with Eclipse. Remember the steps for adding the core.jar library to your Eclipse project. You will need to repeat them for every new Processing project you want to create. The other option you'll have is to copy paste the skeleton project into another new project, in which case the core library should follow automatically into your new project.
  • Note: sometimes it is hard to get the Build Path option for the core.jar file. If this option does not appear, a work-around is to right click on the project, and pick Properties, Java Build Path, then click on the Libraries tab, then Add Jar, pick the core.jar file, and then click Ok.

Skeleton Class

  • Create a new class in your package, and call it Main.java.
  • In the Edit Window, enter the following code:


package tutorial1;

import processing.core.*;

public class Main extends PApplet {

	public void setup() {
		// define the window size, make graphics softer, and make
		// the background white
		size(600, 600);
		smooth();
		background(255);
	}

	public void draw() {
                // erase screen
		background(255);

                // change color of circle paint depending on mouse button
		if (mousePressed) 
			fill(0);
		else 
			fill(255);
		
                // draw a circle where the mouse is located
		ellipse(mouseX, mouseY, 80, 80);
	}
}



Final Project


SQliteProcessingEclipseProject1.png


Testing

  • Run the project, and observe the same output as in Tutorial 1.


SQliteProcessingEclipseProject2.png