jeudi 5 mai 2016

Write to Sqlite database (read only file-system error)

So this is my attempt to connect and write to my sqlite database. I have tried for a while and searched for an answer. There are many similar cases but none that can be applied to my problem it seems. Or it's just me who doesn't understand the solution.

Thanks for any possible help in advance and hope this get solved since it is the last part of my application.

package com.mycompany.lista;

    import java.sql.*;
    import java.util.ArrayList;

public class SQLiteJDBC
{

public static ArrayList<CustomList> customArray = new ArrayList<CustomList>();
Statement stmt = null;
Connection c = null;


public void create()
{
    try
    {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:nyhet");
        System.out.println("Connection to database established");
        stmt = c.createStatement();
        String sql = "CREATE TABLE IF NOT EXISTS NYHET " +
                "(ID INT PRIMARY KEY     NOT NULL," +
                " TYPE           TEXT    NOT NULL, " +
                " TEXT           TEXT    NOT NULL, " +
                " URL            TEXT    NOT NULL, " +
                " DATE           TEXT    NOT NULL)";
        stmt.executeUpdate(sql);
        stmt.close();
        c.close();
    }
    catch (Exception e)
    {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

public ArrayList<CustomList> insert(int id, int type, String text, String url, String date) throws SQLException {
    try {
        Class.forName("org.sqlite.JDBC");

        c = DriverManager.getConnection("jdbc:sqlite:nyhet");
        System.out.println("Connection to database established");
        c.setAutoCommit(false);
        String sql = "INSERT INTO NYHET (ID,TYPE,NAME,URL,DATE) " +
                "VALUES (" + id + "," + type + "," + text + "," + url + "," + date + ");";
        stmt.executeUpdate(sql);

        ResultSet rs = stmt.executeQuery( "SELECT * FROM NYHET;" );
        while (rs.next())
        {
            int id1 = rs.getInt("id");
            int type1 = rs.getInt("type");
            String name1  = rs.getString("text");
            String url1 = rs.getString("url");
            String date1 = rs.getString("date");
            System.out.println( "ID = " + id1 );
            System.out.println( "NAME = " + type1 );
            System.out.println( "AGE = " + name1 );
            System.out.println( "ADDRESS = " + url1 );
            System.out.println( "SALARY = " + date1 );
            customArray.add(new CustomList(id1, type1, name1, url1, date1));
        }
        rs.close();
        stmt.close();
        c.close();

    }
    catch (ClassNotFoundException e)
    {
        return customArray;
    }
    return customArray;
}

public void remove()
{
    try
    {
        ResultSet rs = stmt.executeQuery( "SELECT * FROM NYHET;" );
        if (rs != null)
        {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:nyhet");
            c.setAutoCommit(false);
            System.out.println("Connection mot databasen fixad");
            stmt = c.createStatement();
            String sql = "DELETE from NYHET;";
            stmt.executeUpdate(sql);
            c.commit();
            stmt.close();
            c.close();
        }
    }
    catch (Exception e)
    {
        System.out.println( e.getClass().getName() + ": " + e.getMessage() + " super duper mega test");
    }
    System.out.println("everything went fine with removal");
}

public ArrayList<CustomList> select () {
    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:nyhet");
        c.setAutoCommit(false);
        System.out.println("Connection to database established");
        stmt = c.createStatement();
        ResultSet rs = stmt.executeQuery( "SELECT * FROM NYHET;" );
        while (rs.next())
        {
            int id1 = rs.getInt("id");
            int type1 = rs.getInt("type");
            String name1  = rs.getString("text");
            String url1 = rs.getString("url");
            String date1 = rs.getString("date");
            System.out.println( "ID = " + id1 );
            System.out.println( "NAME = " + type1 );
            System.out.println( "AGE = " + name1 );
            System.out.println( "ADDRESS = " + url1 );
            System.out.println( "SALARY = " + date1 );
            customArray.add(new CustomList(id1, type1, name1, url1, date1));
        }
        rs.close();
        stmt.close();
        c.close();
    }
    catch (Exception e)
    {
        System.err.println( e.getClass().getName() + ": " + e.getMessage() );
        System.out.println("is this one running?");
        return customArray;
    }



      System.out.println("everything went fine with insert");
        return customArray;
    }
}

And this is the error message i get if it helps some (i know the first line says something at least, but i am horrible at reading error messages more than that).

    05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ java.sql.SQLException: opening db: 'nyhet': open failed: EROFS (Read-only file system)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:24)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.createConnection(JDBC.java:114)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.connect(JDBC.java:88)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:179)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:144)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.SQLiteJDBC.insert(SQLiteJDBC.java:42)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:69)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:57)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ java.sql.SQLException: opening db: 'nyhet': open failed: EROFS (Read-only file system)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:24)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.createConnection(JDBC.java:114)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.connect(JDBC.java:88)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:179)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:144)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.SQLiteJDBC.insert(SQLiteJDBC.java:42)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:69)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:57)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ java.sql.SQLException: opening db: 'nyhet': open failed: EROFS (Read-only file system)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:24)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.createConnection(JDBC.java:114)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.connect(JDBC.java:88)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:179)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:144)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.SQLiteJDBC.insert(SQLiteJDBC.java:42)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:69)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:57)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ java.sql.SQLException: opening db: 'nyhet': open failed: EROFS (Read-only file system)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:24)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.createConnection(JDBC.java:114)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at org.sqlite.JDBC.connect(JDBC.java:88)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:179)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.sql.DriverManager.getConnection(DriverManager.java:144)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.SQLiteJDBC.insert(SQLiteJDBC.java:42)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:69)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.mycompany.lista.CounterStore$1.success(CounterStore.java:57)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5001)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-05 05:07:58.741    5277-5277/com.mycompany.lista W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

Aucun commentaire:

Enregistrer un commentaire