0

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

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

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

我注意到这个人也有类似的问题,虽然我的从来没有真正起作用,而他的确实如此。我稍微遵循了他的代码,这只会让我的应用程序开始崩溃。我只想发表评论,但我是新来的,所以没有足够的声誉。

4

1 回答 1

0

我听从了 Yaqub Ahmad 和中提琴给我的指示!有效!我之前有一个 Adapter 类和一个 SQLiteOpenHelper 的迭代,但我认为可能与让 Helper 成为嵌套类有关可能会使事情变得更加困难。我实际上不确定我做错了什么,但我在 Ahmad 的链接上获得的代码似乎工作得很好,因为我的表格可以在应用程序中打印。谢谢你,亚古布·艾哈迈德。我已经提供了对我有用的代码的链接,以供遇到我的问题的任何人使用。

于 2012-07-17T15:49:15.320 回答