//本来这样写的,但是每次运行都要把db文件导入到/data/data/com.xx.demo_weather,有没有其他方法??
db = SQLiteDatabase.openOrCreateDatabase(
    "/data/data/com.xx.demo_weather/chinacity.db", null);
anroid数据库

解决方案 »

  1.   

    我之前要将外部的数据库读进来的做法是:
         将db文件放在res的raw路径(可以自己添加这个路径)下面,要打开它的时候这样:
             private String filename = DB_PATH+"/"+DB_NAME;
     public void open()throws SQLException
    {
    writefromraw(filename);
    db = opendatabase();
    }
            private void writefromraw(String dbfile) {
    // TODO Auto-generated method stub
    try {
                if (!(new File(dbfile).exists())) {
                    InputStream is = mContext.getResources().openRawResource(
                            R.raw.livetv_database);
                    FileOutputStream fos = new FileOutputStream(dbfile);
                    byte[] buffer = new byte[BUFFER_SIZE];
                    int count = 0;
                    while ((count = is.read(buffer)) > 0) {
                        fos.write(buffer, 0, count);
                    }
                    fos.close();
                    is.close();
                }
            } catch (FileNotFoundException e) {
                Log.e("Database", "File not found");
                e.printStackTrace();
            } catch (IOException e) {
                Log.e("Database", "IO exception");
                e.printStackTrace();
            } }DB_PATH我是这样定义的:   
     public static final String DB_PATH = "/data"+Environment.getDataDirectory().getAbsolutePath()
                                      +"/"+PACKAGE_NAME;
    DB_NAME肯定就是你数据库的名字啦。
      

  2.   


    谢谢,我这样写哪里有错?public class TestActivity extends Activity {
    static SQLiteDatabase db;
    private final static String DATABASE_PATH = android.os.Environment
    .getExternalStorageDirectory().getAbsolutePath() + "/database";
    private static String DATABASE_FILENAME = "chinacity.db";
    private Context mContext; public TestActivity(Context context) {
    // TODO Auto-generated constructor stub
    this.mContext = context;
    } // 使用外部数据库
    public static SQLiteDatabase openDatabase(Context context) {
    try {
    String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
    File dir = new File(DATABASE_PATH);
    if (!dir.exists()) // 如果文件夹不存在创建文件夹
    dir.mkdir();
    if (!(new File(databaseFilename)).exists()) { // 如果文件不存在创建文件
    InputStream is = context.getResources().openRawResource(
    R.raw.chinacity);
    FileOutputStream fos = new FileOutputStream(databaseFilename);
    byte[] buffer = new byte[8192];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
    fos.write(buffer, 0, count);
    }
    fos.close();
    is.close();
    }
    db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null); } catch (Exception e) {
    e.printStackTrace();
    }
    return db;
    } @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    System.out.println("数据库地址" + DATABASE_PATH);
    Context c = new TestActivity(mContext);
    SQLiteDatabase db = TestActivity.openDatabase(c);
    System.out.println("数据库为===========" + db);
    }
    }
      

  3.   

     SQLiteDatabase db = TestActivity.openDatabase(c);
    这个是什么?一般在android里面使用数据库的话最好要实现一个一个继承SQLiteOpenHelper的类,用来管理数据库建立之类的。我之前实现了一个类,不过没有继承SQLiteOpenHelper,哈哈,如果可以最好是继承它啦。给你参考一下。
    package com.***.***;import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.database.sqlite.SQLiteDatabase.CursorFactory;
    import android.os.Environment;
    import android.util.Log;public class DatabaseAdapter {
    private static final String TAG = "DatabaseAdapter";    public static final int BUFFER_SIZE = 400000;
        
        public static final String PACKAGE_NAME = "com.***.***";//你的包名
        
        public static final String DB_PATH = "/data"+Environment.getDataDirectory().getAbsolutePath()
                                      +"/"+PACKAGE_NAME;

    public static final String CHANNEL_IDC = "channel_id";//数据库里面的列名

    public static final String CHANNEL_NAME = "channel_name";//数据库里面的列名

    public static final String CHANNEL_NUMBER = "channel_number";//数据库里面的列名

    public static final String FREE = "free";//数据库里面的列名

    public static final String  TYPE_ID = "type_id";//数据库里面的列名

    public static final String ADDRESS_ID = "address_id";//数据库里面的列名

    public static final String URL = "url";//数据库里面的列名

    public static final String URLTYPE = "urlType";//数据库里面的列名

    public static final String CHANNEL_IDA = "channel_id";//数据库里面的列名

    private static final String DB_NAME = "***.db";//数据库名字

    private static final String DB_TABLEC = "***";//数据库里面表的名字

    private static final String DB_TABLEA = "***";//数据库里面表的名字

    private static final int DB_VERSION = 1;

    private Context mContext = null;

    private SQLiteDatabase db = null;
        private String filename = DB_PATH+"/"+DB_NAME;


    public DatabaseAdapter(Context context) {
    mContext = context;
    }
        
     public void open()throws SQLException
    { writefromraw(filename);
    db = opendatabase();
    }

        public SQLiteDatabase opendatabase() throws SQLException
          {
           db = SQLiteDatabase.openOrCreateDatabase(filename, null);
           return db;
            }

    private void writefromraw(String dbfile) {
    // TODO Auto-generated method stub
    try {
                if (!(new File(dbfile).exists())) {
                    InputStream is = mContext.getResources().openRawResource(
                            R.raw.livetv_database);
                    FileOutputStream fos = new FileOutputStream(dbfile);
                    byte[] buffer = new byte[BUFFER_SIZE];
                    int count = 0;
                    while ((count = is.read(buffer)) > 0) {
                        fos.write(buffer, 0, count);
                    }
                    fos.close();
                    is.close();
                }
            } catch (FileNotFoundException e) {
                Log.e("Database", "File not found");
                e.printStackTrace();
            } catch (IOException e) {
                Log.e("Database", "IO exception");
                e.printStackTrace();
            } } public String fetchData(long rowId)throws SQLException//查询DB_TABLEA里面CHANNEL_IDA等于rowId的URL数据
    {
    Cursor mCursor = db.query(DB_TABLEA, new String[]{URL}, CHANNEL_IDA + "=" + rowId, null, null, null, null); if (mCursor.moveToFirst())
    {
    String url = mCursor.getString(mCursor.getColumnIndex(URL));
    mCursor.close();
    return url;
    }
    mCursor.close();
    return null;

    }

    public Cursor fetchAllData()//查询DB_TABLEC里面CHANNEL_IDC和CHANNEL_NAME这两列所有的数据
    {
    Cursor mCursor = db.query(DB_TABLEC, new String[]{CHANNEL_IDC,CHANNEL_NAME}, null, null, null, null, null);
    return mCursor;

    }

    public void close()
    {
    if (db != null)
    db.close();
    }
    }
    实现了这个类之后,要使用数据库就很方便啦。先定义一个这个类的实例,然后调用它的open()方法就可以打开数据库了,然后如果你要取得里面的某一个数据的话就用fetchData这个方法,全部都取得的话就用fetchAllData的方法,当然这里方法里面你要取的列是那些就要你自己定义啦,还有fetchData里面过滤的条件也是你自己要定义啊。
        还有要提醒的一点是调用open()方法在用完数据库后记得要调close()方法啊,不然会报错的。还有fetchAllData也是,Cursor对象用完也是需要close()的,fetchAllData可以参考fetchData,用法差不多的。