Android - 无法在预制数据库中查找表

时间:2012-07-16 18:31:23

标签: android sqlite sqliteopenhelper

我正在尝试创建一个程序,它将采用我已经创建的SQLite数据库(称为os.sqlite)并能够读取/写入该数据库。我跟着来自here的代码并且Eclipse在复制数据库的过程中没有给我任何错误,但是当我尝试运行我的应用程序并显示表“OregonState”时,LogCat(我正在使用Eclipse Juno ADT插件并在SDK模拟器上运行我的应用程序)一直告诉我没有名为“OregonState”的表,这是我知道我创建的数据库中的一个表并放入项目中的assets文件夹中(I甚至可以看到坐在那里的文件)。尝试失败后,我注意到LogCat也说它无法打开数据库,因为数据库已被锁定。有人指出我可能正确地打开和关闭,但添加了这些,我的应用程序崩溃了。有人能帮忙吗?

我创建了一个Adapter(它还包含一个复制数据库的SQLiteHelper嵌套类)来访问数据库并允许我插入/删除/ etc。到桌子。我的主要活动然后使用此适配器。这是我的代码(为了彻底和完整,我已经包含了所有代码,不包括导入和包名):

public class SQLAdapter
{
private static final String TABLE_OSU = "OregonState";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "Name";
private static final String COLUMN_FIELD = "Field";

private static final String DATABASE_PATH = "/data/data/com.example.sql2/databases/";
private static final String DATABASE_NAME = "os.sqlite";
private static final int DATABASE_VERSION = 1;

MySQLiteHelper helper;
private final Context context;

public SQLAdapter(Context context)
{
    this.context = context;
    helper = new MySQLiteHelper(context);
}

private  class MySQLiteHelper extends SQLiteOpenHelper
{

    private final Context context;

    public MySQLiteHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase database)
    {
        try {
            createDatabase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void createDatabase() throws IOException
    {
        boolean dbExist = checkDatabase();          
        if (dbExist)
        {
            // Nothing
        }
        else
        {
                copyDatabase();
        }
    }

    public boolean checkDatabase()
    {
        SQLiteDatabase checkDB = null;
        try
        {
            String path = DATABASE_PATH + DATABASE_NAME;
            checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException e) {}
        if (checkDB != null)
            checkDB.close();
        return checkDB != null ? true : false;
    }

    public void copyDatabase() throws IOException
    {
        InputStream input = context.getAssets().open(DATABASE_NAME);
        String outFileName = DATABASE_PATH + DATABASE_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();
    }

    public void openDatabase()
    {
        String path = DATABASE_PATH + DATABASE_NAME;
        SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        SQLiteDatabase database = this.getWritableDatabase();
        Log.w(DATABASE_NAME, "Upgrading database from " + oldVersion + " to " + newVersion + ", which will erase all data.");
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_OSU);
        onCreate(database);
        database.close();
    }
}

public SQLiteDatabase open() throws SQLException
{
    SQLiteDatabase database = helper.getWritableDatabase();
    return database;
}

public void close()
{
    helper.close();
}

public long insert(SQLiteDatabase database, String name, String field)
{
    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, name);
    values.put(COLUMN_FIELD, field);
    long x = database.insert(TABLE_OSU, null, values);
    database.close();
    return x;
}

public int delete(SQLiteDatabase database, int id)
{
    int x = database.delete(TABLE_OSU, COLUMN_ID + "=" + id, null);
    return x;
}

public Cursor getAll(SQLiteDatabase database)
{
    return database.query(TABLE_OSU, new String[] {COLUMN_ID, COLUMN_NAME, COLUMN_FIELD}, null, null, null, null, null);
}

public Cursor get(SQLiteDatabase database, int id) throws SQLException
{
    return database.query(TABLE_OSU, new String[] {COLUMN_ID, COLUMN_NAME, COLUMN_FIELD}, COLUMN_ID + "=" + id, null, null, null, null);
}

public int update(SQLiteDatabase database, int id, String name, String field)
{
    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, name);
    values.put(COLUMN_FIELD, field);
    return database.update(TABLE_OSU, values, COLUMN_ID + "=" + id, null);
}
}


public class SQLTest extends Activity
{
SQLAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    adapter = new SQLAdapter(this);
    getAll();
}

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    getMenuInflater().inflate(R.menu.activity_sqltest, menu);
    return true;
} 

public long insert(String name, String field)
{
    SQLiteDatabase database = adapter.open();
    long id = adapter.insert(database, name, field);
    adapter.close();
    return id;
}

public int delete(int id)
{
    SQLiteDatabase database = adapter.open();
    int rowsDeleted = adapter.delete(database, id);
    adapter.close();
    return rowsDeleted;
}

public void getAll()
{
    SQLiteDatabase database = adapter.open();
    TextView tv = new TextView(this);
    String table = "";
    try
    {
        Cursor cursor = adapter.getAll(database);
        if (cursor.moveToFirst())
        {
            do
            {
                table += "\n" + cursor.getString(0) + " " + cursor.getString(1) + " " + cursor.getString(2);
            } while (cursor.moveToNext());
        }
        cursor.close();
    } catch (Exception e) {}
    tv.setText(table);
    setContentView(tv);
    adapter.close();
}

public void get(int id)
{
    SQLiteDatabase database = adapter.open();
    Cursor cursor = adapter.get(database, id);
    TextView tv = new TextView(this);
    String table = "";
    if (cursor.moveToFirst())
        table += "\n" + cursor.getString(0) + " " + cursor.getString(1) + " " + cursor.getString(2);
    else
        table += "No hall found with ID: " + id;
    tv.setText(table);
    setContentView(tv);
    cursor.close();
    adapter.close();
}

public int update(int id, String name, String field)
{
    SQLiteDatabase database = adapter.open();
    int rowsUpdated = adapter.update(database, id, name, field);
    adapter.close();
    return rowsUpdated;
}
}

我注意到this guy有一个类似的问题,虽然我从未真正工作,而他的确如此。我跟着他的代码了一点,这只会让我的应用程序开始崩溃。我只是评论,但我是新来的,所以没有足够的声誉。

1 个答案:

答案 0 :(得分:0)

我按照Yaqub Ahmad和中提琴给我的指示!有效!之前我有一个Adapter类和一个SQLiteOpenHelper的迭代,但我认为将Helper作为嵌套类可能会让事情变得更加困难。我实际上不确定我做错了什么,但我在艾哈迈德的link得到的代码似乎工作得非常好,因为我的表能够在应用程序中打印。谢谢Yaqub Ahmad。我已经提供了代码的链接,这些代码对于我遇到问题的任何人都有用。