从资源文件夹复制时,复制的数据库为空

时间:2016-04-06 20:13:44

标签: android database null copy assets

我是android编程的新手,我混合了一些代码来编写我需要的东西。我需要将一个数据库从assets文件夹复制到内部存储器并根据给定的ID获取它的一些内容。但是,当我复制数据库时,getContent()方法返回null,这会导致一些问题。此外,还有一个Checkdatabase()中的SQliteDataBase.openDatabase方法中的错误。如果有人在这个问题上指导我,我将不胜感激。这是我的代码:

MyActivity类:

Store.GetDefaultFolder

DatabaseHelper类:

    package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.sql.SQLException;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       final Button btn = (Button) findViewById(R.id.button);
       final Button btn1 = (Button) findViewById(R.id.button1);
       final TextView txt = (TextView) findViewById(R.id.textView);
       final DataBaseHelper db= new DataBaseHelper(this);
       final String TAG = "my log in myActivity";

       btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {

               try
               {
                   db.createDataBase();
                   //Toast.makeText(getApplicationContext(),"copy database",Toast.LENGTH_LONG).show();
                   //db.copyDataBase();
               }
               catch (IOException io)
               {
                   throw new Error("Unable to create database");
               }

               try {
                   db.openDataBase();
                   //Toast.makeText(getApplicationContext(),"database opened " + db.getContact(1),Toast.LENGTH_LONG).show();

               } catch (SQLException e) {

                   e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                   Log.d(TAG,"Error"+e);
               }



              //Toast.makeText(getApplicationContext(),"go to get content",Toast.LENGTH_LONG).show();
              Contacts contacts = db.getContact(1);
              Log.d(TAG,"log");
              if(contacts == null){
                  Log.d(TAG,"null");
              }
              //Toast.makeText(getApplicationContext(),"value is: " + contacts.getContent(),Toast.LENGTH_LONG).show();
              //txt.setText(String.valueOf(contacts.getContent()));
           }
       });

    }
 }

0 个答案:

没有答案