0

I have an app on play store and everyday I got reports with a error. But this error occur in some devices, but not all. I have 5000 people using my app and that's dont get that error.

So: The report error I haved recived:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.jeanjn.guiadeacademia/com.jeanjn.guiadeacademia.control.Main}:
 android.database.sqlite.SQLiteException: near "CREATE": syntax error
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "CREATE": syntax error
at android.database.sqlite.SQLiteStatement.native_executeSql(Native Method)
at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:90)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
at com.jeanjn.guiadeacademia.control.DataBase.setData(DataBase.java:35)
at com.jeanjn.guiadeacademia.Support.ControleAtualizacao.atualizar(ControleAtualizacao.java:57)
at com.jeanjn.guiadeacademia.Support.ControleAtualizacao.cadastrar(ControleAtualizacao.java:67)
at com.jeanjn.guiadeacademia.Support.ControleAtualizacao.atualizar(ControleAtualizacao.java:16)
at com.jeanjn.guiadeacademia.control.Main.onCreate(Main.java:36)
at android.app.Activity.performCreate(Activity.java:4492)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more

The devices who got the error:

LG Optimus L5 dual (m4ds)   19  15,6%
Galaxy S DUOS (kyleopen)    14  11,5%
Galaxy Pocket Plus (coriplus)   13  10,7%
ckt75_cu_ics    10  8,2%
Optimus L5 (m4) 9   7,4%
Galaxy Y Plus (coriplusds)  7   5,7%
LNV-Lenovo A790e (msm7627a) 7   5,7%
nuclear-evb 6   4,9%
Galaxy S2 (GT-I9100)    6   4,9%
LG Optimus L7 (u0)  4   3,3%
LG Optimus L9 (u2)  4   3,3%
Galaxy Ace2 X (kylessopen)  3   2,5%
XT687 (ironmaxtv_umts)  2   1,6%
GT-P6200L (GT-P6200L)   2   1,6%
DROID RAZR i (smi)  2   1,6%
Galaxy S4 (ja3g)    1   0,8%
Picasso (picasso)   1   0,8%
tmn smart a18 (P175A20) 1   0,8%
yecon75_tb_ics  1   0,8%
nuclear-csitwo  1   0,8%
nuclear-pfdq88d 1   0,8%
Q203_BLU    1   0,8%
nuclear-Puppis  1   0,8%
XT910 (umts_spyder) 1   0,8%
crane-m1003h6   1   0,8%
YPY_AB10D   1   0,8%
ST25a   1   0,8%
TT-2501 1   0,8%
bird75_a_ics    1   0,8%

**Android version: Android 4.0.3 Android 3.2**

Please, can some on help me?

The link of my app: https://play.google.com/store/apps/details?id=com.jeanjn.guiadeacademia

My Script DataBase:

CREATE TABLE IF NOT EXISTS versaoLancada(_id integer primary key,versao integer);
CREATE TABLE IF NOT EXISTS Atualizacao(_id integer primary key,versao datetime);
CREATE TABLE IF NOT EXISTS Exercicio (_id INTEGER PRIMARY KEY,nome VARCHAR(300),categoria integer,descricao varchar(900),url varchar(900), FOREIGN KEY(categoria) references categoria(_id));
CREATE TABLE IF NOT EXISTS Categoria(_id integer primary key,nome varchar(300));
insert into Atualizacao (versao) values (1);
insert into Categoria (nome) values ('Selecione a categoria  ');
insert into Categoria (nome) values ('Abdominal');

This is only a part.

The DataBase class:

    package com.jeanjn.guiadeacademia.Control;

import android.content.Context;
import android.content.ContextWrapper;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class DataBase extends ContextWrapper{
    SQLiteDatabase db = null;

    public DataBase(Context base) {
        super(base);
        db = openOrCreateDatabase("academia.db", Context.MODE_PRIVATE, null);
    }

    public void dropDB()
    {
        this.getDatabasePath("academia.db").delete();
    }

    public Cursor getData(String command)
    {
        Cursor c = db.rawQuery(command, null);
        return c;
    }

    public SQLiteDatabase getDataBase()
    {
        return openOrCreateDatabase("academia.db", Context.MODE_PRIVATE, null);
    }

    public void setData(String command)
    {
        db.execSQL(command);
    }

    public int getInt(String command,String column)
    {
        Cursor c = db.rawQuery(command, null);
        c.moveToNext();
        return c.getInt(c.getColumnIndex(column));
    }

    public void closeDB(Cursor c)
    {
        if(c != null)
            c.close();
        if(db != null)
            db.close();
    }
}

The method call the setData(...)

public static void atualizar(Scanner s,Context cx)
    {
        while(s.hasNextLine())
        {
            String line = s.nextLine();
            if(line.trim().isEmpty())
                break;
            try{
                DataBase db = new DataBase(cx);
                db.setData(line);
                db.closeDB(null);
            }catch(Exception e)
            {
                Suporte.alertar(cx, "Erro ao cadastrar base de dados!\nPor favor, contate-nos no menu 'Fale conosco'.");
            }
        }
    }

How I get the txt:

private void cadastrar(Context cx)
    {
        InputStream is = getClass().getResourceAsStream("0.txt");
        Scanner s = null;
        s = new Scanner(is);
        atualizar(s, cx);
    }
Jean Carlos
  • 1,613
  • 3
  • 18
  • 26
  • Could you share the classes like com.jeanjn.guiadeacademia.control.DataBase.setData(DataBase.java:35) and other where this error coming, to see more over the bug. – jitain sharma May 01 '14 at 16:10
  • Do you know what Android version is running on the devices with the problem? Different versions of Android have different versions of SQLite (e.g. [see here](http://stackoverflow.com/questions/2421189/version-of-sqlite-used-in-android)) so perhaps you're using something that that version doesn't support (although at a quick glance, the only candidate would be `FOREGIN KEY` and this was introduced in [SQLite 3.6.19](http://www.sqlite.org/foreignkeys.html#parentchild) which is reasonably old). – TripeHound May 01 '14 at 16:17
  • You seem to be reading the SQL from a file. How are you creating/reading the file, especially with regards to charset encoding? I'm thinking of that there's some special chars (BOM for example) at top of the file that make the first `CREATE` token unrecognizable. – laalto May 02 '14 at 06:29

1 Answers1

0

just a guess:

CREATE TABLE ... Exercicio ... references categoria(_id));
CREATE TABLE ... Categoria

you should create table Categoria before creating table Exercicio because Exercicio depends on an existing Categoria.

In case that my guess is wrong: if you add the content of the sql-line to the exception message you will know more what went wrong:

        }catch(Exception e)
        {
            addToErrorLog("Error executing " + line +":" + e.getMessage());
            Suporte.alertar(cx, "Erro ao cadastrar base de dados!\nPor favor, contate-nos no menu 'Fale conosco'.");
        }
k3b
  • 14,517
  • 7
  • 53
  • 85
  • The order is rigth, because my app work as well in 5000+ androids. I put that catch because I know the error, but the user no. What hapness is in some devices not work. – Jean Carlos May 03 '14 at 19:55
  • I don't know, because in my android I dind't get that error. I know about it, because my app's user send report to me. But I think the first line the Sqlite execute. Because, if the first table doesn't exist in my app, my app try to get a online txt with querys. – Jean Carlos May 05 '14 at 18:38