Android Open Source - DKO D B






From Project

Back to project page DKO.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project DKO listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.kered.contactlensfinder;
//from   w  w  w .ja  v a  2s .  c o  m
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.kered.dko.Context.Undoer;
import org.kered.dko.datasource.SingleConnectionDataSource;

import android.content.Context;
import android.util.Log;

public class DB {
  
    private static final String DB_FILENAME = "myapp.db";
    private static final String ASSETS_INIT_DB_FILENAME = "init.db";
    private static boolean initted = false;
    
  /**
   * Call once per application run.  Multiple calls are OK / ignored.
   * Recommend in the onCreate() of you main activity.
   * Copies an initial sqlite3 database out of you assets folder
   * into you data folder, then sets up a default DataSource.
   * @param context
   */
  public static void getReady(Context context) {
    try {
      getReadyThrow(context);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

    public static void getReadyThrow(final Context context) throws IOException, SQLException {
      if (initted) return;
    initDriver();
    boolean needToInitDatabase = !new File(getDatabaseFilename(context)).exists();
    Log.v(TAG, needToInitDatabase ? "i need to init our db..." : "i do not need to init our db");
    if (needToInitDatabase) {
      initOnDeviceDatabase(context);
      Log.v(TAG, "...db initted! ");
    }
    setDefaultDataSource(context);
    initted = true;
  }

  private static void initOnDeviceDatabase(final Context context) throws IOException {
    // copies from assets/ to /data/data/.../
    InputStream in = context.getAssets().open(ASSETS_INIT_DB_FILENAME);
    OutputStream out = new FileOutputStream(getDatabaseFilename(context));
    byte[] buffer = new byte[1024];
    for (int len; (len = in.read(buffer)) != -1; ) {
      out.write(buffer, 0, len);
    }
    out.flush();
    out.close();
    in.close();
  }

    private static void initDriver() {
      // make sure the SQLDroid driver is loaded
    try {
      final Driver d = (Driver) Class.forName("org.sqldroid.SQLDroidDriver").newInstance();
    } catch (final Exception e) {
      throw new RuntimeException(e);
    }
    }

    private static void setDefaultDataSource(Context context) {
    try {
      
      // SQLDroid connections are expensive to create, so use a DKO helper
      // class that acts as a really simple connection pool.
      Connection conn = DriverManager.getConnection(getDatabaseURL(context));
      SingleConnectionDataSource ds = new SingleConnectionDataSource(conn);

      // setAutoUndo(false) is important here, otherwise this contextual setting
      // will revert itself once the Undoer object is GCed.
      Undoer undoer = org.kered.dko.Context.getVMContext().setDataSource(ds);
      undoer.setAutoUndo(false);

    } catch (SQLException e) {
      e.printStackTrace();
    }
    }

  private static String getDatabaseURL(Context context) {
    return "jdbc:sqldroid:"+ getDatabaseFilename(context);
  }

  private static String getDatabaseFilename(Context context) {
    return context.getApplicationInfo().dataDir +"/"+ DB_FILENAME;
  }

  private static final String TAG = "DB";

}




Java Source Code List

.HelloWorld.java
org.kered.contactlensfinder.DB.java
org.kered.contactlensfinder.MainActivity.java
org.kered.contactlensfinder.ViewManufacturersActivity.java
org.kered.contactlensfinder.ViewProductsActivity.java
org.kered.contactlensfinder.ViewPropertiesActivity.java
org.kered.dko.AbstractQuery.java
org.kered.dko.Bulk.java
org.kered.dko.CSV.java
org.kered.dko.ClosableIterator.java
org.kered.dko.Condition.java
org.kered.dko.Constants.java
org.kered.dko.Context.java
org.kered.dko.DBQuery.java
org.kered.dko.DBRowIterator.java
org.kered.dko.Diff.java
org.kered.dko.DualIterator.java
org.kered.dko.Expression.java
org.kered.dko.Field.java
org.kered.dko.FilteringQuery.java
org.kered.dko.Function.java
org.kered.dko.InMemoryQuery.java
org.kered.dko.Join.java
org.kered.dko.LazyCacheIterable.java
org.kered.dko.LocalJoin.java
org.kered.dko.M.java
org.kered.dko.Main.java
org.kered.dko.MatryoshkaQuery.java
org.kered.dko.PeekableClosableIterator.java
org.kered.dko.PeekableIterator.java
org.kered.dko.QueryAddField.java
org.kered.dko.QueryFactory.java
org.kered.dko.QuerySnapshot.java
org.kered.dko.Query.java
org.kered.dko.SQLFunction.java
org.kered.dko.SelectAsMapIterable.java
org.kered.dko.SelectFromOAI.java
org.kered.dko.SelectSingleColumn.java
org.kered.dko.SoftJoinUtil.java
org.kered.dko.SoftJoin.java
org.kered.dko.SqlContext.java
org.kered.dko.Statistics.java
org.kered.dko.SubQueryField.java
org.kered.dko.TableInfo.java
org.kered.dko.TableWrapper.java
org.kered.dko.Table.java
org.kered.dko.TemporaryTableFactory.java
org.kered.dko.TmpTableBuilder.java
org.kered.dko.Tuple.java
org.kered.dko.UsageMonitor.java
org.kered.dko.UsageStats.java
org.kered.dko.Util.java
org.kered.dko.ant.ClassGenerator.java
org.kered.dko.ant.CodeGeneratorBase.java
org.kered.dko.ant.CodeGenerator.java
org.kered.dko.ant.DataSourceGenerator.java
org.kered.dko.ant.GsonGenerator.java
org.kered.dko.ant.JoinGenerator.java
org.kered.dko.ant.Main.java
org.kered.dko.ant.SchemaExtractorBase.java
org.kered.dko.ant.SchemaExtractor.java
org.kered.dko.ant.Util.java
org.kered.dko.datasource.CheapConnectionPoolingDataSource.java
org.kered.dko.datasource.ConnectionCountingDataSource.java
org.kered.dko.datasource.JDBCDriverDataSource.java
org.kered.dko.datasource.MatryoshkaDataSource.java
org.kered.dko.datasource.MirroredDataSource.java
org.kered.dko.datasource.ReflectedDataSource.java
org.kered.dko.datasource.SingleConnectionDataSource.java
org.kered.dko.datasource.SingleThreadedDataSource.java
org.kered.dko.datasource.UnClosableConnection.java
org.kered.dko.datasource.Util.java
org.kered.dko.json.CDL.java
org.kered.dko.json.CookieList.java
org.kered.dko.json.Cookie.java
org.kered.dko.json.HTTPTokener.java
org.kered.dko.json.HTTP.java
org.kered.dko.json.JSONArray.java
org.kered.dko.json.JSONException.java
org.kered.dko.json.JSONML.java
org.kered.dko.json.JSONObject.java
org.kered.dko.json.JSONString.java
org.kered.dko.json.JSONStringer.java
org.kered.dko.json.JSONTokener.java
org.kered.dko.json.JSONWriter.java
org.kered.dko.json.Pickle.java
org.kered.dko.json.XMLTokener.java
org.kered.dko.json.XML.java
org.kered.dko.junk.DerbyLoadTestSchema.java
org.kered.dko.junk.OracleCreateTestUser.java
org.kered.dko.junk.OracleLoadTestSchema.java
org.kered.dko.persistence.Util.java
org.kered.dko.util.DumpDatabase.java
sakila.Example0.java
sakila.Example1.java
sakila.Example2.java
sakila.Util.java