Android的 - 不能在pre-国产数据库查找表 [英] Android - Can't Find Table in Pre-made Database

查看:145
本文介绍了Android的 - 不能在pre-国产数据库查找表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个程序,将采取我已经取得了SQLite数据库(称为os.sqlite),并能够读/写数据库。我跟着从 code此处和Eclipse为我提供了复制数据库的过程中没有错误,现在看来,但当我尝试运行我的应用程序,并显示表OregonState,LogCat中(我使用Eclipse的Juno与ADT插件和运行我在SDK模拟器应用程序)不断告诉我,有没有名为OregonState,表,该表中,我知道我创建,并在项目投产后我的资产的文件夹(我甚至可以看到该文件坐在那里数据库中的表)。更失败的尝试后,我发现LogCat中也说,它失败,因为该数据库被锁定打开数据库。被指出是我可能被正确打开和关闭,但那些补充说,我的应用程序崩溃。任何人能帮助?

I am trying to make a program that will take an SQLite database I already made (called os.sqlite) and be able to read/write that database. I followed code from here and Eclipse gives me no errors in the process of copying the database, it seems, but when I try to run my application and display the table "OregonState," LogCat (I am using Eclipse Juno with the ADT plug-in and running my applications on the SDK emulator) keeps telling me that there is no table named "OregonState," which is a table in the database that I know I created and put into my assets folder in the project (I can even see the file sitting there). After more failed attempts, I noticed LogCat also saying that it failed to open the database because the database is locked. That was pointed out to be that I might be properly opening and closing, but with those added, my application crashes. Anyone able to help?

我创建了一个适配器(也包含SQLiteHelper嵌套类数据库,副本)来访问数据库,并允许我插入/删除/等。到表中。我的主要活动,然后使用该适配器。这里是我的code(我已经包括了这一切,但不包括进口和包名)为被彻底和完整)着想:

I created an Adapter (that also contains an SQLiteHelper nested class which copies the database) to access the database and allow me to insert/delete/etc. to the table. My main activity then uses this adapter. Here is my code (I've included all of it, excluding import and package names) for the sake of being thorough and complete):

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

我注意到这家伙也有类似的问题,虽然我的从未实际工作,而他做到了。我顺着他的code一点点,这不仅让我的应用程序开始崩溃。我只是简单地发表评论,但我是新来的,所以没有足够的声誉。

I noticed this guy had a similar issue, although mine has never actually worked, whereas his did. I followed his code a little bit, and that only made my app start to crash. I would simply just comment, but I'm new here, so not enough reputation.

推荐答案

我接着雅各布·艾哈迈德和中提琴给我的方向!有效!我有一个适配器类和之前SQLiteOpenHelper的迭代器,但我想也许是与具有助手被嵌套类可能已作出事情变得更加困难。实际上,我不知道那是什么,我做了错事,但code我在艾哈迈德的<得到了href=\"http://stackoverflow.com/questions/9109438/using-already-created-database-with-android/9109728#9109728\">link似乎奇妙的工作,因为我的表是能够在应用程序中进行打印。谢谢你,雅各布·艾哈迈德。我所提供的链接到code,它为我工作为别人有我的问题。

I followed the directions given to me by Yaqub Ahmad and viola! It worked! I had an iteration of an Adapter class and an SQLiteOpenHelper before, but I think maybe something to do with having the Helper be a nested class may have made things more difficult. I'm actually not sure what it was that I had done wrong, but the code I got at Ahmad's link seemed to work wonderfully, as my table is able to print in the application. Thank you, Yaqub Ahmad. I have provided the link to the code that worked for me for anyone else having my problem.

这篇关于Android的 - 不能在pre-国产数据库查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆