Skip to content
Snippets Groups Projects
Commit 90612489 authored by Matyáš Latner's avatar Matyáš Latner
Browse files

#25 Test of SQLite storage on DESKTOP and ANDROID app types

parent 0305287d
No related merge requests found
......@@ -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"
......
......@@ -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());
}
......
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);
}
}
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment