diff --git a/build.gradle b/build.gradle index 8267576591eee1813daaf78ad234c2898235ba9d..5156561d42bdf69e6c2010bfd12dccfce64cadb4 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,8 @@ allprojects { box2DLightsVersion = '1.3' ashleyVersion = '1.3.1' aiVersion = '1.4.0' + sqlDroidVersion = '1.0.3' + sqlLiteJdbcVersion = '3.8.10.1' } repositories { @@ -99,6 +101,7 @@ project(":android") { compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion" compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" compile "net.engio:mbassador:$mbassadorVersion" + compile "org.sqldroid:sqldroid:$sqlDroidVersion" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" @@ -154,6 +157,7 @@ project(":core") { compile "net.dermetfan.libgdx-utils:libgdx-utils:$gdxUtilsVersion" compile "net.engio:mbassador:$mbassadorVersion" compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" + compile "org.xerial:sqlite-jdbc:$sqlLiteJdbcVersion" testCompile "junit:junit:4.11" testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion" diff --git a/core/src/cz/nic/tablexia/Tablexia.java b/core/src/cz/nic/tablexia/Tablexia.java index f7bde234949749b1aeb2ea1388aeea77329439c5..0bf3078dc3240ebe754f1902780e5be72c276a2f 100644 --- a/core/src/cz/nic/tablexia/Tablexia.java +++ b/core/src/cz/nic/tablexia/Tablexia.java @@ -16,6 +16,7 @@ import cz.nic.tablexia.loader.application.ApplicationTextManager; import cz.nic.tablexia.loader.application.ApplicationTextureManager; import cz.nic.tablexia.loader.zip.ZipAssetLoader; import cz.nic.tablexia.menu.MainMenuContainer; +import cz.nic.tablexia.persist.SqliteStorage; import cz.nic.tablexia.screen.AbstractTablexiaScreen; import cz.nic.tablexia.screen.loader.LoaderScreen; import cz.nic.tablexia.util.Log; @@ -120,6 +121,8 @@ public class Tablexia extends TablexiaApplication { // init event bus handlers ApplicationBus.getInstance().subscribe(this); + SqliteStorage.storageTest(); + // start loading application scope data startLoading(TablexiaSettings.getInstance().getLocale()); } diff --git a/core/src/cz/nic/tablexia/persist/SqliteStorage.java b/core/src/cz/nic/tablexia/persist/SqliteStorage.java new file mode 100644 index 0000000000000000000000000000000000000000..9af75a61f0425e77a624ba3537125b0123cee951 --- /dev/null +++ b/core/src/cz/nic/tablexia/persist/SqliteStorage.java @@ -0,0 +1,115 @@ +package cz.nic.tablexia.persist; + +import com.badlogic.gdx.Application; +import com.badlogic.gdx.Gdx; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import cz.nic.tablexia.loader.TablexiaAssetManager; +import cz.nic.tablexia.util.Log; + +/** + * Created by matyas on 18.5.15. + */ +public class SqliteStorage { + + private enum SQLDriverType { + + DESKTOP(Application.ApplicationType.Desktop, "org.sqlite.JDBC", "jdbc:sqlite:"), + ANDROID(Application.ApplicationType.Android, "org.sqldroid.SQLDroidDriver", "jdbc:sqldroid:"); + + private Application.ApplicationType applicationType; + private String driverName; + private String databaseConnection; + + SQLDriverType(Application.ApplicationType applicationType, String driverName, String databaseConnection) { + this.applicationType = applicationType; + this.driverName = driverName; + this.databaseConnection = databaseConnection; + } + + public static String getDriverNameForApplicationType(Application.ApplicationType applicationType) { + for (SQLDriverType driverType : SQLDriverType.values()) { + if (driverType.applicationType == applicationType) { + return driverType.driverName; + } + } + return null; + } + + public static String getDatabseConnectionForApplicationType(Application.ApplicationType applicationType) { + for (SQLDriverType driverType : SQLDriverType.values()) { + if (driverType.applicationType == applicationType) { + return driverType.databaseConnection; + } + } + return null; + } + } + + public static void storageTest() { + + + + Log.debug(SqliteStorage.class, "------->> POKUS"); + + String dbDirectory = Gdx.files.getExternalStoragePath() + TablexiaAssetManager.StorageType.EXTERNAL.getStoragePath(); + String dbFileName = "tablexia.db"; + String dbPath = dbDirectory + "/" + dbFileName; + + // load the sqlite-JDBC driver using the current class loader + try { + //Class.forName("org.sqlite.JDBC"); + Class.forName(SQLDriverType.getDriverNameForApplicationType(Gdx.app.getType())); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + + Connection connection = null; + try + { + // create a database connection + connection = DriverManager.getConnection(SQLDriverType.getDatabseConnectionForApplicationType(Gdx.app.getType()) + dbPath); + Statement statement = connection.createStatement(); + //statement.setQueryTimeout(30); // set timeout to 30 sec. + + Log.debug(SqliteStorage.class, "------->> POKUS2"); + + statement.executeUpdate("drop table if exists person"); + statement.executeUpdate("create table person (id integer, name string)"); + statement.executeUpdate("insert into person values(1, 'leo')"); + statement.executeUpdate("insert into person values(2, 'yui')"); + ResultSet rs = statement.executeQuery("select * from person"); + while(rs.next()) + { + // read the result set + System.out.println("name = " + rs.getString("name")); + System.out.println("id = " + rs.getInt("id")); + } + } + catch(SQLException e) + { + // if the error message is "out of memory", + // it probably means no database file is found + e.printStackTrace(); + } + finally + { + Log.debug(SqliteStorage.class, "------->> POKUS3"); + try + { + if(connection != null) + connection.close(); + } + catch(SQLException e) + { + // connection close failed. + System.err.println(e); + } + } + } +}