GWT‎ > ‎gwt-mobile-webkit‎ > ‎

Database API

GWT-Mobile-WebkitのDatabase APIについて
http://code.google.com/p/gwt-mobile-webkit/wiki/DatabaseApi

基本的には、サンプルの
HelloDatabaseのように、DataService インターフェースにアノテーションで拡張するのが自然です。

昔から、GearsでDatabaseは使えていましたが
これは、HTML 5 Web SQL Database API ですので、
iPhone2.0, Android2.0, Safari3.1, Chrome4, Opera10.50
などのブラウザーでも動きます。

開発モードでも動くようにする?

number of '?'s in statement string does not match argument count 
コンパイル後の本番モードだと動くが、開発モードだと上記のエラーが出てHelloDatabaseも動かない場合
(私はケースではChrome 4.1.249.1036 +gwt-html5-database-1.5.0.tar.gz)

DataServiceがやっていることを、手書きにして、さらに、うまく動かない、Statementを空にして手動でsqlにしたら開発モードでも動きました。
こういう方法はバグの温床になりやすいのですが、非常時には役に立つかもしれませんので、掲載してみます。
繰り返しますが、DataServiceを使う方法が普通です。

Statementを使わないDB接続

package com.akjava.gwt.webtest.client;

import java.util.ArrayList;
import java.util.List;

import com.google.code.gwt.database.client.Database;
import com.google.code.gwt.database.client.GenericRow;
import com.google.code.gwt.database.client.SQLError;
import com.google.code.gwt.database.client.SQLResultSet;
import com.google.code.gwt.database.client.SQLTransaction;
import com.google.code.gwt.database.client.StatementCallback;
import com.google.code.gwt.database.client.TransactionCallback;
import com.google.code.gwt.database.client.service.DataServiceException;
import com.google.code.gwt.database.client.service.ListCallback;
import com.google.code.gwt.database.client.service.RowIdListCallback;
import com.google.code.gwt.database.client.service.VoidCallback;
import com.google.common.collect.Lists;

/**
 * 
 * 
 * @author aki
 *
 */
/**
 * DataServiceを使うことを強く推奨
 * @deprecated
 */
public class WebTestDatabase {
Database db;
public WebTestDatabase(){
db=connection();
}
private Database connection(){
return Database.openDatabase("WebTestData", "1.0", "Click Counter", 10000);
}
void initTable(final VoidCallback callback){
db.transaction(new TransactionCallback() {
   public void onTransactionStart(SQLTransaction tx) {
       tx.executeSql("CREATE TABLE IF NOT EXISTS inputData ("
               + "id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
               + "title TEXT,value TEXT)", null);
   }
   public void onTransactionFailure(SQLError error) {
       callback.onFailure(new DataServiceException(error));
   }
   public void onTransactionSuccess() {
    callback.onSuccess();
   }
});
}
void getDatas(final ListCallback<GenericRow> callback){
db.transaction(new TransactionCallback() {
final List<GenericRow> rows=new ArrayList<GenericRow>();
   public void onTransactionStart(SQLTransaction tx) {
    tx.executeSql("SELECT * FROM inputData", null, new StatementCallback<GenericRow>() {
           public boolean onFailure(SQLTransaction transaction, SQLError error) {
               return false;  // don't roll back
           }
           public void onSuccess(SQLTransaction transaction, SQLResultSet<GenericRow> resultSet) {
              
               for (GenericRow row : resultSet.getRows()) {
                  rows.add(row);
               }
           }});
   }

@Override
public void onTransactionFailure(SQLError error) {
callback.onFailure(new DataServiceException(error));
}

@Override
public void onTransactionSuccess() {
callback.onSuccess(rows);
}
});
}
 
void addData(final String title,final String text, final RowIdListCallback callback){
db.transaction(new TransactionCallback() {
List<Integer> ids=Lists.newArrayList();
   public void onTransactionStart(SQLTransaction tx) {
    String sql=toSQLString("INSERT INTO inputData (title,value) VALUES (?,?)",new Object[]{title,text});
    System.out.println(sql);
    tx.executeSql(sql,new Object[0],new StatementCallback<GenericRow>() {
           public boolean onFailure(SQLTransaction transaction, SQLError error) {
            callback.onFailure(new DataServiceException(error));
               return false;  // don't roll back
           }
           public void onSuccess(SQLTransaction transaction, SQLResultSet<GenericRow> resultSet) {
              
              ids.add(resultSet.getInsertId());
               
           }});
     
   }
   public void onTransactionFailure(SQLError error) {
       callback.onFailure(new DataServiceException(error));
   }
   public void onTransactionSuccess() {
    callback.onSuccess(ids);
   }
});
}
 
private String toSQLString(String base,Object[] items){
for(int i=0;i<items.length;i++){
int find=base.indexOf('?');
if(find!=-1){
base=base.replaceFirst("\\?", toSQLString(items[i]));
}
}
return base;
}
private String toSQLString(Object value){
if(value instanceof String){
String text=(String)value;
text=text.replace("'","''");
return "'"+text+"'";
}
return value.toString();
}
}





Comments