如何在android应用程序中使用外部sqlite数据库

时间:2015-02-18 17:25:56

标签: android sqlite android-sqlite

我想在我的Android应用中使用我的 companies.db sqlite数据库。我把它复制在" assets" android项目的文件夹。并使用DatabaseHelper类,如下所示:

public class DatabaseHelper extends SQLiteOpenHelper {
String DB_PATH = null;
public static String DB_NAME = "companies";
private SQLiteDatabase myDataBase;
private final Context myContext;
Context ctx;

public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, 2);
    this.myContext = context;
    DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        // do nothing - database already exist
    } else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
            //throw new Error("Error copying database");
        }
    }
}
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        e.printStackTrace();
        // database does\'t exist yet.
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}
/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {
    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);
    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;
    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
    // transfer bytes from the inputfile to the outputfile
    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();
}
public void openDataBase() throws SQLException {
    // Open the database
    String myPath = DB_PATH + DB_NAME;
    // SQLiteDatabase.NO_LOCALIZED_COLLATORS
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY
                    | SQLiteDatabase.NO_LOCALIZED_COLLATORS
                    | SQLiteDatabase.CREATE_IF_NECESSARY);
}
@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    super.close();
}
// return cursor
public Cursor query(String table, String[] columns, String selection,
                    String[] selectionArgs, String groupBy, String having,
                    String orderBy) {
    return myDataBase.query("pwp_singers", null, null, null, null, null,
            null);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion)
        try {
            copyDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我在一个我想要数据库信息的活动中使用下面的代码:

DatabaseHelper myDbHelper=new DatabaseHelper(ListViewActivity.this);
    try {
        myDbHelper.createDataBase();
    }catch(Exception e)
    {}
    myDbHelper.openDataBase();
    db = myDbHelper.getReadableDatabase();
    Cursor c;
    c= db.rawQuery("select * from companies_fa where id=103", null);

但是当我运行应用程序时,我遇到了一些例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmad.exhibition/com.example.ahmad.exhibition.ListViewActivity}: android.database.sqlite.SQLiteException: no such table: companies_fa: , while compiling: select * from companies_fa where id=103
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
        at android.app.ActivityThread.access$500(ActivityThread.java:122)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:132)
        at android.app.ActivityThread.main(ActivityThread.java:4123)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.database.sqlite.SQLiteException: no such table: companies_fa: , while compiling: select * from companies_fa where id=103
        at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
        at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
        at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:146)
        at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:367)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:130)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
        at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1539)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1508)
        at com.example.ahmad.exhibition.ListViewActivity.onCreate(ListViewActivity.java:70)
        at android.app.Activity.performCreate(Activity.java:4397)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
            at android.app.ActivityThread.access$500(ActivityThread.java:122)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:132)
            at android.app.ActivityThread.main(ActivityThread.java:4123)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:491)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
            at dalvik.system.NativeStart.main(Native Method)

companies_fa是我存储我的信息的表,我的代码中的问题在哪里?我究竟做错了什么?提前谢谢。

4 个答案:

答案 0 :(得分:1)

使用SQLiteAssetHelper(https://github.com/jgilfelt/android-sqlite-asset-helper)解决了我的问题!

答案 1 :(得分:0)

您可以使用此数据库帮助程序类

在资产中创建一个名为数据库的文件夹,并将您的数据库文件放在此处。

将DATABASE_NAME变量更改为您的数据库全名。只是带有扩展名的文件名。

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

class DatabaseHelper extends SQLiteOpenHelper {

    final private static int DATABASE_VERSION = 1;
    final private static String DATABASE_NAME = "wordlist.db";

    private AssetManager assets;
    private String databaseDir;

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        assets = context.getAssets();
        databaseDir = context.getApplicationInfo().dataDir + "/databases/";

        File file = new File(databaseDir);
        if(!file.exists()) file.mkdir();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        copyDatabase();
    }

    @Override
    public SQLiteDatabase getWritableDatabase() {
        if(!isDatabaseExist())
            copyDatabase();
        return super.getWritableDatabase();
    }

    @Override
    public SQLiteDatabase getReadableDatabase() {
        if(!isDatabaseExist())
            copyDatabase();
        return super.getReadableDatabase();
    }


    private Boolean isDatabaseExist() {
        return new File(databaseDir + DATABASE_NAME).exists();
    }

    private void copyDatabase() {

        try {
            InputStream inputStream = assets.open("databases/" + DATABASE_NAME);

            FileOutputStream outputStream = new FileOutputStream(databaseDir + DATABASE_NAME);

            byte[] buffer = new byte[8 * 1024];

            int readed;
            while ((readed = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, readed);
            }

            outputStream.flush();

            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

答案 2 :(得分:0)

您可以使用以下示例在Android应用程序中使用外部SQLite数据库:

public void initDb(Context context, String from) {
    File f = context.getDatabasePath("db_mame.db");
    if(!f.exist) {
        copyDatabase(from, f.getAbsolutePath());
    } 
    initDbHandler();
}

答案 3 :(得分:0)

查询必须更改为:

c= db.rawQuery("select * from companies_fa where id=='103'", null);