SQLiteException没有这样的表代码1

时间:2018-06-30 18:23:56

标签: java android sqlite exception

有时会出现以下异常:

Fatal Exception: android.database.sqlite.SQLiteException
no such table: OperationQuestions (code 1): , while compiling: SELECT * FROM OperationQuestions WHERE Ticket=2 AND E=1 ORDER BY Number
android.database.sqlite.SQLiteConnection.nativePrepareStatement (SQLiteConnection.java)
android.database.sqlite.SQLiteConnection.acquirePreparedStatement (SQLiteConnection.java:896)
android.database.sqlite.SQLiteConnection.prepare (SQLiteConnection.java:507)
android.database.sqlite.SQLiteSession.prepare (SQLiteSession.java:588)
android.database.sqlite.SQLiteProgram.<init> (SQLiteProgram.java:58)
android.database.sqlite.SQLiteQuery.<init> (SQLiteQuery.java:37)
android.database.sqlite.SQLiteDirectCursorDriver.query (SQLiteDirectCursorDriver.java:44)
android.database.sqlite.SQLiteDatabase.rawQueryWithFactory (SQLiteDatabase.java:1346)
android.database.sqlite.SQLiteDatabase.rawQuery (SQLiteDatabase.java:1285)
com.grydmitrij.gosnadzore_f.db.SEDB.readArray (SEDB.java:58)
com.grydmitrij.gosnadzore_f.model.DividedCategoryTicket.create (DividedCategoryTicket.java:45)
com.grydmitrij.gosnadzore_f.model.Ticket.<init> (Ticket.java:41)
com.grydmitrij.gosnadzore_f.model.DividedCategoryTicket.<init> (DividedCategoryTicket.java:20)
com.grydmitrij.gosnadzore_f.data.TicketViewModel.createDividedCategoryTicket (TicketViewModel.java:45)
com.grydmitrij.gosnadzore_f.adapters.SelectTicketAdapterForSE$1.onClick (SelectTicketAdapterForSE.java:135)
android.view.View.performClick (View.java:5265)

异常被不规则地抛出。我不明白是什么导致异常。我既不能在设备上也不可以在模拟器上自己生成异常。只有用户会得到这种例外。我只能看到报告。

TicketViewModel.java

42    public Ticket createDividedCategoryTicket(int numberTicket, byte category, boolean jumble)
43        {
44            clearTicket();
45            ticket = new DividedCategoryTicket(this.getApplication(),
46                    numberTicket, category, jumble);
47            return ticket;
48        }

DividedCategoryTicket.java

24 @Override
25    public void create()
26    {
27        SEDB db;
28        switch (category)
29        {
          ...
33            case DataBaseStatsHelper.SEE_CATEGORY:
34            case DataBaseStatsHelper.SEF_CATEGORY:
35            default:
36                db = new SEDB(context);
37                break;
38        }
39        db.openBase();
         ...
45        DataBaseQuestion[] dataBaseQuestions = db.readArray(numberTicket, category);

SEDB.java

10 public class SEDB extends AbstractDB
11 {
12  private char[] categoryArray = {'E', 'F', 'E', 'F'};
13
14  public SEDB(Context context)
15  {
16      super(context);
17  }
18
19  @Override
20  public AbstractHelper createHelper()
21  {
22      return new SEHelper(context);
23  }
24
    ...
51
52  public DataBaseQuestion[] readArray(int numTicket, byte category)
53  {
54      if (sqDB == null)
55          openBase();
56      String sql = "SELECT * FROM OperationQuestions WHERE Ticket=" + numTicket +
57              " AND " + categoryArray[category] + "=1" + " ORDER BY Number";
58      Cursor cursor= sqDB.rawQuery(sql, null);
59      int count = cursor.getCount();
        ...
81      cursor.close();
82      return dataBaseQuestions;
83
84  }
}

AbstractDB.java

public abstract class AbstractDB
    {
        protected AbstractHelper helper;
        protected SQLiteDatabase sqDB;
        protected Context context;

        public AbstractDB(Context context)
        {
            this.context = context;
            helper = createHelper();
        }

        public abstract AbstractHelper createHelper();

        public void openBase()
        {
            if (helper == null)
                helper = createHelper();
            sqDB = helper.getReadableDatabase();
        }
        ...
        }

SEHelper.java

public class SEHelper extends AbstractHelper{

    private final static String NAME_PDD="GosNadzorSEE-F.db";
    private final static int VER_PDD=3;

    public SEHelper(Context context)
    {       
        super(context, NAME_PDD, VER_PDD);
    }
}

AbstractHelper.java

public abstract class AbstractHelper extends SQLiteOpenHelper
{
    private String NAME;
    private int VER;
    private String PATH;
    private String PATH_EXTERNAL;

    private Context context;

    public AbstractHelper(Context context, String name, int ver)
    {
        super(context, name, null, ver);
        this.context = context;
        NAME = name;
        VER = ver;
        if (context == null)
            return;
        PATH= Environment.getDataDirectory()+"/data/"+context.getPackageName()+"/databases/";
        PATH_EXTERNAL = context.getExternalFilesDir(null)+"/";
        createDataBase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    public Context getContext()
    {
        return context;
    }

    //проверка на существование файла
    private boolean checkDataBase(String path)
    {
        File dbFile=new File(path+NAME);
        if (dbFile.exists())
        {
            SQLiteDatabase checkDB = null;
            try {
                checkDB = SQLiteDatabase.openDatabase(path+NAME, null,
                        SQLiteDatabase.OPEN_READONLY);
                if (checkDB.getVersion()!=VER)
                {
                    dbFile.delete();
                }
            } catch (Exception e) {
                //Log.w("LOG_SQL", e.getMessage());
                dbFile.delete();
            } finally {
                if (checkDB != null)
                    checkDB.close();
            }

        }
        return dbFile.exists();
    }

    private void createDataBase() //throws IOException
    {
        boolean dbExist = checkDataBase(PATH) || checkDataBase(PATH_EXTERNAL);
        //dbExist=false;
        if (!dbExist) //если база данных еще не создана, то создать
        {
            this.getWritableDatabase();     //создаем сначала пустую БД
            try
            {
                copyDataBase(PATH);
            }
            catch(IOException e)
            {
                try
                {
                    copyDataBase(PATH_EXTERNAL);
                }catch (Exception e1)
                {
                    throw new Error(e1);
                }
            }
        }
    }

    private void copyDataBase(String path) throws IOException
    {
        //Открываем локальную БД как входящий поток
        InputStream input=context.getAssets().open(NAME);
        //Путь к новой БД
        String outFileName=path+NAME;
        //Открываем пустую БД как исходящий поток
        OutputStream output=new FileOutputStream(outFileName);
        //перемещаем байты из входящего файла в исходящий
        byte[] buffer=new byte[1024];
        int length;
        while ((length=input.read(buffer))>0)
        {
            output.write(buffer,0,length);
        }
        //закрываем потоки
        output.flush();
        output.close();
        input.close();
    }
}

1 个答案:

答案 0 :(得分:-1)

我不知道Android或SQLLite是否直接打开连接。我认为您既未指定也未导入JDBC驱动程序和DriverManager。您也没有打开数据库连接