Google uses BigTable as the database in Google App Engine (GAE). This article uses the JDO layer with the Datastore Java API to create, retrieve, update & delete Database records in PHP at Google App Engine.
The starting point for this example is an GAE project in Eclipse with PHP, see this blog posting to set that up.
First create a java class for setting up the database connection, save below file as src/database/connection.java in your Eclipse GAE project.
package database;Create a Java class with JDO annotations for every database table you want to use, below the one as used in this article, save it as src/database/person.java in your Eclipse GAE project.
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManagerFactory;
public final class connection {
private static final PersistenceManagerFactory pmfInstance =
JDOHelper.getPersistenceManagerFactory("transactions-optional");
private connection() {}
public static PersistenceManagerFactory get() {
return pmfInstance;
}
}
package database;Above Java code has the same functionality as below SQL code (please make no nasty comments about the java folks)
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class person {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Long person;
@Persistent
private String firstName;
@Persistent
private String lastName;
public Long getPerson() {
return person;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public void setFirstName(String in) {
this.firstName = in;
}
public void setLastName(String in) {
this.lastName = in;
}
}
CREATE TABLE person (Now we are ready to create PHP scripts that uses GAE BigTable as database! Below the PHP source that will be used as a template for every example.
person BIGINT AUTO_INCREMENT ,
firstName TEXT,
lastName TEXT,
PRIMARY KEY (person)
);
<?phpCreating records in the database can be done with below PHP code.
echo "<pre>";
// Import the java code for setting up a database connection.
import database.connection;
// Import the table definition class
import database.person;
// Setup a database connection.
$connection = connection::get()->getPersistenceManager();
..... example code goes here .....
// Close the database connection.
$connection->close();
echo "</pre>";
?>
$record = new person;Retrieving records from the database, that can be done with below PHP script.
$record->setFirstName('John');
$record->setLastName('Doe');
$connection->makePersistent($record);
$key = $record->getPerson(); // Not the record but the field person that is primary key
echo "Added 'John Doe' with number $key in the database \n";
$record = new person;
$record->setFirstName('Jane');
$record->setLastName('Doe');
$connection->makePersistent($record);
$key = $record->getPerson(); // Not the record but the field person that is primary key
echo "Added 'Jane Doe' with number $key in the database \n";
$query = $connection->newQuery("select from database.person");Above examples prints all records from the table, of course you can change that, use something like below
$results = $query->execute();
foreach($results as $record) {
echo "Key: " . $record->getPerson();
echo " Name: " . $record->getFirstName() . " " . $record->getLastName() . "\n";
}
$query = $connection->newQuery("select from person where person == 5");A Update example below.
$query = $connection->newQuery('select from person where lastName <> "Doe"');
$query = $connection->newQuery('select from database.person where firstName == "John"');The delete example:
$results = $query->execute();
$record = $results[0];
$record->setFirstName("Lucky");
$record->setLastName("Luke");
$connection->flush{};
$query = $connection->newQuery('select from database.person where firstName == "Jane"');
$results = $query->execute();
$connection->deletePersistentAll($results);
See here for a proof of concept how you can use 'normal' SQL in PHP at GAE
Sources from this article
4 opmerkingen:
I had a problem accessing the functions. I moved the database package into the other package I had setup for a eclipse/java project but it's not recognizing the java functions. Is there somewhere else I have to define the classes (I've had some success with php/quercus on the gae for java).
Hi,
Can you please mention the changes in a hierarchial tree form so that we can understand which file to be placed at what location.
here changes to be made in servlet are not mentioned
Question:
I want JDO on app engine java to send a string to php so that it can store in mysql database
I am getting the following error while executing the neWquery function:
$query = $this->connection->newQuery("select from sinica.googlefileservice.server.datastore.tbl_corporation where id == 222 and corp_contact_person == 'XYZ'");
com.caucho.quercus.QuercusException: org.datanucleus.jdo.JDOQuery.execute: Portion of expression could not be parsed: and corp_contact_person == 'XYZ'
Is there any other finction to fecth the reacord based on mutiple condion in where clause?
I've read a few good stuff here. Certainly price bookmarking for revisiting. I surprise how much attempt you put to create one of these magnificent informative website.
my web page - special compilation
Een reactie posten