没有这样的表:Android SQLite - 更新

时间:2014-04-21 18:56:58

标签: android sqlite external

我一直试图将我的外部SQLite数据库添加到Android项目中几天没有成功。我知道这个问题在过去已经提出过,但我一直无法找到任何明确的答案,为什么这个没有这样的表:"某些人而不是其他人会发生错误。

我已经完成了几乎所有内容以尝试解决问题,其中一些已经适用于其他人,例如创建SQLite表android_metadata并将id标记更改为" _id&#34 ;对于一些似乎对其他人有用的变化,但没有运气..

我似乎也有人说代码适用于Android 4.3但不适用于4.2。我的问题是有人知道为什么这在Android 4.4.2上不起作用吗?我花了很多时间在它上面,它让我疯了,所以如果由于某种原因无法完成,我宁愿不再浪费时间非常感谢任何人都能给我的建议。对不起,咆哮!我已经包含了我的DataBaseHelper类和错误代码。

 package com.smith.jim;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;``
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;


public class DataBaseHelper extends SQLiteOpenHelper{

    //The Android's default system path of your application database.
    private static String DB_PATH = "//data/data/com.smith.jim/databases/";
    //private static String DB_PATH = "context.getApplicationInfo().dataDir/databases/";




    public static final String KEY_ROWID ="_id";
    public static final String KEY_QUESTION ="E_NAME";




    // Data Base Name.
    private static final String DATABASE_NAME = "externalDB";
    // Data Base Version.
    private static final int DATABASE_VERSION = 1;
    // Table Names of Data Base.
    static final String TABLE_Name1 = "android_metadata";
    static final String TABLE_Name2 = "EMP_TABLE";
    static final String TABLE_Name3 = "questions";
    static final String TABLE_Name4 = "sqlite_sequence";

    public Context context;
    static SQLiteDatabase sqliteDataBase;

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null ,DATABASE_VERSION);
        this.context = context;
    }
    @Override
    public void onCreate(SQLiteDatabase arg0) {
    }
    //check if the database exists
    public void createDataBase() throws IOException{
        boolean databaseExist = checkDataBase();
        if(databaseExist){
        // Do Nothing.

        }else{
        this.getWritableDatabase();

        SQLiteDatabase db = this.getWritableDatabase();
        if (db.isOpen()){
        db.close();
        }
        copyDataBase();
        // TODO Auto-generated catch block
        }
    }// end if else dbExist
    // end createDataBase().
    public boolean checkDataBase(){
        File databaseFile = new File(DB_PATH + DATABASE_NAME);
        return databaseFile.exists();
    }
    private void copyDataBase() throws IOException{
        //Open your local db as the input stream
        InputStream myInput = context.getAssets().open(DATABASE_NAME);
        // Path to the just created empty db
        String outFileName = DB_PATH + DATABASE_NAME;
        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);
        //transfer bytes from the input file to the output file
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
        }
        //Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }
    /**
    * This method opens the data base connection.
    * First it create the path up till data base of the device.
    * Then create connection with data base.
    */
    public void openDataBase() throws SQLException{
        //Open the database
        try {
        createDataBase();
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        String myPath = DB_PATH + DATABASE_NAME;
        sqliteDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }
    /**
    * This Method is used to close the data base connection.
    */
    @Override
    public synchronized void close() {
        if(sqliteDataBase != null)
        sqliteDataBase.close();
        super.close();
    }
    //declare methods to fetch data
    public String getBasicCategoryDetails(){
        // TODO Auto-generated method stub
                String[] columns = new String[]{ KEY_ROWID, KEY_QUESTION};
                Cursor c = sqliteDataBase.query(TABLE_Name2, columns, null, null, null, null, null);
                String result = "";

                int iRow = c.getColumnIndex(KEY_ROWID);
                int iQues = c.getColumnIndex(KEY_QUESTION);


                for (c.moveToFirst(); c.isAfterLast(); c.moveToNext()){
                    result = result + c.getString(iRow) + " " + c.getString(iQues) + " "  + "\n"; 
                }

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

}


04-21 14:35:50.160: E/SQLiteLog(2756): (1) no such table: EMP_TABLE
04-21 14:35:50.290: I/Choreographer(2756): Skipped 37 frames!  The application may be doing too much work on its main thread.
04-21 14:35:50.960: I/Choreographer(2756): Skipped 179 frames!  The application may be doing too much work on its main thread.
04-21 14:35:51.050: D/gralloc_goldfish(2756): Emulator without GPU emulation detected.

0 个答案:

没有答案