检索数据库到listview时出错

时间:2012-06-06 04:27:25

标签: android database sqlite

我的主要课程

 public class Tips2Main extends Activity{

DBOpener dbopener;

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

    dbopener = new DBOpener(this);    
}


 @Override
 protected void onResume() {
 super.onResume();

 // Configure the listview
 ArrayList<String> tips = new ArrayList<String>();
 ListView lstTips = (ListView)this.findViewById(R.id.lst_tips);
 lstTips.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tips));
 dbopener.createDatabase();

  Cursor Tips = dbopener.getTips();
  while (Tips.moveToNext()) {
      tips.add(Tips.getString(0)); // Get the current subj
  }
  Tips.close();
  lstTips.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, tips));


  // Update the listview
  ArrayAdapter<String> ad = (ArrayAdapter<String>)lstTips.getAdapter();
  ad.notifyDataSetChanged();

   }

  //Close the DB when app pauses
    @Override
   protected void onPause() {
  super.onPause();
  dbopener.close();
    }

   }

我的数据库:

    public class DBOpener extends Activity{
private static String DB_PATH = 
        "/data/data/sg.edu.iit.mns/databases/"; //path of our database
    private static String DB_NAME ="Tips"; // Database name
    private final Context myContext;
    private SQLiteDatabase db;

    public DBOpener(Context context) {
        //super(context, DB_NAME, null, 1 );
        myContext = context;
    }

    public void createDatabase() {
        boolean dbExists = checkDatabase();
        if (!dbExists) {
            try
            {
                copyDatabase();
            }

            catch (IOException e)
            {
                Toast.makeText(myContext, "Error", Toast.LENGTH_LONG).show();
            }
        }
        String path = DB_PATH + DB_NAME;
        db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
    }

    private boolean checkDatabase() {
        File database = new File(DB_PATH + DB_NAME);
        return database.exists();
    }

    private void copyDatabase() throws IOException {
        InputStream istream = myContext.getAssets().open(DB_NAME);
        OutputStream ostream = new FileOutputStream(DB_PATH + DB_NAME);

        // Transfer bytes from istream to ostream
        byte[] buffer = new byte[1024];
        int length;
        while ((length = istream.read(buffer)) > 0) {
            ostream.write(buffer, 0, length);
        }
        // Close streams
        istream.close();
        ostream.flush();
        ostream.close();
    }

    public synchronized void close()
    {
        if(db !=null)
            db.close();
    }

    // Retrieve tips
    public Cursor getTips() {
        if (db == null)
            return null;
        return db.query("tips", new String[] {"Tips"},
            null, null, null, null, null);
    }

错误日志:

     06-06 04:21:34.214: I/Database(719): sqlite returned: error code = 14, msg = cannot open file at source line 25467
     06-06 04:21:34.224: E/Database(719): sqlite3_open_v2("/data/data/sg.edu.iit.mns/databases/Tips", &handle, 2, NULL) failed
     06-06 04:21:34.224: D/AndroidRuntime(719): Shutting down VM
     06-06 04:21:34.224: W/dalvikvm(719): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
     06-06 04:21:34.264: E/AndroidRuntime(719): FATAL EXCEPTION: main
     06-06 04:21:34.264: E/AndroidRuntime(719): java.lang.RuntimeException: Unable to resume activity {sg.edu.iit.mns/sg.edu.iit.mns.Tips2Main}: android.database.sqlite.SQLiteException: unable to open database file
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.os.Handler.dispatchMessage(Handler.java:99)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.os.Looper.loop(Looper.java:123)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.ActivityThread.main(ActivityThread.java:4627)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at java.lang.reflect.Method.invokeNative(Native Method)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at java.lang.reflect.Method.invoke(Method.java:521)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at dalvik.system.NativeStart.main(Native Method)
     06-06 04:21:34.264: E/AndroidRuntime(719): Caused by: android.database.sqlite.SQLiteException: unable to open database file
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1812)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at sg.edu.iit.mns.DBOpener.createDatabase(DBOpener.java:41)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at sg.edu.iit.mns.Tips2Main.onResume(Tips2Main.java:33)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.Activity.performResume(Activity.java:3823)
     06-06 04:21:34.264: E/AndroidRuntime(719):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
     06-06 04:21:34.264: E/AndroidRuntime(719):     ... 12 more

当我尝试从数据库填充listview时,它强制关闭应用程序。我不确定如何进行错误检查并使其运行。在这里帮助不大。谢谢。数据库已在assets文件夹中。

3 个答案:

答案 0 :(得分:0)

这是您的错误“android.database.sqlite.SQLiteException:无法打开数据库文件”。我会在你的DBOpener类中扩展SQLiteOpenHelper。请参阅此示例应用,特别是此网址中的类,以获取快速模板:http://developer.android.com/resources/samples/SearchableDictionary/src/com/example/android/searchabledict/DictionaryDatabase.html

答案 1 :(得分:0)

试试这个链接,它提供了关于SQLite和样本的详细描述..

AndroidSQLite

SQLite Samples

它可能对你有帮助......

答案 2 :(得分:0)

1。)为什么要从Activity类扩展数据库类?
改变

public class DBOpener extends Activity{

public class DBOpener{

2.。)在数据库类中添加此方法:

public DBOpener openDB() throws SQLException {
    db = dataHelper.getWritableDatabase();
    return this;
}

copyDatabase()

调用此方法
private void copyDatabase() throws IOException {
    InputStream istream = myContext.getAssets().open(DB_NAME);

希望它有所帮助!!