Problem to connect to external database

Asked

Viewed 90 times

0

When trying to open the connection an error message appears:

W/System.err: java.sql.SQLException: opening db: 'C:\sqlite\AgendaOnline': open failed: EROFS (Read-only file system)
W/System.err:     at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
W/System.err:     at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
W/System.err:     at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:0)
W/System.err:     at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:0)
W/System.err:     at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:0)
W/System.err:     at org.sqlite.JDBC.createConnection(JDBC.java:114)
W/System.err:     at org.sqlite.JDBC.connect(JDBC.java:88)
W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:179)
W/System.err:     at java.sql.DriverManager.getConnection(DriverManager.java:213)
W/System.err:     at com.example.jonathan.agendaonline.BancoConexao$override.abrir(BancoConexao.java:33)
W/System.err:     at com.example.jonathan.agendaonline.BancoConexao$override.access$dispatch(BancoConexao.java)
W/System.err:     at com.example.jonathan.agendaonline.BancoConexao.abrir(BancoConexao.java:0)
W/System.err:     at com.example.jonathan.agendaonline.CriarUsuario$1.onClick(CriarUsuario.java:34)
W/System.err:     at android.view.View.performClick(View.java:5198)
W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

this is bank class

package com.example.jonathan.agendaonline;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class BancoConexao {
    private static Connection con;
    private static Statement stm;
    public static void main(String[] args) throws Exception {
      abrir();
    }

    public static void abrir() throws Exception {

        String driver = "org.sqlite.JDBC";
        String url = "jdbc:sqlite:"
                + "C:\\sqlite\\AgendaOnline";
        String usuario = null;
        String senha = null;

        Class.forName(driver);
        con = DriverManager.getConnection(url, usuario, senha);
        System.out.print("Conetado");
        stm = con.createStatement();
    }

    public static void fechar() throws Exception {
        if (stm != null)
            stm.close();
        if (con != null)
            con.close();
        stm = null;
        con = null;
    }

    public static void atualizar(String sql) throws Exception {
        if (con == null || stm == null)
            abrir();
        stm.execute(sql);
    }

    public static ResultSet getData(String sql) throws Exception {
        if (con == null || stm == null)
            abrir();
        return stm.executeQuery(sql);
    }
}

And this is one of the methods you should use from the bank

package com.example.jonathan.agendaonline;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class CriarUsuario extends AppCompatActivity {

    TextView nome,senha,email;
    String nomeS,senhaS,emailS;
    Button criar;
    Usuario usuario;
    int tipo = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_criar_usuario);
        nome = (EditText) findViewById(R.id.nome);
        senha = (EditText) findViewById(R.id.senha);
        email = (EditText) findViewById(R.id.email);
        criar = (Button) findViewById(R.id.Criar);
        criar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nomeS = nome.getText().toString();
                senhaS = senha.getText().toString();
                emailS = email.getText().toString();
                try {
                    BancoConexao.abrir();
                    String sql = String.format("INSERT INTO Usuarios ("
                                    + nomeS
                                    + senhaS
                                    + emailS
                                    +tipo
                                    + ") VALUES ('%1$s', '%2$s', '%3$s','%4$s')"
                            );
                    BancoConexao.atualizar(sql);
                    Toast.makeText(getApplicationContext(),"Cadastro bem sussedido", Toast.LENGTH_LONG).show();

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

                Intent intent = new Intent(CriarUsuario.this, LoginActivity.class);

            }    
        });
    }
}
  • The error isn’t in the bank, it’s in your methods, you’re not allowed to write in the bank, just read. Take a look at this class here: https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html. which I usually use when I move with Sqlite, has a method called getWritableDatabase() that allows you to exactly pick up a BD with a writing permission.

  • In case as I understand, can only be used for native android bank that is not my case

  • Have a look at this link here, follow the instructions that you can use Sqlite https://developer.android.com/training/basics/data-storage/databases.html

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.