从资产文件夹复制数据库时出错

时间:2015-03-10 15:26:33

标签: android sqlite

我正在使用sqlite数据库创建一个简单的应用程序,放入资产文件夹并复制并使用它。 我使用下面的代码来实现它

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "numerology.sqlite";

    // Contacts table name
    private static final String TABLE_CONTACTS = "detail";
    public Context context;

    // private static final String KEY_LIKE = "like";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        this.context = context;
        try {
            copyDataBase(DATABASE_NAME);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    private void copyDataBase(String dbname) throws IOException {
        // Open your local db as the input stream
        InputStream myInput = context.getAssets().open(dbname);
        // Path to the just created empty db
        String outFileName = "/data/data/com.example.firstapp/databases/"
                + dbname;
        // 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();
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }

    // Getting All Contacts
    public Utility getDetail(String type, String no) {
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS
                + " where type = '" + type + "' and no = '" + no + "'";

        Utility news = null;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            news = new Utility(cursor.getString(1));
        }

        // return contact list
        cursor.close();
        return news;
    }

}

在我的活动类

中使用此代码
public class DetailActivity extends Activity {
    // TextView contact;
    DatabaseHandler db;
    TextView detail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);
        // contact = (TextView) findViewById(R.id.contact);
        db = new DatabaseHandler(getApplicationContext());

        Utility msg =  db.getDetail("LoveAttitude", "1" );

        detail.setText(msg.toString());

    }
}

当我运行此代码时,首次提供sqlite异常,但在第二次运行时它就像魅力一样。

我认为复制数据库存在问题但经过多次研究后我无法找到解决方案。 帮助我

0 个答案:

没有答案
相关问题