<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
<Button 
android:text="Button" 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></Button>
<TextView 
android:layout_height="wrap_content" 
android:id="@+id/textView1" 
android:text="TextView" 
android:layout_width="match_parent" 
android:layout_weight="0.11"></TextView>
</LinearLayout>界面布局一个按钮,一个TextView,点击按钮,从程序自带的数据库中读取一条记录 数据库类package com.easymorse;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper; public class DataBaseHelper extends SQLiteOpenHelper{
 
    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.easymorse/databases/";
 
    private static String DB_NAME = "f_dic2.db";
 
    private SQLiteDatabase myDataBase; 
 
    private final Context myContext;
 
    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DataBaseHelper(Context context) {
 
     super(context, DB_NAME, null, 1);
        this.myContext = context;
    }
 
  /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    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) {
 
         throw new Error("Error copying database");
 
         }
     }
 
    }
 
    /**
     * Check if the database already exist to avoid re-copying the file each time you open the application.
     * @return true if it exists, false if it doesn't
     */
    private boolean checkDataBase(){
 
     SQLiteDatabase checkDB = null;
 
     try{
     String myPath = DB_PATH + DB_NAME;
     checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
     }catch(SQLiteException e){
 
     //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 SQLiteDatabase openDataBase() throws SQLException{
 
     //Open the database
        String myPath = DB_PATH + DB_NAME;
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        return myDataBase;
    }
 
    @Override
public synchronized void close() {
 
         if(myDataBase != null)
         myDataBase.close();
 
         super.close();
 
}
 
@Override
public void onCreate(SQLiteDatabase db) {
 
}
 
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 
}
 
        // Add your public helper methods to access and get content from the database.
       // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
       // to you to create adapters for your views.
 
}/////////////////主页面代码package com.easymorse;
import java.io.IOException;
import android.app.Activity; 
import android.database.Cursor; 
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.TextView;public class SHARKActivity extends Activity  implements OnClickListener{ 
    
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        View newButton=this.findViewById(R.id.button1);
    newButton.setOnClickListener(SHARKActivity.this);
    } 
    public void onClick(View view) 
    { 
      SQLiteDatabase dicData;
      DataBaseHelper myDbHelper = new DataBaseHelper(null);
         myDbHelper = new DataBaseHelper(this);
         try {
          myDbHelper.createDataBase();
           } catch (IOException ioe) {
       throw new Error("Unable to create database");
   }
   try {
   dicData=myDbHelper.openDataBase();
       }catch(SQLException sqle){
   throw sqle;
   }
   String sql = "select * from Question_DATA where id=1";    
   Cursor cursor =dicData.rawQuery(sql, null);
   if (cursor.getCount() > 0)
   {
   //必须使用moveToFirst方法将记录指针移动到第1条记录的位置
   cursor.moveToFirst();
   String result = cursor.getString(cursor.getColumnIndex("Question_Content"));
   TextView tv=(TextView)SHARKActivity.this.findViewById(R.id.textView1);
   tv.setText(result);
   }
    }
}
出的错误
07-29 06:17:08.109: ERROR/Database(15257): SELECT locale FROM android_metadata failed07-29 06:17:08.149: ERROR/Database(15257): Failed to setLocale() when constructing, closing the database07-29 06:17:08.149: ERROR/Database(15257): android.database.sqlite.SQLiteException: no such table: android_metadata07-29 06:17:08.149: ERROR/Database(15257):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)07-29 06:17:08.149: ERROR/Database(15257):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
07-29 06:17:08.149: ERROR/Database(15257):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1818)07-29 06:17:08.149: ERROR/Database(15257):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)07-29 06:17:08.149: ERROR/Database(15257):     at com.easymorse.DataBaseHelper.checkDataBase(DataBaseHelper.java:74)07-29 06:17:08.149: ERROR/Database(15257):     at com.easymorse.DataBaseHelper.createDataBase(DataBaseHelper.java:41)07-29 06:17:08.149: ERROR/Database(15257):     at com.easymorse.SHARKActivity.onClick(SHARKActivity.java:38)

解决方案 »

  1.   

    检查下数据库里 有android_metadata 这个表吗?
      

  2.   

    我也很奇怪啊,我的程序里没有调用这个表啊我调用的是
    private static String DB_PATH = "/data/data/com.easymorse/databases/";
    private static String DB_NAME = "f_dic2.db";
    f_dic2.db里的Question_DATA表的Question_Content字段
      

  3.   

    checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);改成
    checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);试试
      

  4.   

    用SQLiteDatabase.NO_LOCALIZED_COLLATORS flag 替换SQLiteDatabase.OPEN_READONLY试试。
    参考:http://stackoverflow.com/questions/2528489/no-such-table-android-metadata-whats-the-problem
      

  5.   

    07-29 07:02:59.828: ERROR/Database(23994): SELECT locale FROM android_metadata failed
    07-29 07:02:59.860: ERROR/Database(23994): Failed to setLocale() when constructing, closing the database
    07-29 07:02:59.860: ERROR/Database(23994): android.database.sqlite.SQLiteException: no such table: android_metadata
    07-29 07:02:59.860: ERROR/Database(23994):     at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
    07-29 07:02:59.860: ERROR/Database(23994):     at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1950)
    07-29 07:02:59.860: ERROR/Database(23994):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1818)
    07-29 07:02:59.860: ERROR/Database(23994):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
    07-29 07:02:59.860: ERROR/Database(23994):     at com.easymorse.DataBaseHelper.openDataBase(DataBaseHelper.java:127)
    07-29 07:02:59.860: ERROR/Database(23994):     at com.easymorse.SHARKActivity.onClick(SHARKActivity.java:34)
    07-29 07:02:59.860: ERROR/Database(23994):     at android.view.View.performClick(View.java:2408)
      

  6.   

    昨天给你的连接上有答案了 要加android_metadata table
    http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
    adb shell
    cd /data/data/com.easymorse/databases
    sqlite3  f_dic2.db
    sqlite> CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US');
    sqlite> INSERT INTO "android_metadata" VALUES ('en_US');
    sqlite> .tables
    Question_DATA     android_metadata
    sqlite> .quite
      

  7.   


    是的 应用程序需要这个表 高版本的API会自动生成 ,没有就要自己加了。
      

  8.   

    en_US会不会对数据库中的中文内容造成影响
      

  9.   

    請問一下...手動添加 metadata 但是數據庫還沒被複製到 databases 裡面咧?现在数据库只有 metadata 看不到其他 tables(要复制的数据库有放进 assets了),还要修改什么吗?有没有成功的大大可分享代码呢?谢谢呀
      

  10.   


    試出來了... 代碼在此... 請參考http://www.box.net/shared/u3oictqj599kfbfepm5j可以將 data/data/.../databases/dbtest 刪除再運行...確認無誤 
      

  11.   

    试出来了...代码在此...请参考http://www.box.net/shared/u3oictqj599kfbfepm5j可以将 data/data/.../databases/dbtest  删除再运行...确认无误