错误Sytax SQLITE NEAR“CREATE”在我的应用程序android上

时间:2014-05-01 16:03:28

标签: android sqlite syntax

我在Play商店有一个应用程序,每天我收到错误的报告。但是在某些设备中会出现此错误,但不是全部。我有5000人使用我的应用程序,并且没有得到该错误。

所以:我收到的报告错误:

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

收到错误的设备:

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版: Android 4.0.3 Android 3.2 **

拜托,有人可以帮帮我吗?

我的应用的链接: https://play.google.com/store/apps/details?id=com.jeanjn.guiadeacademia

我的脚本数据库:

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');

这只是其中的一部分。

DataBase类:

    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();
    }
}

该方法调用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'.");
            }
        }
    }

我如何获得txt:

private void cadastrar(Context cx)
    {
        InputStream is = getClass().getResourceAsStream("0.txt");
        Scanner s = null;
        s = new Scanner(is);
        atualizar(s, cx);
    }

1 个答案:

答案 0 :(得分:0)

只是一个猜测:

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

您应该在创建表Categoria之前创建表Exercicio,因为Exercicio依赖于现有的Categoria。

如果我的猜测是错误的:如果你将sql-line的内容添加到异常消息中,你就会知道出了什么问题:

        }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'.");
        }